【问题标题】:Flutter: Change Bottom Navigation Bar Items on real timeFlutter:实时更改底部导航栏项目
【发布时间】:2021-02-23 12:00:30
【问题描述】:

我正在尝试查看如何在 DefaultTabController 中实时更改 BottomNavigationBar 中的项目,但无济于事。

基本上这些是场景:

  1. 非管理员标签栏:[屏幕 A 和 B]
  2. 管理标签栏:[屏幕 C、D 和 B]

在屏幕 B 中,有一个按钮可以从管理员视图更改为非管理员视图,反之亦然。

提前谢谢你。

【问题讨论】:

    标签: flutter


    【解决方案1】:

    试试这个,它有效。但是,我建议使用 Provider 而不是将 WidgetState 作为参数传递给任何函数或任何 Widget。

    List<Widget> nonAdminWidgets(_BottomNavScaffoldState parent) {
      return <Widget>[
        ProfilePage(parent),
        ClothesPage(),
        ColorsPage(),
      ];
    }
    
    List<Widget> adminWidgets(_BottomNavScaffoldState parent) {
      return <Widget>[
        ProfilePage(parent),
        IdeasPage(),
      ];
    }
    
    int _currentIndex = 0;
    
    bool isAdmin = true;
    
    List<dynamic> nonAdminNavBars = [
      BottomNavigationBarItem(
        title: Text('Profile'),
        icon: Icon(Icons.face_rounded),
      ),
      BottomNavigationBarItem(
        title: Text('Clothes'),
        icon: Icon(Icons.design_services_rounded),
      ),
      BottomNavigationBarItem(
        title: Text('Colors'),
        icon: Icon(Icons.colorize_rounded),
      ),
    ];
    
    List<dynamic> adminNavBars = [
      BottomNavigationBarItem(
        title: Text('Profile'),
        icon: Icon(Icons.face_rounded),
      ),
      BottomNavigationBarItem(
        title: Text('Ideas'),
        icon: Icon(Icons.lightbulb_outline_rounded),
      ),
    ];
    
    class BottomNavScaffold extends StatefulWidget {
      @override
      _BottomNavScaffoldState createState() => _BottomNavScaffoldState();
    }
    
    class _BottomNavScaffoldState extends State<BottomNavScaffold> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          backgroundColor: Colors.white,
          body: isAdmin ? adminWidgets(this)[_currentIndex] : nonAdminWidgets(this)[_currentIndex],
          bottomNavigationBar: BottomNavigationBar(
            type: BottomNavigationBarType.fixed,
            currentIndex: _currentIndex,
            backgroundColor: Colors.orangeAccent,
            selectedItemColor: Colors.white,
            onTap: (value) {
              _currentIndex = value;
              setState(() {});
            },
            items: isAdmin ? adminNavBars : nonAdminNavBars,
          ),
        );
      }
    }
    
    class ClothesPage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Card(
          color: Colors.white,
          child: Padding(
            padding: EdgeInsets.all(10),
            child: Text(
              "Clothes",
            ),
          ),
        );
      }
    }
    
    class ColorsPage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Card(
          color: Colors.white,
          child: Padding(
            padding: EdgeInsets.all(10),
            child: Text(
              "Colors",
            ),
          ),
        );
      }
    }
    
    class IdeasPage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Card(
          color: Colors.white,
          child: Padding(
            padding: EdgeInsets.all(10),
            child: Text(
              "Ideas",
            ),
          ),
        );
      }
    }
    
    class ProfilePage extends StatelessWidget {
      _BottomNavScaffoldState parent;
    
      ProfilePage(this.parent);
    
      @override
      Widget build(BuildContext context) {
        return RaisedButton(
          onPressed: () {
            isAdmin = !isAdmin;
            _currentIndex = 0;
            parent.setState(() {});
          },
          child: Text("Change"),
        );
      }
    }
    

    【讨论】:

    • 谢谢你,Preet,我会看看并尽快回复。
    • 抱歉,我不能投票,因为我是新手,没有足够的声誉。
    • 您不必为它投票。您需要将其标记为已接受。乐意效劳。 :)
    猜你喜欢
    • 2020-02-09
    • 2020-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-07
    • 1970-01-01
    相关资源
    最近更新 更多