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

如何解决根据应用状态快速更新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.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?