【Flutter】AutomaticKeepAliveClientMixin页面保持

在切换页面时,经常会刷新页面,为了避免initState方法重复调用

1、添加AutomaticKeepAliveClientMixin,

class _LCNewsPageState extends State<LCNewsPage> with AutomaticKeepAliveClientMixin

2、并实现对应的方法bool get wantKeepAlive => true;,

  @override
  bool get wantKeepAlive => true;

3、同时build方法实现父方法 super.build(context);

  @override
  Widget build(BuildContext context) {
    super.build(context);
    
    return Container(
      child: Scaffold(appBar: AppBar(), body: body()),
    );
  }

猜你喜欢

转载自blog.csdn.net/tianzhilan0/article/details/107634318