首页 > 解决方案 > 仅当您在颤动的导航堆栈中的最后一个基本窗口中时,如何防止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];
              },
            );
          },
        ),
      );



  }


}

标签: flutterdart

解决方案


您可以尝试使用下面的代码并使用从页面退出的第一页

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);
  }

推荐阅读