Flutter 如何返回到颤振中的上一页
我是个新手。当我在应用程序中按下设备后退按钮时。它将直接退出应用程序。如何像以前的图片一样转到上一页。如果我在选项卡3,当我按下设备后退按钮时,它将转到选项卡2。如果我在标签1按下后退按钮,它将退出应用程序。有人能帮我吗?提前感谢使用Flutter 如何返回到颤振中的上一页,flutter,Flutter,我是个新手。当我在应用程序中按下设备后退按钮时。它将直接退出应用程序。如何像以前的图片一样转到上一页。如果我在选项卡3,当我按下设备后退按钮时,它将转到选项卡2。如果我在标签1按下后退按钮,它将退出应用程序。有人能帮我吗?提前感谢使用WillPopScope处理返回导航 class _TabsControllerState extends State<TabsController> { final List<Widget> pages = [ TabOne(
WillPopScope
处理返回导航
class _TabsControllerState extends State<TabsController> {
final List<Widget> pages = [
TabOne(
key: PageStorageKey('page1'),
),
TabTwo(
key: PageStorageKey('page2'),
),
TabThree(
key: PageStorageKey('page3'),
),
];
final PageStorageBucket bucket = PageStorageBucket();
var _selectedIndex = 0;
Widget _bottomNavigationBar(var selectedIndex) => BottomNavigationBar(
onTap: (var index) => setState(() => _selectedIndex = index),
currentIndex: selectedIndex,
type: BottomNavigationBarType.fixed,
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(icon: Icon(Icons.apps), title: Text('Tab 1')),
BottomNavigationBarItem(icon: Icon(Icons.apps), title: Text('Tab 2')),
BottomNavigationBarItem(icon: Icon(Icons.apps), title: Text('Tab 3')),
],
);
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: ()async{
if(_selectedIndex == 0)
return true;
else setState(() {
_selectedIndex -= 1;
});
return false;
},
child: Scaffold(
bottomNavigationBar: _bottomNavigationBar(_selectedIndex),
body: PageStorage(
child: pages[_selectedIndex],
bucket: bucket,
),
));
}
}
class\u选项卡ControllerState扩展状态{
最终列表页=[
塔伯恩(
key:PageStorageKey('page1'),
),
塔布二(
key:PageStorageKey('page2'),
),
塔布三(
key:PageStorageKey('page3'),
),
];
final PageStorageBucket bucket=PageStorageBucket();
var _selectedIndex=0;
小部件\u底部导航栏(变量selectedIndex)=>底部导航栏(
onTap:(var索引)=>setState(()=>_selectedIndex=index),
currentIndex:selectedIndex,
类型:BottomNavigationBarType.fixed,
项目:常数[
BottomNavigationBarItem(图标:图标(Icons.apps),标题:文本('tab1')),
BottomNavigationBarItem(图标:图标(Icons.apps),标题:文本('tab2')),
BottomNavigationBarItem(图标:图标(Icons.apps),标题:文本('Tab 3')),
],
);
@凌驾
小部件构建(构建上下文){
返回式示波器(
onWillPop:()异步{
如果(_selectedIndex==0)
返回true;
else设置状态(){
_选择的索引-=1;
});
返回false;
},
孩子:脚手架(
bottomNavigationBar:_bottomNavigationBar(_selectedIndex),
正文:页面存储(
子:页面[\u selectedIndex],
桶:桶,
),
));
}
}
使用WillPopScope
处理后台导航
class _TabsControllerState extends State<TabsController> {
final List<Widget> pages = [
TabOne(
key: PageStorageKey('page1'),
),
TabTwo(
key: PageStorageKey('page2'),
),
TabThree(
key: PageStorageKey('page3'),
),
];
final PageStorageBucket bucket = PageStorageBucket();
var _selectedIndex = 0;
Widget _bottomNavigationBar(var selectedIndex) => BottomNavigationBar(
onTap: (var index) => setState(() => _selectedIndex = index),
currentIndex: selectedIndex,
type: BottomNavigationBarType.fixed,
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(icon: Icon(Icons.apps), title: Text('Tab 1')),
BottomNavigationBarItem(icon: Icon(Icons.apps), title: Text('Tab 2')),
BottomNavigationBarItem(icon: Icon(Icons.apps), title: Text('Tab 3')),
],
);
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: ()async{
if(_selectedIndex == 0)
return true;
else setState(() {
_selectedIndex -= 1;
});
return false;
},
child: Scaffold(
bottomNavigationBar: _bottomNavigationBar(_selectedIndex),
body: PageStorage(
child: pages[_selectedIndex],
bucket: bucket,
),
));
}
}
class\u选项卡ControllerState扩展状态{
最终列表页=[
塔伯恩(
key:PageStorageKey('page1'),
),
塔布二(
key:PageStorageKey('page2'),
),
塔布三(
key:PageStorageKey('page3'),
),
];
final PageStorageBucket bucket=PageStorageBucket();
var _selectedIndex=0;
小部件\u底部导航栏(变量selectedIndex)=>底部导航栏(
onTap:(var索引)=>setState(()=>_selectedIndex=index),
currentIndex:selectedIndex,
类型:BottomNavigationBarType.fixed,
项目:常数[
BottomNavigationBarItem(图标:图标(Icons.apps),标题:文本('tab1')),
BottomNavigationBarItem(图标:图标(Icons.apps),标题:文本('tab2')),
BottomNavigationBarItem(图标:图标(Icons.apps),标题:文本('Tab 3')),
],
);
@凌驾
小部件构建(构建上下文){
返回式示波器(
onWillPop:()异步{
如果(_selectedIndex==0)
返回true;
else设置状态(){
_选择的索引-=1;
});
返回false;
},
孩子:脚手架(
bottomNavigationBar:_bottomNavigationBar(_selectedIndex),
正文:页面存储(
子:页面[\u selectedIndex],
桶:桶,
),
));
}
}