【Flutter】AutomaticKeepAliveClientMixin页面保持

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

1、添加AutomaticKeepAliveClientMixin,

class _LCNewsPageState extends State 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()),
    );
  }

你可能感兴趣的:(【Flutter】AutomaticKeepAliveClientMixin页面保持)