简体   繁体   English

Flutter - 如何隐藏/删除 BottomNavigationBarItem 的标题

[英]Flutter - How to hide/remove title of BottomNavigationBarItem

so i have this flutter app, and i'm trying to hide or remove the title.所以我有这个颤振应用程序,我试图隐藏或删除标题。 I tried leaving the title as an empty string ie new Text("") but that messed with the alignment of the navbar.我尝试将标题保留为空字符串,即new Text("")但这与导航栏的对齐方式混淆了。

Desired Outcome:期望的结果:

她是我想要达到的

What i'm getting (if i leave the title as empty string):我得到了什么(如果我将标题保留为空字符串):

在此处输入图片说明 :

There are two workarounds for this problem, as this feature is not yet implemented.此问题有两种解决方法,因为此功能尚未实现。

  1. Pass Container(height: 0.0) instead of Text("")通过Container(height: 0.0)而不是Text("")
  2. Create widget and use it instead of Flutter's bottom navigation.创建小部件并使用它代替 Flutter 的底部导航。 Source . 来源

Update:更新:

Just add this to your BottomNavigationBar只需将此添加到您的 BottomNavigationBar

showSelectedLabels: false,
showUnselectedLabels: false,

Now you can simply disable labels for selected or unselected items:现在您可以简单地禁用选定或未选定项目的标签:

bottomNavigationBar: BottomNavigationBar(
  showSelectedLabels: false,   // <-- HERE
  showUnselectedLabels: false, // <-- AND HERE
  items: [
    BottomNavigationBarItem(
      icon: Icon(Icons.person),
      title: Text('Personal')
    ),
    BottomNavigationBarItem(
      icon: Icon(Icons.notifications),
      title: Text('Notifications'),
    ),
  ]
  ...
)

...resulting in: ...导致:

结果

Hopefully, this snippet helps someone.希望这个片段可以帮助某人。 It worked well for me.它对我来说效果很好。

bottomNavigationBar : new BottomNavigationBar(
      items: [
      BottomNavigationBarItem(
        icon: Icons.search,
        title: Padding(padding: EdgeInsets.all(0))
      ),
      BottomNavigationBarItem(
        icon: Icons.share,
        title: Padding(padding: EdgeInsets.all(0))
      ),
      BottomNavigationBarItem(
        icon: Icons.call,
        title: Padding(padding: EdgeInsets.all(0))
      )],
     type: BottomNavigationBarType.fixed
) 
//bottomNavBar

As of now, this feature is not implement.截至目前,此功能尚未实现。 For a BottomNavigationBarItem , title is a required field对于BottomNavigationBarItem , title 是必填字段

But you can build a new widget for this.但是您可以为此构建一个新的小部件。

Try this :尝试这个 :

Column buildButtonColumn(IconData icon) {
 Color color = Theme.of(context).primaryColor;

  return Column(
    mainAxisSize: MainAxisSize.min,
    mainAxisAlignment: MainAxisAlignment.center,
    children: [
      Icon(icon, color: color),
    ],
  );
}

Widget buttonSection = Container(
  child: Row(
    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
    children: [
      buildButtonColumn(Icons.call),
      buildButtonColumn(Icons.near_me),
      buildButtonColumn(Icons.share),
    ],
  ),
);

I have tried this approach and it works like charm:我已经尝试过这种方法,它的作用就像魅力一样:

BottomNavigationBarItem(
  icon: Icon(
    Icons.home,
    size: 40,
  ),
  title: Text(
    "",
    style: TextStyle(fontSize: 0),
  ),
)

It worked well for me.它对我来说效果很好。

BottomNavigationBarItem(
  icon: Icon(
    Icons.home,
  ),
  title: SizedBox.shrink(),
)

Use BottomAppBar to achieve this BottomNavigationBarItem without label.使用BottomAppBar来实现这个没有标签的BottomNavigationBarItem You could further customize it.您可以进一步自定义它。

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: body,
      bottomNavigationBar: new BottomAppBar(
          child: new Row(
            mainAxisSize: MainAxisSize.max,
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: <Widget>[
              IconButton(
                  icon: Icon(Icons.home),
                  disabledColor: Colors.green,
                  onPressed: _currentIndex == 0
                      ? null
                      : () => setState(() => _currentIndex = 0)),
              IconButton(
                  icon: Icon(Icons.notifications),
                  disabledColor: Colors.green,
                  onPressed: _currentIndex == 1
                      ? null
                      : () => setState(() => _currentIndex = 1)),
              IconButton(
                  icon: Icon(Icons.settings),
                  disabledColor: Colors.green,
                  onPressed: _currentIndex == 2
                      ? null
                      : () => setState(() => _currentIndex = 2)),
            ],
          )
      ),
    );
  }

Hope it really helps.希望它真的有帮助。

In the new version of flutter, the title is depreciated, and we must provide the label.在新版flutter中,title是折旧的,必须提供label。 So, make the label an empty string因此,将标签设为空字符串

          BottomNavigationBarItem(
            label: '',
            icon: Icon(
              Icons.home_rounded,
              color: kHintColor,
              size: 35,
            ),
            activeIcon: Icon(
              Icons.home_rounded,
              color: kMainColor,
              size: 35,
            ),
          ),

and add the following to the BottomNavigationBar:并将以下内容添加到 BottomNavigationBar:

        selectedLabelStyle: TextStyle(fontSize: 0),
        unselectedLabelStyle: TextStyle(fontSize: 0),
title: Container(height: 0.0)

will give you some extra padding below.下面会给你一些额外的填充。 You can use您可以使用

title: Text(
      '',
      style: TextStyle(fontWeight: FontWeight.bold, height: 0.0),
)

One can use bottom navigation bar type to shifting .可以使用底部导航栏类型来切换

  bottomNavigationBar: BottomNavigationBar(
      type: BottomNavigationBarType.shifting,
      items: [
        BottomNavigationBarItem(icon: Icon(Icons.star, color: Colors.red,), title: Text("")),
        BottomNavigationBarItem(icon: Icon(Icons.star, color: Colors.red,), title: Text(""))
      ]
  ),

To display icons without any labels, below step worked for me: Set selectedFontSize: 0 and set label to empty string.要显示没有任何标签的图标,以下步骤对我有用:设置 selectedFontSize: 0 并将标签设置为空字符串。 For example,例如,

BottomNavigationBar(
      selectedFontSize: 0,
      items: BottomNavigationBarItem(
                icon: Icons.search
                label: '',
              ),
)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM