Android back button doesn't work correctly in CupertinoTabBar apps

262 views Asked by At

How can I set up a native back button on Android devices when using CupertinoTAbScaffold?So that she does not close the application, but returns back. I tried to solve with the method which is described in this question: Flutter : CupertinoTabScaffold - Back button close app on Android from TabView's navigation. But the method presented in this question does not work. How can this be implemented? Help fix the problem, please. My Code:

class HomeScreen extends StatefulWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  final GlobalKey<NavigatorState> firstTabNavKey = GlobalKey<NavigatorState>();
  final GlobalKey<NavigatorState> secondTabNavKey = GlobalKey<NavigatorState>();
  final GlobalKey<NavigatorState> thirdTabNavKey = GlobalKey<NavigatorState>();

  CupertinoTabController? tabController;

  //widgets in tabs
  List<Widget> data = [
    ImagePickerScreen(),
    DataPickerScreen(),
    UserDataScreen()
  ];

  @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];
    return CupertinoApp(
      home: WillPopScope(
        onWillPop: () async {
          return !await listOfKeys[tabController!.index]
              .currentState!
              .maybePop();
        },
        child: CupertinoTabScaffold(
          tabBar: CupertinoTabBar(
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: Icon(CupertinoIcons.circle),
                label: 'Image Picker',
              ),
              BottomNavigationBarItem(
                icon: Icon(CupertinoIcons.calendar),
                label: 'Data Picker',
              ),
              BottomNavigationBarItem(
                icon: Icon(CupertinoIcons.pencil),
                label: 'User Data',
              ),
            ],
          ),
          tabBuilder: (BuildContext context, int index) {
            return CupertinoTabView(
              navigatorKey: listOfKeys[index],
              builder: (BuildContext context) {
                return data[index];
              },
            );
          },
        ),
      ),
    );
  }
}

0

There are 0 answers