Flutter AutomaticKeepAliveClient

2020-12-16  本文已影响0人  Albert新荣

使用这个 AutomaticKeepAliveClientMixin 的时候为了保持控制器的状态但是有时候这个是无效的

正常的你在你的控制器里面这样设置

class _TaskState extends State<Task> with AutomaticKeepAliveClientMixin{

  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context);
}

}

我也是这样设置的但是毫无软用,我找了好久发现这样设置应该是对的
你可以去上面的一层控制器找找原因,tabbar 控制器里面
度娘之后找到原因

   body: vcs[context.watch<Counter>().value] //AutomaticKeepAliveClientMixin要配合IndexedStack使用才有效

请把你的tabbar的切换改成这样

   body: IndexedStack(
        index: context.watch<Counter>().value, //我这边使用了Provide管理了index 
        children: vcs,
      ), 
上一篇下一篇

猜你喜欢

热点阅读