根据应用状态快速更新BottomNavigationBar项目

问题描述

我是一个扑朔迷离的新手,目前正在查看其BottomNavigationBar小部件。

我的概念应用程序要求底部导航栏根据应用程序状态包含3或5个项目。另外,底部标签中的最后一项需要根据应用状态呈现2个单独的小部件。

我正在使用提供程序包来维护应用程序状态。我在下面编写了这段代码,根据应用程序状态(根据下面代码中的siteSelected变量进行解析)可解决3或5个项目的呈现。

现在,当用户根据应用程序状态(loggedIn)点击“帐户”标签时,我还需要打开LoginScreen或Profile屏幕。我想不出一种简单的方法来实现这一目标。看来我们只能在构建窗口小部件内引用应用程序状态变量,因此我无法通过getAccountWidget()函数中的loginIn状态通过if else条件来实现此目的。

我该如何克服?请帮忙。我根据条件决定3个或5个图标的方法还是一种有效的方法,还是有一种更有效的方法呢?

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  int _selectedindex = 0;
  static const TextStyle optionStyle =
      TextStyle(fontSize: 30,fontWeight: FontWeight.bold);
  List<Widget> _widgetoptions = <Widget>[
    Text(
      'Index 0: Home',style: optionStyle,),Text(
      'Index 1: Scan',getAccountWidget(),];
  List<Widget> _widgetoptionsSiteSelected = <Widget>[
    Text(
      'Index 0: Home',Text(
      'Index 1: Menu',Text(
      'Index 2: Scan',Text(
      'Index 3: Basket',];

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

  @override
  Widget build(BuildContext context) {
    final appState = Provider.of<BasicState>(context);
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,title: Text(
          'Home',style: TextStyle(color: Color.fromrGBO(255,184,14,1)),body: Center(
        child: getChildWidget(appState.siteSelected),bottomNavigationBar:
          getBottomNavigationBar(appState.isLogged,appState.siteSelected),);
  }

  BottomNavigationBar getBottomNavigationBar(bool loggedIn,bool siteSelected) {
    if (!siteSelected) {
      return (BottomNavigationBar(
        type: BottomNavigationBarType.fixed,items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.home),title: Text('Home'),BottomNavigationBarItem(
            icon: Icon(Icons.aspect_ratio),title: Text('Scan'),BottomNavigationBarItem(
            icon: Icon(Icons.person),title: Text('Account'),)
        ],currentIndex: _selectedindex,selectedItemColor: Color.fromrGBO(255,1),onTap: _onItemTapped,));
    } else {
      return (BottomNavigationBar(
        type: BottomNavigationBarType.fixed,BottomNavigationBarItem(
            icon: Icon(Icons.import_contacts),title: Text('Menu'),BottomNavigationBarItem(
            icon: Icon(Icons.alarm_on),title: Text('Basket'),));
    }
  }

  Widget getChildWidget(bool siteSelected) {
    if (siteSelected) {
      return _widgetoptionsSiteSelected.elementAt(_selectedindex);
    } else {
      return _widgetoptions.elementAt(_selectedindex);
    }
  }

  List<Widget> getWidgetList() {
    return <Widget>[
      Text(
        'Index 0: Home',Text(
        'Index 1: Scan',Text(
        'Index 2: Account',];
  }
}

Widget getAccountWidget() {
  return Text(
    'Index 3: Account',);
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...