Flutter: Keep BottomNavigationBar When Push to New Screen with Navigator

后端 未结 10 735
情深已故
情深已故 2020-12-25 12:55

In iOS, we have a UITabBarController which stays permanently at the bottom of the screen when we push to a new ViewController.

In Flutter, we have a bottomNavigation

10条回答
  •  天命终不由人
    2020-12-25 13:52

    Option 1: If you only want to keep BottomNavigationBar then try to use this.

    Option 2: Use CupertinoTabBar as shown below for the static BottomNavigationBar.

    import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';
    import 'package:mqttdemo/Screen2.dart';
    import 'package:mqttdemo/Screen3.dart';
    
    import 'Screen1.dart';
    
    class Home extends StatefulWidget {
      @override
      _HomeState createState() => _HomeState();
    }
    
    class _HomeState extends State {
      int _currentIndex;
      List _children;
    
      @override
      void initState() {
        _currentIndex = 0;
        _children = [
          Screen1(),
          Screen2(),
          Screen3(),
        ];
        super.initState();
      }
    
      @override
      Widget build(BuildContext context) {
        return CupertinoTabScaffold(
          tabBar: CupertinoTabBar(
            currentIndex: _currentIndex,
            onTap: onTabTapped,
            items: [
              BottomNavigationBarItem(
                icon: Icon(Icons.home),
                title: Text("Screen 1"),
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.home),
                title: Text("Screen 2"),
              ),
              BottomNavigationBarItem(
                  icon: Icon(Icons.home), title: Text("Screen 3")),
            ],
    
          ),
            tabBuilder: (BuildContext context, int index) {
              return CupertinoTabView(
                builder: (BuildContext context) {
                  return SafeArea(
                    top: false,
                    bottom: false,
                    child: CupertinoApp(
                      home: CupertinoPageScaffold(
                        resizeToAvoidBottomInset: false,
                        child: _children[_currentIndex],
                      ),
                    ),
                  );
                },
              );
            }
        );
      }
    
      void onTabTapped(int index) {
        setState(() {
          _currentIndex = index;
        });
      }
    }
    

    Navigate to screen4 from Screen3 as shown below:

        class Screen3 extends StatefulWidget {
          @override
          _Screen3State createState() => _Screen3State();
        }
        
        class _Screen3State extends State {
          @override
          Widget build(BuildContext context) {
            return Container(
              color: Colors.black,
              child: Center(
                child: RaisedButton(
                  child: Text("Click me"),
                  onPressed: () {
                    Navigator.of(context, rootNavigator: false).push(MaterialPageRoute(
                        builder: (context) => Screen4(), maintainState: false));
                  },
                ),
              ),
            );
          }
    
    }
    

提交回复
热议问题