【发布时间】:2018-11-25 16:21:10
【问题描述】:
我在 Flutter 中创建了自己的简单底部导航栏实现。当按下选项卡时,Flutter 当前正在重新创建小部件(initState() 每次都会被调用),这是不可取的。
我希望小部件持久保存在内存中,因此如果它们已经创建,它们会直接弹出。
主小部件
class _MainRootScreenState extends State<MainRootScreen> {
int _selectedIndex = 0;
List<Widget> _screens;
@override
void initState() {
// load pages
_screens = [
PageOne(),
PageTwo(),
PageThree()
];
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: _screens[_selectedIndex],
bottomNavigationBar: _buildBottomTabBar(context)
);
}
}
所以当_selectedIndex 更新时,所选页面将重新创建。
我尝试在页面上使用AutomaticKeepAliveClientMixin,但没有成功。
【问题讨论】:
-
AutomaticKeepAliveClientMixin仅适用于Scrollable。你这里没有 -
您找到解决方案了吗?
标签: dart flutter flutter-layout