简体   繁体   English

带有导航器的底部导航栏?

[英]BottomNavigationBar with Navigator?

I have a problem with this code, is not working why?我对这段代码有问题,为什么不工作? please.请。

My code:我的代码:

Container(
  color: Colors.white,
  child: BottomNavigationBar(
    onTap: (_index) {
      StepState.disabled.index;
      switch (_index) {
        case 0:
          onTap:
          () {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => Home()),
            );
          };
          break;

        case 1:
          onTap:
          () {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => NosVoyages()),
            );
          };
          break;
      }
      ;
    },
    currentIndex: _index,
    items: [
      BottomNavigationBarItem(
          label: '',
          icon: Padding(
            padding: const EdgeInsets.only(top: 10.0),
            child: Icon(Icons.home),
          )),
      BottomNavigationBarItem(
          label: '',
          icon: Padding(
            padding: const EdgeInsets.only(top: 10.0),
            child: Icon(Icons.list_alt),
          )),
    ],
    type: BottomNavigationBarType.fixed,
    backgroundColor: Colors.white,
    unselectedItemColor: Colors.black,
  ),
);

The home page is loading fine but as soon as I click any button, it change nothing.主页加载正常,但只要我点击任何按钮,它什么都没有改变。 Probably there is an issue with MaterialApp or Scaffold but I am not able to fix it yet. MaterialApp 或 Scaffold 可能存在问题,但我还无法修复它。 Can anyone tell me what's the problem and how to fix it?谁能告诉我是什么问题以及如何解决?

try this variant initialized _index试试这个变体初始化_index

int _index=0;
 @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: SafeArea(
            top: false,
            child: Container(
              color: Colors.white,
              child: Padding(
                padding: const EdgeInsets.all(20.0),
                child: BottomNavigationBar(
                  onTap: (_index) {
                    StepState.disabled.index;
                    switch (_index) {
                      case 0:
                        {
                          Navigator.push(
                            context,
                            MaterialPageRoute(builder: (context) => Home()),
                          );
                          setState(() {
                            _index = 1;
                          });
                        }
                        ;
                        break;

                      case 1:
                        {
                          setState(() {
                            _index = 0;
                          });
                          Navigator.push(
                            context,
                            MaterialPageRoute(
                                builder: (context) => NosVoyages()),
                          );
                        }
                        ;
                        break;
                    }
                  },
                  currentIndex: _index,
                  items: [
                    BottomNavigationBarItem(
                        label: '',
                        icon: Padding(
                          padding: const EdgeInsets.only(top: 10.0),
                          child: Icon(Icons.home),
                        )),
                    BottomNavigationBarItem(
                        label: '',
                        icon: Padding(
                          padding: const EdgeInsets.only(top: 10.0),
                          child: Icon(Icons.list_alt),
                        )),
                  ],
                  type: BottomNavigationBarType.fixed,
                  backgroundColor: Colors.white,
                  unselectedItemColor: Colors.black,
                ),
              ),
            )));
  }

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

相关问题 如何将 BottomNavigationBar 与 Navigator 一起使用? - How to use BottomNavigationBar with Navigator? 将 Navigator 与 bottomNavigationBar 一起使用 - Using Navigator with a bottomNavigationBar Flutter BottomNavigationBar 带 Navigator 无需更换(丢失状态) - Flutter BottomNavigationBar with Navigator and without replacement (loosing state) 如何在 Navigator.push 之后更改 BottomNavigationBar? - How to change the BottomNavigationBar after a Navigator.push? Flutter BottomNavigationBar 调用导航器推送一个导航器项 - Flutter BottomNavigationBar call navigator push for one Navigator item Flutter:使用导航器推送到新屏幕时保留 BottomNavigationBar - Flutter: Keep BottomNavigationBar When Push to New Screen with Navigator bottomNavigationBar 在调用 Navigator.pushNamed 时不断重建多次 - bottomNavigationBar keeps rebuilding multiple times when calling Navigator.pushNamed Flutter/Dart - Navigator.pop(context) 在添加 BottomNavigationBar 后停止工作 - Flutter/Dart - Navigator.pop(context) stopped working after adding BottomNavigationBar 当我们谈论 Flutter 中的性能时,使用 Navigator 比 BottomNavigationBar 更好吗? - is it better to use Navigator over BottomNavigationBar when we talk about performance in Flutter? 延迟的BottomNavigationBar - Delayed BottomNavigationBar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM