CupertinoTabBar and PopScope not working in Flutter

42 Views Asked by At

I want to try show dialog when user exit app, in WillPopScope it was working, but with PopScope I get Navigator error. I checked print value, but didPop everytime return true. Flutter documentation doesnt help. I used Android Predictive Back tutorial. Can you help me? It is my codes:

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  static final navigatorKey = GlobalKey<NavigatorState>();

  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;

  late List pages;
  late List listOfKeys;

  @override
  void initState() {
    super.initState();

    tabController = CupertinoTabController(initialIndex: 0);

    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown,
    ]);

    pages = [
      HomePage(),
      CategoriesPage(),
      WishlistPage(),
      CartPage(),
      SettingsPage(),
    ];

    listOfKeys = [firstTabNavKey, secondTabNavKey, thirdTabNavKey, fourthTabNavKey, fifthTabNavKey];
  }

  @override
  Widget build(BuildContext context) {
    Brightness brightness = MediaQuery.of(context).platformBrightness;
    return GetMaterialApp(
      scaffoldMessengerKey: SnackbarGlobal.key,
      navigatorKey: navigatorKey,
      debugShowCheckedModeBanner: false,
      routes: Routes.generalRoutes,
      home: PopScope(
          canPop: false,
          onPopInvoked: (didPop) async {
            if(didPop) {
              print('ok');
              return;
            }
            return;
          },
          child: CupertinoTabScaffold(
                    controller: tabController,
                    tabBar: CupertinoTabBar(
                      onTap: (index) {
                        SnackbarGlobal.remove();
                        if (index == tabBarController.index.value) {
                          if (index == 0) {
                            firstTabNavKey.currentState?.popUntil((r) => r.isFirst);
                          } else if (index == 1) {
                            secondTabNavKey.currentState?.popUntil((r) => r.isFirst);
                          } else if (index == 2) {
                            thirdTabNavKey.currentState?.popUntil((r) => r.isFirst);
                          } else if (index == 3) {
                            fourthTabNavKey.currentState?.popUntil((r) => r.isFirst);
                          } else if (index == 4) {
                            fifthTabNavKey.currentState?.popUntil((r) => r.isFirst);
                          }
                        }
                        tabBarController.update(index);
                      },
                      border: Border(top: BorderSide(width: 1.0, color: Theme.of(context).colorScheme.grey4)),
                      height: 80.0,
                      backgroundColor: (darkmodeController.mode.value == 'Dark') ? MsColors.darkBase : MsColors.lightBase,
                      items: [
                        BottomNavigationBarItem(icon: MsBottomNavItem(icon: 'home.svg', label: 'Əsas səhifə')),
                        BottomNavigationBarItem(icon: MsBottomNavItem(icon: 'catalog.svg', label: 'Kataloq')),
                        BottomNavigationBarItem(icon: MsBottomNavItem(icon: 'favorite.svg', label: 'İstək listi')),
                        BottomNavigationBarItem(icon: MsBottomNavItem(icon: 'cart.svg', label: 'Səbət')),
                        BottomNavigationBarItem(icon: MsBottomNavItem(icon: 'user.svg', label: 'Hesabım')),
                      ],
                    ),
                    tabBuilder: (context, index) {
                      return CupertinoTabView(
                        routes: Routes.routes,
                        navigatorKey: listOfKeys[index],
                        builder: (context) {
                          return pages[index];
                        },
                      );
                    },
                  ),
          ),
    );
  }
}
         

I tried PopScope method, but not success

1

There are 1 best solutions below

0
On

Edit PopScope widget onPopInvoked Function to this Code:

onPopInvoked: (didPop) {
              if (didPop) {
                return;
              }
               Navigator.pop(context);
              // Add Your Code here if you want to trigger it after invoked
            },