How to use BottomNavigationBar with Navigator?

后端 未结 6 781
北荒
北荒 2020-12-02 08:36

The Flutter Gallery example of BottomNavigationBar uses a Stack of FadeTransitions in the body of the Scaffold.

I

6条回答
  •  失恋的感觉
    2020-12-02 08:55

    Here is an example how you can use Navigator with BottomNavigationBar to navigate different screen.

    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);
      final String title;
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State {
      // This navigator state will be used to navigate different pages
      final GlobalKey _navigatorKey = GlobalKey();
      int _currentTabIndex = 0;
    
      @override
      Widget build(BuildContext context) {
        return SafeArea(
          child: Scaffold(
            body: Navigator(key: _navigatorKey, onGenerateRoute: generateRoute),
            bottomNavigationBar: _bottomNavigationBar(),
          ),
        );
      }
    
      Widget _bottomNavigationBar() {
        return BottomNavigationBar(
          type: BottomNavigationBarType.fixed,
          items: [
            BottomNavigationBarItem(
              icon: Icon(Icons.home),
              title: Text("Home"),
            ),
            BottomNavigationBarItem(
                icon: Icon(Icons.account_circle), title: Text("Account")),
            BottomNavigationBarItem(
              icon: Icon(Icons.settings),
              title: Text("Settings"),
            )
          ],
          onTap: _onTap,
          currentIndex: _currentTabIndex,
        );
      }
    
      _onTap(int tabIndex) {
        switch (tabIndex) {
          case 0:
            _navigatorKey.currentState.pushReplacementNamed("Home");
            break;
          case 1:
            _navigatorKey.currentState.pushReplacementNamed("Account");
            break;
          case 2:
            _navigatorKey.currentState.pushReplacementNamed("Settings");
            break;
        }
        setState(() {
          _currentTabIndex = tabIndex;
        });
      }
    
      Route generateRoute(RouteSettings settings) {
        switch (settings.name) {
          case "Account":
            return MaterialPageRoute(builder: (context) => Container(color: Colors.blue,child: Center(child: Text("Account"))));
          case "Settings":
            return MaterialPageRoute(builder: (context) => Container(color: Colors.green,child: Center(child: Text("Settings"))));
          default:
            return MaterialPageRoute(builder: (context) => Container(color: Colors.white,child: Center(child: Text("Home"))));
        }
      }
    }
    

提交回复
热议问题