美文网首页
Flutter页面状态的保持

Flutter页面状态的保持

作者: 小学生课代表 | 来源:发表于2020-05-13 21:42 被阅读0次

AutomaticKeepAliveClientMixin这个Mixin就是Flutter为了保持页面设置的。哪个页面需要保持页面状态,就在这个页面进行混入。

不过使用使用这个Mixin是有几个先决条件的:

  • 使用的页面必须是StatefulWidget,如果是StatelessWidget是没办法办法使用的。
  • 其实只有两个前置组件才能保持页面状态:PageView和IndexedStack。
  • 重写wantKeepAlive方法,如果不重写也是实现不了的
  1. Scaffold中的bottomNavigationBar,body如下:
 body: IndexedStack(
        index: currentIndex,//当前的下标
        children: tabBodies//子页面的Widget
      )
  1. 然后在要保持状态的子页面混入
class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive =>true;
}
  1. 验证:
@override
  void initState() {
    super.initState();
     print('111111111111111111111111111');
  }

只会输出一次'111111111111111111111111111'

相关文章

网友评论

      本文标题:Flutter页面状态的保持

      本文链接:https://www.haomeiwen.com/subject/ydxanhtx.html