在Flutter中,我有一个底部导航栏,里面有4个不同的底部导航栏项目。在第四个菜单项中,我有一个复选框小部件。在浏览导航栏中的项目时,我希望保持选中或未选中项目的状态。例如,如果我取消选中这个小部件,我希望它在浏览其他导航栏项目时保持“未选中”状态。我该怎么做呢?我尝试使用checkbox的key参数,但失败了。
发布于 2020-04-21 19:08:18
在浏览items.Extend时,您应该使用AutomaticKeepAliveClientMixin保持状态不变,使用它创建一个覆盖方法wannaKeepAlive并将其设置为true.Here就是一个使用AutomaticKeepAliveClientMixin的小部件的示例。
class Example extends StatefulWidget {
@override
_ExampleState createState() => _ExampleState();
}
class _ExampleState extends State<Example> with AutomaticKeepAliveClientMixin {
@override
Widget build(BuildContext context) {
return Container(
);
}
@override
// TODO: implement wantKeepAlive
bool get wantKeepAlive => true;
}发布于 2020-04-21 19:28:27
欢迎使用状态管理!Official guideline for state management from Flutter team。
我建议所有内容都从introduction to state management开始阅读。
https://stackoverflow.com/questions/61339872
复制相似问题