【问题标题】:How to display more than 3 items in a BottomNavigationBar while coding in flutter在颤动中编码时如何在BottomNavigationBar中显示3个以上的项目
【发布时间】:2018-11-20 11:20:39
【问题描述】:

我尝试添加 5 个 BottomNavigationBarItem,但是如果我尝试添加 3 个以上的项目,编译器会抛出错误。它看起来像这样:

 The following RangeError was thrown building BottomNavigationBar(dirty, state:
_BottomNavigationBarState#a56dd(tickers: tracking 3 tickers)):
 RangeError (index): Invalid value: Not in range 0..2, inclusive: 3

我需要在底部导航栏中显示 5 个项目。帮我解决这个问题。

BottomNavigationBar missing

有代码的链接,目前那里只有三个项目,我想再添加两个项目而不会让编译器抛出错误消息

【问题讨论】:

  • PageView 的子项数量是否与底部导航项的数量相匹配?请在问题中显示更多代码。
  • 代码与链接中的相同
  • 分享您的代码。似乎您正在使用索引 = 3 的东西,但您只有 3 个项目(索引范围 = 0..2)。所以 index = 3 无效
  • 我添加了链接,请看代码。

标签: dart material-design flutter


【解决方案1】:

只是为了将来有关此问题的查询而编写它。

只需在您的 BottomNavigationBar 构造函数中添加一个额外的参数 -

type : BottomNavigationBarType.fixed

Check also the Flutter Official Ref.

可选:从头开始重新启动您的应用以修复渲染问题。

【讨论】:

    【解决方案2】:

    这是因为默认的 NavigatioBar 不支持超过 3 个项目,在您的 BottomNavigationBar 小部件中使用此参数: type: BottomNavigationBarType.fixed

    或复制并粘贴以下代码

        bottomNavigationBar: BottomNavigationBar(
         type: BottomNavigationBarType.fixed, ///This allow more than 3 items
         backgroundColor: Theme.Colors.primaryDarkColor,
        currentIndex: 1,
         items: [
        BottomNavigationBarItem(icon: Icon(Icons.arrow_drop_up,), title: Text("GLO", 
        style: TextStyle(color: Colors.black),),),
        BottomNavigationBarItem(icon: Icon(Icons.arrow_drop_up), title: 
        Text("MTN"),),
        BottomNavigationBarItem(icon: Icon(Icons.arrow_drop_up), title: 
        Text("Airtel"),),
        BottomNavigationBarItem(icon: Icon(Icons.arrow_drop_up), title: 
        Text("9mobile"),),
         ],
      ),
    

    【讨论】:

    • 不应该被否决,因为它正确地传达了要点:BottomNavigationBarType.fixed
    猜你喜欢
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    • 2021-03-16
    • 1970-01-01
    • 2021-11-09
    • 2021-02-23
    • 2018-07-05
    • 2022-11-26
    相关资源
    最近更新 更多