flutter - 仅当您在颤动的导航堆栈中的最后一个基本窗口中时,如何防止android后退按钮关闭应用程序?
问题描述
我正在尝试使用 CupertinoTabBar 构建一个自定义底部导航系统,并且它工作得很好,但我遇到了 Android 上的 backbitten 问题。
当我打开窗口时,后退按钮会正确关闭我的屏幕(导航到上一个屏幕)但是当我在最后一个屏幕时,如果您按下后退,整个应用程序将关闭。
我想尽量防止这种情况
有没有一种好方法可以让我检测到我何时在我的基本窗口并防止在该WillPopScope
区域发生这种情况?
这是我的底部导航条码
class MarkBottomNav2 extends StatefulWidget {
@override
State<MarkBottomNav2> createState() => _MarkBottomNavState2();
}
class _MarkBottomNavState2 extends State<MarkBottomNav2> {
final GlobalKey<NavigatorState> firstTabNavKey = GlobalKey<NavigatorState>();
final GlobalKey<NavigatorState> secondTabNavKey = GlobalKey<NavigatorState>();
final GlobalKey<NavigatorState> thirdTabNavKey = GlobalKey<NavigatorState>();
final GlobalKey<NavigatorState> fourthTabNavKey = GlobalKey<NavigatorState>();
final GlobalKey<NavigatorState> fifthTabNavKey = GlobalKey<NavigatorState>();
late CupertinoTabController tabController;
int index = 0;
@override
void initState() {
// TODO: implement initState
super.initState();
tabController = CupertinoTabController(initialIndex: 0);
}
@override
Widget build(BuildContext context) {
//making a list of the keys
final listOfKeys = [
firstTabNavKey,
secondTabNavKey,
thirdTabNavKey,
fourthTabNavKey,
fifthTabNavKey,
];
List homeScreenList = [
NewsArea(),
Area2(),
Area3(),
Area4(),
Area5()
];
return WillPopScope(
onWillPop: () async {
return !await listOfKeys[tabController.index].currentState!.maybePop();
},
child: CupertinoTabScaffold(
controller: tabController, //set tabController here
tabBar: CupertinoTabBar(
items: [
///this is where we are setting aur bottom ICONS
BottomNavigationBarItem(
icon: Icon(FontAwesome5.newspaper),
label: 'News',
),
BottomNavigationBarItem(icon: Icon(Icons.format_list_bulleted), label: 'Area 2'),
BottomNavigationBarItem(icon: Icon(Icons.mail_outline_outlined), label: 'Area 3'),
BottomNavigationBarItem(icon: Icon(Icons.alternate_email), label: 'Area 4'),
BottomNavigationBarItem(icon: Icon(Icons.more_horiz), label: 'Area 5'),
],
activeColor: Color.fromRGBO(26, 70, 128, 1),
inactiveColor: Color.fromRGBO(26, 70, 128, 0.3),
iconSize:20,
// currentIndex: pageIndex,
),
tabBuilder: (
context,
index,
) {
return CupertinoTabView(
navigatorKey: listOfKeys[
index], //set navigatorKey here which was initialized before
builder: (context) {
return homeScreenList[index];
},
);
},
),
);
}
}
解决方案
您可以尝试使用下面的代码并使用从页面退出的第一页
DateTime currentBackPressTime;
@override
Widget build(BuildContext context) {
return Scaffold(
...
body: WillPopScope(child: getBody(), onWillPop: onWillPop),
);
}
Future<bool> onWillPop() {
DateTime now = DateTime.now();
if (currentBackPressTime == null ||
now.difference(currentBackPressTime) > Duration(seconds: 2)) {
currentBackPressTime = now;
Fluttertoast.showToast(msg: exit_warning);
return Future.value(false);
}
return Future.value(true);
}
推荐阅读
- ide - 在 CS50 IDE 中以协作模式显示用户名
- laravel - 如何在 Laravel 中同时使用 whereNull 和 orwhereBetween?
- python - 从二维列表返回具有重复键(两级键字典)的数据列表的最快和优雅的方法
- node.js - 无法安装凉亭。在安装过程中出现 NPM 错误
- mysql - MySQL SUM 不适用于相同的数值
- javascript - Webpack多个条目输出1个js库文件
- php - 如何在关系数据库中为少量固定数量的选项保存多对多数据?
- android - 滚动父视图在 Android 中不起作用
- c++ - C++ 17 STL std::string_view 是否符合 RAII 设计理念?
- python - numpy 遵循没有循环的自索引列表