【问题标题】:how to keep the nested navigator stack history when swiping to another stack in Flutter在 Flutter 中滑动到另一个堆栈时如何保留嵌套的导航器堆栈历史记录
【发布时间】:2020-10-18 04:40:53
【问题描述】:

我创建了一个TabBarView 和一个TabBar,每个选项卡都有自己的导航器堆栈 在第一个选项卡的导航器堆栈中导航并在某些时候滑动到另一个选项卡时 并滑回上一个选项卡,上一个选项卡导航器堆栈已重置,就像它再次构建一样

如何处理这种情况 我希望每个选项卡导航器堆栈在滑动到另一个选项卡时保留其历史记录

【问题讨论】:

    标签: flutter nested navigator


    【解决方案1】:

    我已经找到了解决方案,它只是用一个有状态的小部件包装嵌套的导航器,并使用有状态小部件的状态和 mixin AutomaticKeepAliveClientMixin 并实现(必须实现的方法) @覆盖 bool get wantKeepAlive => true;

    如下例

     class NotificationsNavigator extends StatefulWidget {
      @override
     _NotificationsNavigatorState createState() => _NotificationsNavigatorState();
    }
    
    class _NotificationsNavigatorState extends State<NotificationsNavigator> with 
    AutomaticKeepAliveClientMixin {
    
    @override 
    bool get wantKeepAlive => true;
    
      @override
      Widget build(BuildContext context) {
        return new Navigator(
          key: someKey,
          onGenerateRoute: notificationsOnGenerateRoutes,
          initialRoute: '/',
        );
      }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-02
      • 2019-10-08
      • 2021-09-22
      • 2020-07-27
      • 2018-03-17
      • 2013-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多