状态保持一般都会说要加 SingleTickerProviderStateMixin 或AutomaticKeepAliveClientMixin
class _HomePageState extends State<HomePage>
with SingleTickerProviderStateMixin {
@override
Widget build(BuildContext context) {
super.build(context);
return Scaffold(
appBar: AppBar(
title: Text('云台法律咨询'),
),
body: HomeScreen(),
);
}
//必须加下面的状态保持
@override
bool get wantKeepAlive => true;
}
但是以上代码要实现状态保持是不够的,还需要在tabbar里面添加如下代码
class _CustormTabbarPageState extends State<CustormTabbarPage>
with SingleTickerProviderStateMixin {
final _BottomNavigationColor = Colors.black54;
final _SelectedColor = Colors.blueAccent;
int _currentIndex = 0;
List<Widget> pageList = List();
final PageController _controller = PageController(initialPage: 0);
@override
void initState() {
// pageList
pageList..add(HomePage());
pageList..add(MinePage());
super.initState();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
// super.build(context);
return Scaffold(
// body: PageView(
// controller: _controller,
// children: [HomePage(), MinePage()],
// ),
body: IndexedStack(
index: _currentIndex,
children: pageList,
),
// body: pageList[_currentIndex],
bottomNavigationBar: BottomNavigationBar(
items: [
BottomNavigationBarItem(
icon: Icon(
Icons.home,
),
title: Text('首页'),
),
BottomNavigationBarItem(
icon: Icon(
Icons.people,
),
title: Text('我的'),
),
],
currentIndex: _currentIndex,
fixedColor: _SelectedColor,
onTap: (index) {
// _controller.jumpToPage(index);
setState(() {
_currentIndex = index;
});
}),
);
}
}
必须使用IndexedStack 才行