0

As someone new to Flutter/Dart, I have a simplistic proof-of-concept web app (code provided below) where I've genuinely put the effort in to reading & prototyping but cannot quite get the code finished.

The app is a test of named routes. FirstScreen uses an ElevatedButton to jump to SecondScreen. SecondScreen can only return to FirstScreen via the Back arrow in SecondScreen's AppBar, but I also want the ability to dynamically hide that Back arrow if the app is started from SecondScreen, or enable it if the app is started from FirstScreen. Why ? Because the idea is to provide the option to run SecondScreen in a kiosk mode where the user's navigation is restricted to that one route of the app.

I designed the code to use a ChangeNotifier/Consumer approach using Flutter's "provider" package. The state is provided by the routedViaHome boolean (default is false) in RouteMonitor. The only time the boolean should be set true is if the app starts at FirstScreen and the user clicks the ElevatedButton to go to SecondScreen; that change is observed by SecondScreen and should be used to dynamically set its own AppBar 'automaticallyImplyLeading' property.

My question is this: the Consumer is correctly displaying the Text widget, but how do I ALSO get it to update the automaticallyImplyLeading property ? What am I missing please and/or have I needlessly complicated this ?

Thank you !

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

void main() {
  runApp(
    ChangeNotifierProvider(
      create: (context) => RouteMonitor(),
      child: const MyApp(),
    )
  );
}

class RouteMonitor with ChangeNotifier {
  bool routedViaHome = false;

  void routeViaHome() {
    routedViaHome = true;
    notifyListeners();
  }
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Named Routes Demo',
      initialRoute: '/',
      routes: {
        '/': (context) => const FirstScreen(),
        '/second': (context) => const SecondScreen(),
      },
    );
  }
}

class FirstScreen extends StatelessWidget {
  const FirstScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('First Screen'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            Provider.of<RouteMonitor>(context, listen: false).routeViaHome();
            Navigator.pushNamed(context, '/second');
            //Navigator.pushReplacementNamed(context, '/second');
            },
          child: const Text('Launch screen'),
        ),
      ),
    );
  }
}

class SecondScreen extends StatelessWidget {

  const SecondScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Second Screen'),
        automaticallyImplyLeading: false,
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Text('This is static data but no navigation'),
            Consumer<RouteMonitor>(
              builder: (context, routemonitor, child) => Text(
                '${routemonitor.routedViaHome}',
                style: Theme.of(context).textTheme.headlineMedium,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
Ken White
  • 123,280
  • 14
  • 225
  • 444
Gaz
  • 328
  • 3
  • 15

0 Answers0