Flutter:每次访问页面时如何在bottomnavigationbar上重新加载页面

如何解决Flutter:每次访问页面时如何在bottomnavigationbar上重新加载页面

我有一个在3页之间导航的底部导航栏。它持续存在并保持每个页面的状态。但是我希望每次访问时都能重新加载页面。下面是我的代码


class navigationPurchase extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => AppState();
}

class AppState extends State<navigationPurchase> {
  // this is static property so other widget throughout the app
  // can access it simply by AppState.currentTab
  static int currentTab = 0;

  // list tabs here
  final List<TabItem> tabs = [
    TabItem(
      tabName: "Home",icon: Icons.home,page: HomepagePurchase(),),TabItem(
      tabName: "Announcement",icon: Icons.announcement,page: MyAnnouncementAppPurchase(),TabItem(
      tabName: "Notification",icon: Icons.notifications,page: MyNotificationAppPurchase(),];

  AppState() {
    // indexing is necessary for proper funcationality
    // of determining which tab is active
    tabs.asMap().forEach((index,details) {
      details.setIndex(index);
    });
  }

  // sets current tab index
  // and update state
  void _selectTab(int index) {
    if (index == currentTab) {
      // pop to first route
      // if the user taps on the active tab
      tabs[index].key.currentState.popUntil((route) => route.isFirst);
    } else {
      // update the state
      // in order to repaint
      setState(() => currentTab = index);
    }
  }

  @override
  Widget build(BuildContext context) {
    // WillPopScope handle android back btn
    return WillPopScope(
      onWillPop: () async {
        final isFirstRouteInCurrentTab =
        !await tabs[currentTab].key.currentState.maybePop();
        if (isFirstRouteInCurrentTab) {
          // if not on the 'main' tab
          if (currentTab != 0) {
            // select 'main' tab
            _selectTab(0);
            // back button handled by app
            return false;
          }
        }
        // let system handle back button if we're on the first route
        return isFirstRouteInCurrentTab;
      },// this is the base scaffold
      // don't put appbar in here otherwise you might end up
      // with multiple appbars on one screen
      // eventually breaking the app
      child: Scaffold(
        // indexed stack shows only one child
        body: IndexedStack(
          index: currentTab,children: tabs.map((e) => e.page).toList(),// Bottom navigation
        bottomNavigationBar: BottomNavigation(
          onSelectTab: _selectTab,tabs: tabs,);
  }
}

class TabItem {
  // you can customize what kind of information is needed
  // for each tab
  final String tabName;
  final IconData icon;
  final GlobalKey<NavigatorState> key = GlobalKey<NavigatorState>();
  int _index = 0;
  Widget _page;

  TabItem({
    @required this.tabName,@required this.icon,@required Widget page,}) {
    _page = page;
  }

  // I was getting a weird warning when using getters and setters for _index
  // so I converted them to functions

  // used to set the index of this tab
  // which will be used in identifying if this tab is active
  void setIndex(int i) {
    _index = i;
  }

  int getIndex() => _index;

// adds a wrapper around the page widgets for visibility
// visibility widget removes unnecessary problems
// like interactivity and animations when the page is inactive
  Widget get page {
    return Visibility(
      // only paint this page when currentTab is active
      visible: _index == AppState.currentTab,// important to preserve state while switching between tabs
      maintainState: true,child: Navigator(
        // key tracks state changes
        key: key,onGenerateRoute: (routeSettings) {
          return MaterialPageRoute(
            builder: (_) => _page,);
        },);
  }
}

class BottomNavigation extends StatelessWidget {
  BottomNavigation({
    this.onSelectTab,this.tabs,});

  final ValueChanged<int> onSelectTab;
  final List<TabItem> tabs;

  @override
  Widget build(BuildContext context) {
    return BottomNavigationBar(
      type: BottomNavigationBarType.fixed,items: tabs
          .map(
            (e) => _buildItem(
          index: e.getIndex(),icon: e.icon,tabName: e.tabName,)
          .toList(),onTap: (index) => onSelectTab(
        index,);
  }

  BottomNavigationBarItem _buildItem(
      {int index,IconData icon,String tabName}) {
    return BottomNavigationBarItem(
      icon: Icon(
        icon,color: _tabColor(index: index),// ignore: deprecated_member_use
      title: Text(
        tabName,style: TextStyle(
          color: _tabColor(index: index),fontSize: 12,);
  }

  Color _tabColor({int index}) {
    return AppState.currentTab == index ? Colors.blue : Colors.grey;
  }
}


我不希望它再保持状态,但是每当我访问每个页面时,都应该重新加载数据以显示新数据。 Home,Announcement和Notification从数据库获取内容的新数据,但是使用上面的代码,它仅存储以前加载的数据,直到应用程序重新启动后才获取新数据。

解决方法

默认情况下,每次点击不同页面时,每个页面都会重新加载/重建。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 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元字符(。)和普通点?