美文网首页
flutter中AutomaticKeepAliveClient

flutter中AutomaticKeepAliveClient

作者: 男人宫 | 来源:发表于2021-06-25 08:57 被阅读0次

在flutter中如果切换tabar,则initState方法会被反复重调,若想保持原有状态,切换页面时不再调用initState方法,则需使用AutomaticKeepAliveClientMixin.同Vue中的<keep-alive>使用原理一样

第一步:在State类中混入AutomaticKeepAliveClientMixin这个类

class _MilitaryPageState extends State<MilitaryPage> with AutomaticKeepAliveClientMixin{
........
}

第二步: 重写AutomaticKeepAliveClientMixin中的wantKeepAlive方法

 @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;

第三步:在build中调用父类的build方法

 Widget build(BuildContext context) {
    super.build(context);
    return Center(...)
}

相关文章

网友评论

      本文标题:flutter中AutomaticKeepAliveClient

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