如何将按钮添加到上面挂出的底部导航栏 - Flutter

时间:2018-03-15 12:26:02

标签: android flutter bottomnavigationview floating-action-button

我的底部导航栏中心项目如下图所示:

如何在Flutter中实现这样的东西?

我发现我放入BottomNavigatonBarItem的每个图标都符合导航栏的界限。但我需要把它挂在上面。

4 个答案:

答案 0 :(得分:6)

您可以Stack在彼此的顶部显示小部件。 结合属性overflowOverflow.visible,以及符合您需求的对齐方式。

以下示例将实现图片中的内容:水平居中的浮动按钮,顶部与底栏对齐。

return new Scaffold(
  bottomNavigationBar: new Stack(
    overflow: Overflow.visible,
    alignment: new FractionalOffset(.5, 1.0),
    children: [
      new Container(
        height: 40.0,
        color: Colors.red,
      ),
      new Padding(
        padding: const EdgeInsets.only(bottom: 12.0),
        child: new FloatingActionButton(
          notchMargin: 24.0,
          onPressed: () => print('hello world'),
          child: new Icon(Icons.arrow_back),
        ),
      ),
    ],
  ),
);

答案 1 :(得分:2)

Google最近添加了一个名为BottomAppBar的东西,它提供了一种更好的方法。只需在脚手架中添加BottomAppBar,即可创建导航FAB,并向您要添加文本的FAB添加标签。创建类似于以下结果:https://cdn-images-1.medium.com/max/1600/1*SEYUo6sNrW0RoKxyrYCqbg.png

Widget build(BuildContext context) {
  return new Scaffold(
    appBar: AppBar(title: const Text('Tasks - Bottom App Bar')),
    floatingActionButton: FloatingActionButton.extended(
      elevation: 4.0,
      icon: const Icon(Icons.add),
      label: const Text('Add a task'),
      onPressed: () {},
    ),
    floatingActionButtonLocation: 
      FloatingActionButtonLocation.centerDocked,
    bottomNavigationBar: BottomAppBar(
      hasNotch: false,
      child: new Row(
        mainAxisSize: MainAxisSize.max,
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          IconButton(
            icon: Icon(Icons.menu),
            onPressed: () {},
          ),
          IconButton(
            icon: Icon(Icons.search),
            onPressed: () {},
          )
        ],
      ),
    ),
  );
}

答案 2 :(得分:0)

您也可以使用FloatingActionButtonLocation和Expanded窗口小部件,如下所示:

Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: _buildTodoList(),
      floatingActionButton: new FloatingActionButton(
        onPressed: _pushAddTodoScreen,
        tooltip: 'Increment',
        child: new Icon(Icons.add),
        elevation: 4.0,
      ),
      bottomNavigationBar: BottomAppBar(
        child: new Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Expanded(child: IconButton(icon: Icon(Icons.home)),),
            Expanded(child: IconButton(icon: Icon(Icons.show_chart)),),
            Expanded(child: new Text('')),
            Expanded(child: IconButton(icon: Icon(Icons.tab)),),
            Expanded(child: IconButton(icon: Icon(Icons.settings)),),
          ],
        ),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
    );
  }

预览:

答案 3 :(得分:0)

除了@Raunak的输入之外,您还可以使用FloatingActionButton的“ border”属性来获得无缝边框以生成所需的效果-代码段如下:

Widget buildFab() {
  FloatingActionButton fab = FloatingActionButton(
    backgroundColor: Color(0xFF9966CC),
    child: Icon(Icons.add),
    shape: CircleBorder(
      side: BorderSide(
        color: Colors.white,
        width: 3.0,
      ),
    ),
    tooltip: "Add...",
    onPressed: () {
      print("fab is pressed!!!");
    }
  );

  return fab;
}

如果将FAB包裹在Material中并为其添加阴影效果,效果会更好-如下所示:

Material buildFabWithShadow() {
  return Material(
  shadowColor: Color(0x802196F3),
  shape: CircleBorder(), 
  elevation: 16.0,
  child: buildFab(),
);

尝试时,我得到了1的效果-请注意,边框宽度可以根据您的意愿进行调整!