简体   繁体   中英

Using MaterialApp Builder to have a common scaffold across screens, but back button is not displayed in AppBar on Navigation

I'm trying to use MaterialApp Builder to have a common scaffold across screens, but back button is not displayed in AppBar on Navigation. Im quite new to Flutter. Any direction to understand the reason/to fix it would help.

I'm using onGenerateRoute to setup the route.

Material App Widget

class MyApp extends StatefulWidget {
  // This widget is the root of your application.
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final AppRouter _appRouter = AppRouter();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: APP_TITLE,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      builder: (context, child) => Scaffold(
        appBar: AppBar(
          title: Text(APP_TITLE),
        ),
        body: child,
      ),
      onGenerateRoute: _appRouter.onGenerateRoute,
    );
  }

  @override
  void dispose() {
    _appRouter.dispose();
    super.dispose();
  }
}

Home Screen


class _HomeScreenState extends State<HomeScreen> {
  int _counter = 0;

  @override
  Widget build(BuildContext context) {
    return Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
           Text(
              'My Home Screen',
            ),
            ElevatedButton(
              child: Text("Navigate Back Here"),
              onPressed: () {
                Navigator.of(context).pushNamed('/');
              },
            ),
          ],
        ),
      );
  }
}

I think you can not push the home route and have a back button You may use: Navigator.of(context).push(MaterialPageRoute(builder:(_)=>SecondScreen()));

The '/' route is defined for home route by default in flutter, So if you want to define you named routes, use something else for ex: 'SecondScreen/' for you second screen widget

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM