1

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];
              },
            );
          },
        ),
      ),
    );
  }
}
userName
  • 903
  • 2
  • 20
  • do you have localisation enabled, or show us `MaterialApp` part – nitishk72 May 05 '22 at 10:09
  • What do you mean by MaterialApp part? This error appeared after I added the code from the question that I attached in the description: when i added the GlobalKeys – userName May 05 '22 at 10:14
  • As the previous commenter asked, can you please share the root part of the code of your application - the `MaterialApp` or `CuppertinoApp` part - in it's entirety? Also please shared the error from the console, instead of the screenshot of the error on emulator. – J. S. May 05 '22 at 10:17
  • looks like you are using `CupertinoApp` instead of `MaterialApp`. am I right? – nitishk72 May 05 '22 at 10:19
  • I managed to fix the problem, only the back button on Android does not work as it should – userName May 05 '22 at 10:33

0 Answers0