底部导航栏不更改按钮上的图标按颤动

Bottom Nav Bar Not Changing Icons On Button Press In Flutter

我想在某些特定的 screens/pages 中使用 BottomNavigationBar,但我不确定我这样做是否正确。我制作了一个名为 BottomNavBar 的小部件,并在我的 pages/screens 中实现了它。每当我单击 Page3 时,它都会导航到 Page3,但 selected 图标始终位于第一个图标按钮上,如下图所示。

我的想法是更改为 selected screen/page 的图标按钮(汽车),在本例中为 Page 3。总而言之,每当我 select 来自 BottomNavBar 的页面时,相应的图标按钮都应该 selected 并像上图一样突出显示为蓝色。这是 BottomNavBar:

的代码
import 'package:flutter/material.dart';
import 'screens/page_1.dart';
import 'screens/page_2.dart';
import 'screens/page_3.dart';
import 'screens/page_4.dart';
import 'screens/page_5.dart';


class BottomNavBar extends StatefulWidget {
  @override
  _BottomNavBarState createState() => _BottomNavBarState();
}

class _BottomNavBarState extends State<BottomNavBar> {
  int _selectedIndex = 0;
  @override
  Widget build(BuildContext context) {
    return BottomNavigationBar(
      backgroundColor: Colors.white,
      currentIndex: _selectedIndex, // this will be set when a new tab is tapped
      showUnselectedLabels: true,
      onTap: (int index) {
        setState(() {
          _selectedIndex = index;
        });
        switch (index) {
          case 0:
            Navigator.pushNamed(context, Page1.id);
            break;
          case 1:
            Navigator.pushNamed(context, Page2.id);
            break;
          case 2:
            Navigator.pushNamed(context, Page3.id);
            break;
          case 3:
            Navigator.pushNamed(context, Page4.id);
            break;
          case 4:
            Navigator.pushNamed(context, Page5.id);
            break;
        }
      },
      type: BottomNavigationBarType.fixed,
      items: [
        BottomNavigationBarItem(
          icon: Icon(Icons.dashboard),
          label: "Page 1",
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.work), 
          label: "Page 2",
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.directions_car),
          label: "Page 3",
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.supervised_user_circle),
          label: "Page 4",
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.account_circle),
          label: "Page 5",
        ),
      ],
    );
  }
}

和 5 个相同的其中之一 pages/screens:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'components/bottom_navigation_bar.dart';

class Page1 extends StatelessWidget {
  static const String id = 'page_1';
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        iconTheme: IconThemeData(
          color: Colors.grey[600],
        ),
      ),
      body: SafeArea(
          child: Container(
            child: Text('THIS IS PAGE 1'),
        ),
      ),
/// Here is where have I implemented the widget
      bottomNavigationBar: BottomNavBar(),
    );
  }
}

这是不是一个好方法?感谢任何帮助,提前致谢!

在每个页面上都有一个 bottomNavigationBar: BottomNavBar(),当您从一个页面导航到另一个页面时,它会被重置。所以currentIndex总是0.

2 个解决方案:

      appBar: AppBar(
        title: const Text('BottomNavigationBar Sample'),
      ),
      body: Center(
        child: _widgetOptions.elementAt(_selectedIndex),
      ),
      bottomNavigationBar: BottomNavigationBar(

我同意@BabC 的观点,该问题与在页面之间移动时重置多个导航栏有关。 这是有关如何正确使用底部导航栏的好指南 https://www.refactord.com/guides/flutter-bottom-navigation-simplified