Need a persistent/same Bottom Navigation Bar for all screens - Flutter

前端 未结 9 1114
不思量自难忘°
不思量自难忘° 2021-02-01 21:50

I am a beginner with flutter and dart. I have been trying to implement a navigationBar on three different pages in my app. The toggling works well for an individual

9条回答
  •  感情败类
    2021-02-01 22:00

    Use PageView and bottomNavigationBar :

    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    /// This Widget is the main application widget.
    class MyApp extends StatelessWidget {
      static const String _title = 'Flutter App';
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: _title,
          home: App(),
        );
      }
    }
    
    class App extends StatefulWidget {
      App({Key key}) : super(key: key);
      _AppState createState() => _AppState();
    }
    
    class _AppState extends State {
      PageController _myPage;
      var selectedPage;
    
      @override
      void initState() {
        super.initState();
        _myPage = PageController(initialPage: 1);
        selectedPage = 1;
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            body: PageView(
              physics: NeverScrollableScrollPhysics(),
              controller: _myPage,
              children: [
                Center(
                  child: Text("Another Page"),
                ),
                Center(
                    child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text("Page 1"),
                    RaisedButton(
                      onPressed: () {
                        _myPage.jumpToPage(0);
                        setState(() {
                          selectedPage = 0;
                        });
                      },
                      child: Text("Go to another page"),
                    )
                  ],
                )),
                Center(child: Text("Page 2")),
                Center(child: Text("Page 3")),
              ],
            ),
            bottomNavigationBar: BottomAppBar(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: [
                  IconButton(
                    icon: Icon(Icons.home),
                    color: selectedPage == 1 ? Colors.blue : Colors.grey,
                    onPressed: () {
                      _myPage.jumpToPage(1);
                      setState(() {
                        selectedPage = 1;
                      });
                    },
                  ),
                  IconButton(
                    icon: Icon(Icons.star),
                    color: selectedPage == 2 ? Colors.blue : Colors.grey,
                    onPressed: () {
                      _myPage.jumpToPage(2);
                      setState(() {
                        selectedPage = 2;
                      });
                    },
                  ),
                  IconButton(
                    icon: Icon(
                      Icons.settings,
                    ),
                    color: selectedPage == 3 ? Colors.blue : Colors.grey,
                    onPressed: () {
                      _myPage.jumpToPage(3);
                      setState(() {
                        selectedPage = 3;
                      });
                    },
                  ),
                ],
              ),
            ));
      }
    }
    

    Hope it helps.

提交回复
热议问题