美文网首页
Flutter 保持页面缓存 保持页面状态

Flutter 保持页面缓存 保持页面状态

作者: 吾等斩去红尘时 | 来源:发表于2022-03-31 14:28 被阅读0次

在默认情况下页面切换走时会被销毁,页面切换回来时会被重新创建,如果页面中有列表那么整个列表将会被重新创建,降低了用户体验,下面是解决这个问题的几种处理方式

方式一 AutomaticKeepAliveClientMixin

// 1、添加 with AutomaticKeepAliveClientMixin
class _PageState extends State<Page> with AutomaticKeepAliveClientMixin {

  @override
  Widget build(BuildContext context) {
    return Center(child: Text('页面'));
  }

  // 2、添加 bool get wantKeepAlive => true
  @override
  bool get wantKeepAlive => true; // 是否需要缓存
}

相关文章

网友评论

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

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