app首页切换时,保持页面状态,使用IndexedStack实现
IndexedStack(
index: _selectedIndex,
children: pages,
)
Android在首页时通常返回会有“再按一次退出”以及在其他tab时返回首页tab的要求
body: WillPopScope(
child: IndexedStack(
index: _selectedIndex,
children: pages,
),
onWillPop: () async {
print('_selectedIndex:${_selectedIndex}');
if(_selectedIndex != 0){
_onItemTapped(0);
return false;
}
if (_lastPressedAt == null ||
DateTime.now().difference(_lastPressedAt) > Duration(seconds: 1)) {
//两次点击间隔超过1秒则重新计时
_lastPressedAt = DateTime.now();
Fluttertoast.showToast(msg: '再按一次退出');
return false;
}
// 退出app
await SystemChannels.platform.invokeMethod('SystemNavigator.pop');
return true;
},
),
);