如何在Flutter中使用BottomAppBar进行导航

19

我是Flutter新手,已经创建了一个很好的带有停靠FAB的BottomAppBar,但我也想将其用于页面导航。 我尝试使用BottomNavigationBar,但那样我就失去了停靠的浮动操作按钮。如何将导航实现到底部应用栏中?

floatingActionButton: Container(
        height: 65.0,
        width: 65.0,
        child: FittedBox(
          child: FloatingActionButton(

        onPressed: (){},
        child: Icon(Icons.add, color: Colors.white,),
        // elevation: 5.0,
      ),
        ),
      ),


      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      bottomNavigationBar: BottomAppBar(
        // elevation: 20.0,

        shape: CircularNotchedRectangle(),
        child: Container( 
          height: 75,
          child: Row( 

        mainAxisSize: MainAxisSize.max,
        mainAxisAlignment: MainAxisAlignment.spaceBetween,

        children: <Widget>[
          IconButton(
            iconSize: 30.0,
            padding: EdgeInsets.only(left: 28.0),
            icon: Icon(Icons.home),
            onPressed: () {
              setState(() {
                currentIndex = 0;
              });
            },
          ),

          IconButton(            
            iconSize: 30.0,
            padding: EdgeInsets.only(right: 28.0),
            icon: Icon(Icons.search),
            onPressed: () {
               setState(() {
                currentIndex = 1;
                print("${currentIndex}");

              });
            },
          ),
          IconButton(
            iconSize: 30.0,
            padding: EdgeInsets.only(left: 28.0),
            icon: Icon(Icons.notifications),
            onPressed: () {
               setState(() {
                currentIndex = 2;
                print("${currentIndex}");

              });
            },
          ),
          IconButton(
            iconSize: 30.0,
            padding: EdgeInsets.only(right: 28.0),
            icon: Icon(Icons.list),
            onPressed: () {
               setState(() {
                currentIndex = 3;
                print("${currentIndex}");
              });
            },
          )
        ],
      ),
        )
      )

1
只需将 BottomNavigationBar 放置在 BottomAppBarchild 属性中即可。 - Daksh Gargas
那并不真正起作用。 - Kőne Mátyás
3个回答

32

使用PageView小部件是一种方法。

示例代码中使用了您编写的BottomAppBar

class _DemoPageState extends State<FormPage> {
  PageController _myPage = PageController(initialPage: 0);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      bottomNavigationBar: BottomAppBar(
        shape: CircularNotchedRectangle(),
        child: Container(
          height: 75,
          child: Row(
            mainAxisSize: MainAxisSize.max,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              IconButton(
                iconSize: 30.0,
                padding: EdgeInsets.only(left: 28.0),
                icon: Icon(Icons.home),
                onPressed: () {
                  setState(() {
                    _myPage.jumpToPage(0);
                  });
                },
              ),
              IconButton(
                iconSize: 30.0,
                padding: EdgeInsets.only(right: 28.0),
                icon: Icon(Icons.search),
                onPressed: () {
                  setState(() {
                    _myPage.jumpToPage(1);
                  });
                },
              ),
              IconButton(
                iconSize: 30.0,
                padding: EdgeInsets.only(left: 28.0),
                icon: Icon(Icons.notifications),
                onPressed: () {
                  setState(() {
                    _myPage.jumpToPage(2);
                  });
                },
              ),
              IconButton(
                iconSize: 30.0,
                padding: EdgeInsets.only(right: 28.0),
                icon: Icon(Icons.list),
                onPressed: () {
                  setState(() {
                    _myPage.jumpToPage(3);
                  });
                },
              )
            ],
          ),
        ),
      ),
      body: PageView(
        controller: _myPage,
        onPageChanged: (int) {
          print('Page Changes to index $int');
        },
        children: <Widget>[
          Center(
            child: Container(
              child: Text('Empty Body 0'),
            ),
          ),
          Center(
            child: Container(
              child: Text('Empty Body 1'),
            ),
          ),
          Center(
            child: Container(
              child: Text('Empty Body 2'),
            ),
          ),
          Center(
            child: Container(
              child: Text('Empty Body 3'),
            ),
          )
        ],
        physics: NeverScrollableScrollPhysics(), // Comment this if you need to use Swipe.
      ),
      floatingActionButton: Container(
        height: 65.0,
        width: 65.0,
        child: FittedBox(
          child: FloatingActionButton(
            onPressed: () {},
            child: Icon(
              Icons.add,
              color: Colors.white,
            ),
            // elevation: 5.0,
          ),
        ),
      ),
    );
  }
}

将图标按钮包裹在容器中,并对其进行填充,就能赋予图标按钮圆形涟漪效果。 - meditat

7
BottomAppBarBottomNavigationBar的区别在于,使用后者可以设置一个子页面列表,当你点击下方的图标时,这些页面将被渲染出来(通过onTap方法)。而使用BottomAppBar,你需要设置一个Navigator方法,从UI/UX角度来看,我认为这样做不太美观。

  1. 创建一个辅助组件,它将包含BottomAppBar
  2. 然后,将一个Row作为其child方法传递进去。
  3. 填充IconButtons。
  4. 将onPressed方法设置为调用页面:(Navigator.of(context).pushName('/yourScreenHere')

然后,对于你制作的每个屏幕,都可以添加一个AppBar。


1
只需将 BottomNavigationBar 放置在 BottomAppBarchild 属性中即可。 - Daksh Gargas

2
你可以使用 switch casetabcontrollerradiobutton 一样来控制主体。只需要在按下 bottomAppBar 图标时更新主体即可。查看此链接以获得更好的理解。 :) "Original Answer" 翻译成 "最初的回答"。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接