简体   繁体   中英

Set State not working in bottom navigation bar item even after using setState for 4 elements

When I use 4 items in bottom navigation bar, only menu is shown as selected item (index 0) even though _currentIndex is changing as usual. What to do now, is it because of constructor of used in category screen?

 @override
  Widget build(BuildContext context) {
    User user = FirebaseAuth.instance.currentUser;

    int _currentIndex = 0;

    void onTabTapped(int index) {
      setState(() {
        _currentIndex = index;
      });
    }


    final List<Widget> _children = [
      CategoryScreen(email: user.email),
      TabBarOrder(),
      PayInScreen(),
      ProfileScreen(),
    ];

    return Scaffold(
      body: _children[_currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        showSelectedLabels: true,
        onTap: onTabTapped,
        currentIndex: _currentIndex,
        type: BottomNavigationBarType.fixed,
        iconSize: 0,
        items: [
          BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Menu'),
          BottomNavigationBarItem(icon: Icon(Icons.mail), label: 'Order'),
          BottomNavigationBarItem(
              icon: Icon(Icons.play_circle_filled), label: 'Pay-in'),
          BottomNavigationBarItem(
              icon: Icon(Icons.play_circle_filled), label: 'Profile'),
        ],
      ),
    );
 }

Please help

int _currentIndex = 0;

    void onTabTapped(int index) {
      setState(() {
        _currentIndex = index;
      });
    }

@override
  Widget build(BuildContext context) {
    User user = FirebaseAuth.instance.currentUser;

    

    final List<Widget> _children = [
      CategoryScreen(email: user.email),
      TabBarOrder(),
      PayInScreen(),
      ProfileScreen(),
    ];

    return Scaffold(
      body: _children.elementAt(_currentIndex),
      bottomNavigationBar: BottomNavigationBar(
        showSelectedLabels: true,
        onTap: onTabTapped,
        currentIndex: _currentIndex,
        type: BottomNavigationBarType.fixed,
        iconSize: 0,
        items: [
          BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Menu'),
          BottomNavigationBarItem(icon: Icon(Icons.mail), label: 'Order'),
          BottomNavigationBarItem(
              icon: Icon(Icons.play_circle_filled), label: 'Pay-in'),
          BottomNavigationBarItem(
              icon: Icon(Icons.play_circle_filled), label: 'Profile'),
        ],
      ),
    );
 }

same problem:/ don't know why:(

int screenIndex = 0;
  final List<Widget> screens = <Widget>[
    InfoTab(),
    FormTab(),
    GroupTab(),
    WitnessTab(),
    MoreTab(),
  ];
  void _selectTab(int index) {
    setState(() {
      screenIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    final routeArgs =
        ModalRoute.of(context)?.settings.arguments as Map<String, dynamic>;
    final projectId = routeArgs['id'];
    final projectTitle = routeArgs['title'];

    _setProjectData(projectId, projectTitle);

    return WillPopScope(
      onWillPop: () async {
        // prevent going back when swipe left
        if (Navigator.of(context).userGestureInProgress)
          return false;
        else
          return true;
      },
      child: Scaffold(
        appBar: AppBar(
          title: const Text(
            'ITR Inspection Form',
            style: TextStyle(
              fontSize: 13,
              fontWeight: FontWeight.w600,
            ),
          ),
          centerTitle: true,
          actions: <Widget>[
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: GestureDetector(
                onTap: () {
                  Navigator.of(context).pushNamed(MyAccountScreen.routeName);
                },
                child: CircleAvatar(
                  radius: 30.0,
                  child: ClipOval(
                    child: Image.network('https://picsum.photos/250?image=9'),
                  ),
                ),
              ),
            )
          ],
        ),
        body: screens[screenIndex],
        bottomNavigationBar: BottomNavigationBar(
          onTap: _selectTab,
          unselectedItemColor: Color(0xFF9E9E9E),
          selectedItemColor: Colors.black,
          type: BottomNavigationBarType.fixed,
          currentIndex: screenIndex,
          items: [
            BottomNavigationBarItem(
                icon: Icon(Icons.info_outline_rounded), label: 'Info'),
            BottomNavigationBarItem(icon: Icon(Icons.notes), label: 'Form'),
            BottomNavigationBarItem(icon: Icon(Icons.post_add), label: 'Group'),
            BottomNavigationBarItem(
                icon: Icon(Icons.playlist_add_check_sharp), label: 'Witness'),
            BottomNavigationBarItem(
                icon: Icon(Icons.more_horiz_outlined), label: 'More'),
          ],
        ),
      ),
    );
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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