在颤动中按下按钮时底部导航栏不改变图标

时间:2021-03-11 15:55:01

标签: flutter mobile flutter-bottomnavigation

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

enter image description here

我的想法是更改所选屏幕/页面的图标按钮(汽车),在本例中为 Page 3。总而言之,每当我从底部导航栏中选择一个页面时,应该选择适当的图标按钮并将其突出显示为蓝色,如上图所示。这是 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 个相同页面/屏幕之一:

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(),
    );
  }
}

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

2 个答案:

答案 0 :(得分:1)

在每个页面上都有一个 bottomNavigationBar: BottomNavBar(),当您从页面导航到页面时会重置该 appBar: AppBar( title: const Text('BottomNavigationBar Sample'), ), body: Center( child: _widgetOptions.elementAt(_selectedIndex), ), bottomNavigationBar: BottomNavigationBar( 。所以 currentIndex 总是 0。

2 解决方案:

//when a user enter the room
db.collection('chat_rooms').doc('room_1').update({
    userCount: firebase.firestore.FieldValue.increment(1)
});
//when a user quits the room
db.collection('chat_rooms').doc('room_1').update({
    userCount: firebase.firestore.FieldValue.increment(-1)
});

答案 1 :(得分:1)

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