flutter在tab切换的时候,怎么让他不执行initState

文章来源:https://blog.csdn.net/qq_33559682/article/details/80694987,感谢qq_33559682

第一次加载2个Tab页面:LoadImgByNetPage,LoadImgByLocPage 
Log所示: 
这里写图片描述 
反复切换2个Tab页面:LoadImgByNetPage,LoadImgByLocPage 
Log所示: 
flutter在tab切换的时候,怎么让他不执行initState_第1张图片
解决方案

在LoadImgByNetPage,LoadImgByLocPage中添加AutomaticKeepAliveClientMixin,并设置为true,这样就能一直保持当前不被initState了。

class _LoadImgByLocAppPageState extends State with AutomaticKeepAliveClientMixin{

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    print('_LoadImgByLocAppPageState initState');
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Scaffold(
      body: new ListView(
        children: [
          new Image.asset('images/ic_assignment_ind_36pt.png'),
          new Divider(),
          new Image(
            image: new AssetImage('images/ic_assignment_ind_36pt.png'),
          ),
          new Divider(),
        ],
      ),
    );
  }

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

你可能感兴趣的:(flutter在tab切换的时候,怎么让他不执行initState)