简体   繁体   English

如何从 flutter 的底部导航栏中删除 label 以便我可以水平居中添加按钮?

[英]How can I remove label from bottom navigation bar in flutter so I can center horizontally my add button?

can someone help me with this problem.有人可以帮我解决这个问题。 The only solution i have currently found, is to set both showSelectedLabels and showUnselecedLabels to false.我目前找到的唯一解决方案是将 showSelectedLabels 和 showUnselecedLabels 都设置为 false。 However this will remove all the labels, but i only want to remove the label of the add button.但是,这将删除所有标签,但我只想删除添加按钮的 label。 If I just use a placeholder "" for label, my add button is off centred horizontally...如果我只为 label 使用占位符“”,我的添加按钮不在水平居中...

button off centered horizontally按钮关闭水平居中

the result i want我想要的结果

Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: tabs[_selectedIndex],
      ),
      bottomNavigationBar: BottomNavigationBar(
        elevation: 10,
        backgroundColor: Colors.white,
        type: BottomNavigationBarType.fixed,
        selectedIconTheme: IconThemeData(color: kPrimaryMagentaColor),
        selectedLabelStyle: TextStyle(fontWeight: FontWeight.w500),
        selectedItemColor: Colors.black,
        showSelectedLabels: true,
        showUnselectedLabels: true,
        items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Container(
              padding: kBottomNavIconLabelSpace,
              child: Icon(
                FeatherIcons.map,
                size: 26.5,
              ),
            ),
            label: 'Map',
          ),
          BottomNavigationBarItem(
            icon: Container(
              padding: kBottomNavIconLabelSpace,
              child: Icon(
                FeatherIcons.compass,
                size: 28,
              ),
            ),
            label: 'Discover',
          ),
          BottomNavigationBarItem(
            icon: Container(
              decoration: BoxDecoration(
                color: kPrimaryMagentaColor,
                shape: BoxShape.circle,
              ),
              padding: EdgeInsets.all(10),
              child: Icon(
                FeatherIcons.plus,
                color: Colors.white,
              ),
            ),
            label: "",
          ),
          BottomNavigationBarItem(
            icon: Container(
              padding: kBottomNavIconLabelSpace,
              child: Transform(
                alignment: Alignment.center,
                transform: Matrix4.rotationY(math.pi),
                child: Icon(
                  FeatherIcons.messageSquare,
                  size: 28,
                ),
              ),
            ),
            label: 'Inbox',
          ),
          BottomNavigationBarItem(
            icon: Container(
              padding: kBottomNavIconLabelSpace,
              child: Icon(
                FeatherIcons.calendar,
                size: 28,
              ),
            ),
            label: 'My Activity',
          ),
        ],
        currentIndex: _selectedIndex,
        onTap: _onItemTapped,
      ),
    );

Add this to the BottomNavigationBar properties将此添加到 BottomNavigationBar 属性

showSelectedLabels: false,
showUnselectedLabels: false,

Ok, so this is kinda possible by manipulating the Text 's fontSize .好的,所以这可以通过操纵TextfontSize来实现。

First, change all the label s you have used for each of the BottomNavigationBarItem to use title parameter.首先,将您用于每个BottomNavigationBarItem的所有label更改为使用title参数。 Like this,像这样,

Change label: 'Map' to title: Text('Map') . label: 'Map'更改为title: Text('Map') Similarly change it with all your BottomNavigationBarItem .同样用你所有的BottomNavigationBarItem改变它。 Since this is not possible using label parameter.因为使用label参数是不可能的。

Now, for your center BottomNavigationBarItem use it like this,现在,对于您的中心BottomNavigationBarItem像这样使用它,

BottomNavigationBarItem(
  icon: Align(
    alignment: Alignment.bottomCenter,
    child: Container(
      decoration: BoxDecoration(color: Colors.deepPurple, shape: BoxShape.circle),
      padding: EdgeInsets.all(14),
      child: Icon(Icons.add, color: Colors.white),
    ),
  ),
  title: Text("", style: TextStyle(fontSize: 0)),
),

So, you are changing two things.所以,你正在改变两件事。

  1. Increase padding of your Container to EdgeInsets.all(14) to make the button look bigger.Containerpadding增加到EdgeInsets.all(14)以使按钮看起来更大。
  2. Change fontSize using style: TextStyle(fontSize: 0) , this make the view invisible.使用样式更改字体大小: TextStyle( fontSize style: TextStyle(fontSize: 0) ,这会使视图不可见。

Now, you have something like this, change the color to whatever you want.现在,你有这样的东西,将颜色更改为你想要的任何颜色。

在此处输入图像描述

If you use 2 params with如果您使用 2 个参数

showSelectedLabels: false, showUnselectedLabels: false,

with label != ""label != ""

You can get this issue .你可以得到这个问题

If you are using showSelectedLabels:true on BottomNavigationBar the icons doesnot be on center so try to avoid showSelectedLabels:true so remove showSelectedLabels:true如果您在 BottomNavigationBar 上使用showSelectedLabels:true图标不在中心所以尽量避免 showSelectedLabels:true 所以删除showSelectedLabels:true

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 flutter 的底部导航栏中垂直和水平地在中心添加一个按钮? - How do I add a button in the center both vertically and horizontally in a bottom navigation bar in flutter? 我可以在 Flutter 底部导航栏中的图标周围添加间距吗? - Can I add spacing around an icon in Flutter Bottom Navigation Bar? 如何将导航路线添加到 flutter 底部导航路线 - How can I add navigation routes to flutter bottom navigation routes 如何在 Flutter 中实现这种类型的自定义底部导航栏? - How can I achieve this type of custom Bottom Navigation Bar in Flutter? 如何在Flutter中为底部导航栏创建一个animation? - How can I create an animation for the bottom navigation bar in Flutter? 我如何在颤动中实现这个底部导航栏 - How can i achieve this bottom navigation bar in flutter 如何删除底部导航栏中浮动操作按钮的白色背景? - How can I remove white background of floating action button in bottom navigation bar? 如何在按下按钮 flutter 时隐藏底部导航栏? - How can i hide my bottom nav bar on button pressed, flutter? 如何使我在颤动中使用堆栈实现的底部导航栏中的白色区域透明 - How can I make the white area transparent in this bottom navigation bar that I have implemented using stack in flutter 如何在带边框的中心底部导航栏中添加浮动操作按钮? - How to add Floating action Button in Bottom Navigation Bar in Center with border?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM