首页 > 解决方案 > 颤动底部导航栏图标颜色不变。是某种 setState 错误还是我的代码有问题?

问题描述

我是 Flutter 的新手,实际上我不是编码员,但我正在尝试构建一个可以执行基本操作的应用程序,例如登录页面、注册、用户数据显示在屏幕上,而且我可以从我卡在底部导航栏中的应用程序,我从没想过它会很难通过。
我没有放弃,而是决定四处搜索并得出不同的结果(其中一些很难理解和混淆)。我正在尝试构建一个导航栏,以最少的内存使用(高效代码)路由到不同的页面
我很困惑,我现在不知道该怎么办!请帮助另一个编码器(如果有人解决了这个问题,我会认为自己是编码器,不要想太多

  @override
  Widget build(BuildContext context) {
    PageController _pageController = PageController();
    final List<Widget> _tabs = [FeedPage(), ListingPage(), SettingPage()];

    int _selectedIndex = 0;
    void _onPageChanged(int index) {
      setState(() {                                //Is something wrong with my setState?
        _selectedIndex = index;
      });
    }

    void _onItemTapped(int _selectedIndex) {
      _pageController.jumpToPage(_selectedIndex);
    }

    return Scaffold(
      body: PageView(
        controller: _pageController,
        children: _tabs,
        onPageChanged: _onPageChanged,                         //Check it out
        physics: NeverScrollableScrollPhysics(),
      ),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _selectedIndex,                          //I use this
        onTap: _onItemTapped,
        items: [
          BottomNavigationBarItem(
            icon: Icon(
              Icons.home,
            ),
            title: Text("Home"),
          ),
          BottomNavigationBarItem(
            icon: Icon(
              Icons.list,
            ),
            title: Text("List"),
          ),
          BottomNavigationBarItem(
            icon: Icon(
              Icons.person,
            ),
            title: Text("Profile"),
          ),
        ],
      ),
    );
  }
}
//is this the code bugged? or something wrong in my flutter IED or something I am confused

标签: flutterflutter-layoutsetstate

解决方案


试试下面的代码,在脚手架 perntesis 之前插入这段代码,也不要忘记插入包。

 bottomNavigationBar: CurvedNavigationBar(
        color: Colors.black26,
        backgroundColor: Color(0xFFFFC5C5),
        buttonBackgroundColor: Colors.greenAccent,
        height: 50.0,
        animationCurve: Curves.bounceInOut,
        items: <Widget>[
          Icon(Icons.translate, size: 20, color: Colors.black),

          Icon(Icons.translate, size: 20, color: Colors.black),
          Icon(Icons.list, size: 20, color: Colors.black),
          Icon(Icons.account_balance, size: 20,
              color: Colors.black),
//            Icon(Icons.camera, size: 20, color: Colors.black),
        ],
        index: 2,
        animationDuration: Duration(milliseconds: 400),
        onTap: (index) {
          debugPrint("Curent index is $index");
        }
    )

推荐阅读