【问题标题】:Flutter Bottom Navigation Bar with conditions带有条件的 Flutter 底部导航栏
【发布时间】:2022-02-10 06:57:33
【问题描述】:

我想要一个底部导航栏,并且在小部件列表中我想要不同的屏幕。但要显示工作屏幕与否,我有 var isWorking = prefs.getString('isWorking'),我希望如果 isWorking == null 返回 WorkingScreen,否则 StopWorkingScreen。通过我的研究,我认为我必须使用 Future Builder,但如果有人可以演示如何解决这个问题,我将不胜感激。顺便说一句,我试图为 WorkingScreen() 和 StopWorkingScreen() 设置相同的底部导航栏,所以如果用户 isWorking != null 它将显示 StopWorkingScreen(),底部导航栏也在底部,与WorkingScreen(),所以底部导航栏的屏幕是 WorkingScreen()、StopWorkingScreen()、ReportScreen()、ProfileScreen()、EditProfileScreen。但是当用户按下主页底部的导航按钮时,它会检查用户是否在工作并显示屏幕。但是 EditProfileScreen() 不会有自己的底部导航栏,因为它是 ProfileScreen 的一部分,所以它只是与 Profile Screen 具有相同的底部导航栏。所以 (WorkingScreen(), StopWorkingScreen()) 相同的底部导航栏,(ReportsScreen()) 导航第二个导航 var,第三个是 (ProfileScreen() 和 EditProfileScreen()) 具有相同的底部导航栏

class MyStatefulWidget extends StatefulWidget {
  const MyStatefulWidget({Key? key}) : super(key: key);

  @override
  State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {

  Future<Widget> isUserWorking () async{
    final prefs = await SharedPreferences.getInstance();
    var isWorking = prefs.getString('isWorking');
    if(isWorking == null){
      return const WorkingScreen();
    } else{
      return const StopWorkingScreen();
    }
  },
  int _selectedIndex = 0;
  static const TextStyle optionStyle =
  TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
  static List<Widget> _widgetOptions = <Widget>[
    isUserWorking(), // this is what im trying to do but getting error
    ReportsScreen(),
    ProfileScreen(),
  ];

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('BottomNavigationBar Sample'),
      ),
      body: Center(
        child: _widgetOptions.elementAt(_selectedIndex),
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            label: 'Home',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.business),
            label: 'Business',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.school),
            label: 'School',
          ),
        ],
        currentIndex: _selectedIndex,
        selectedItemColor: Colors.amber[800],
        onTap: _onItemTapped,
      ),
    );
  }
}

【问题讨论】:

    标签: flutter dart flutter-layout flutter-dependencies


    【解决方案1】:

    您可以像这样将您的未来插入FutureBuilder

    import 'package:flutter/material.dart';
    import 'package:shared_preferences/shared_preferences.dart';
    
    class IsUserWorking extends StatelessWidget {
      const IsUserWorking({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return FutureBuilder<SharedPreferences>(
          future: SharedPreferences.getInstance(),
          builder: (context, snapshot) {
            final isWorking = snapshot.data?.getString('isWorking');
            if(isWorking == null){
              return const WorkingScreen();
            } else{
            return const StopWorkingScreen();
            }
          },
        );
      }
    }
    

    一个简单的谷歌搜索也给了我这个,How to change the bottom navigation bar items based on a condition in Flutter

    【讨论】:

      猜你喜欢
      • 2020-07-30
      • 2023-02-25
      • 1970-01-01
      • 2021-10-02
      • 2020-04-15
      • 1970-01-01
      • 1970-01-01
      • 2020-02-07
      相关资源
      最近更新 更多