【问题标题】:Flutter : align icon to the middle -- bottom navigation barFlutter:将图标对齐到中间——底部导航栏
【发布时间】:2023-03-17 02:16:02
【问题描述】:

我的 Flutter 应用中有一个底部导航栏。如何将图标对齐到中间。目前,它似乎是顶部对齐

Widget _bottomNavigationBar(int selectedIndex) {
    return BottomNavigationBar(
      backgroundColor: Palette.YELLOW,
      onTap: (int index) {
        setState(() {
          userModel.selectedIndex = index;
        });
      },
      currentIndex: userModel.selectedIndex,
      items: <BottomNavigationBarItem>[
        BottomNavigationBarItem(
          icon: Image.asset(
            "assets/images/search_nav_icon.png",

          ),
          title: Text(''),
        ),
        BottomNavigationBarItem(
            icon: Image.asset(
              "assets/images/fav_nav_icon.png",

            ),
      ],
    );
  }

【问题讨论】:

    标签: flutter


    【解决方案1】:

    当使用图标但没有文本时,这对我有用。

    返回底部导航栏( 显示选择标签:假, showUnselectedLabel:假, // ... );

    【讨论】:

      【解决方案2】:

      Container 包装每个图标并将其alignment 属性设置为Alignment.center

      现在用Expanded 小部件包装每个Container,并在行小部件的帮助下将它们水平分组。

       Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget> [
          Expanded(child: Container(alignment:Alignment.center,child: Icon([...]),),),
          Expanded(child: Container(alignment:Alignment.center,child: Icon([...]),),),
          Expanded(child: Container(alignment:Alignment.center,child: Icon([...]),),)
        ]
      )
      

      您可以根据自己的方便增加/减少 Expanded 小部件的数量,但请确保不要将任何其他单子小部件包裹在 Expanded 小部件上

      【讨论】:

      • 我们需要将此行与 BottomNavigationBar 一起使用吗?
      • 这个解决方案对我不起作用。我也有标签
      【解决方案3】:

      简单的解决方案是在 BottomNavigationBar 中使用以下属性:

       showSelectedLabels: false,
       showUnselectedLabels: true,
      

      【讨论】:

        【解决方案4】:

        如果您想要完全的灵活性,您可以创建自己的 bottomNavigationBar 这是我几个月前的方法

        bottomNavigationBar: Container(
          color: primary,
          padding: EdgeInsets.all(20.0),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center, // Optional
            mainAxisAlignment: MainAxisAlignment.spaceEvenly, // Change to your own spacing
            children: [
              Text("SHOPS"), //REPLACE WITH ICON
              Text("ORDERS"), //REPLACE WITH ICON
            ],
          ),
        ),
        

        【讨论】:

          【解决方案5】:

          一个简单的解决方案是 - 用 Column 包裹 BottomNavigationBar 小部件。 并设置mainAxisAlignment: MainAxisAlignment.center

          return Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget> [
              BottomNavigationBar(...)
            ]
          );
          

          必须有更好的方法来做到这一点,但我发现这种技术非常有用。

          【讨论】:

            猜你喜欢
            • 2013-07-16
            • 1970-01-01
            • 1970-01-01
            • 2021-06-27
            • 2020-11-27
            • 1970-01-01
            • 2016-10-31
            • 1970-01-01
            相关资源
            最近更新 更多