skip to Main Content

I created one screen DashBoard, in that I set bottom navigation and set other screen that all working but i need to add functionality, when user press back button than if other tab selected then it navigates to first tab other wise open Alert Dialog for asking question like "Are you sure you want to exit the app?".

I have implement using Will Pop scope set in Dash board screen.This is my code.

class DashBoardView extends GetWidget<DashBoardController> {
  const DashBoardView({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    MySize().init(context);
    getStatusBarColor(color: AppColor.whiteF9F9F9);
    return WillPopScope(
        child: Scaffold(
          body: Obx(() => controller.pages[controller.currentIndex.value]),
          bottomNavigationBar: customBottomNavigation(),
        ),
        onWillPop: () {
          if (controller.currentIndex.value == 0) {
            AppDialog.appAlertDialog(
                context: context,
                description: "Are you sure you want to exitnfrom the app? ",
                question: 'Exit app',
                questionTextStyle: TextStyle(fontSize: 14, color: AppColor.grey, fontWeight: FontWeight.w700),
                onTap: () {
                  Get.put(SocketController()).disconnectFromSocket();
                  exit(0);
                });
          } else {
            controller.currentIndex.value = 0;
            controller.update();
          }
          return Future.value(false);
        });
  }
}

But it is not working in Android 14+ version, I have also replace the Will pop scope to PopScope as per latest Update but it still not working.

If Anyone face this issue so please give me suggestion about this functionality in android 14+ version.

2

Answers


  1. Try this-

    retrun WillPopScope(
         onWillPop: exitPopup,
         ..... );
    
    Future<bool> exitPopup() async {
            return await showDialog(
                  context: context,
                  builder: (context) => AlertDialog(
                    contentPadding: EdgeInsets.zero,
                    content: Container(
                      alignment: Alignment.center,
                      width: double.infinity,
                      decoration: BoxDecoration(
                        color: Colors.transparent,
                        borderRadius: BorderRadius.only(
                          topRight: Radius.circular(3.0),
                          // bottomRight: Radius.circular(40.0),
                          topLeft: Radius.circular(6.0),
                          // bottomLeft: Radius.circular(40.0),
                        ),
                      ),
                      height: 40,
                      child: Text(
                        "Alert!",
                        textAlign: TextAlign.center,
                        style: TextStyle(
                            color: BrandColors.kRed, fontWeight: FontWeight.bold),
                      ),
                    ),
                    actions: [
                      Container(
                          alignment: Alignment.center,
                          margin: EdgeInsets.symmetric(vertical: 20),
                          child: Text(
                            "Do you want to exit the application?",
                            style: TextStyle(fontWeight: FontWeight.bold),
                          )),
                      Row(
                        children: [
                          SizedBox(width: 5),
                          Expanded(
                            child: OutlinedButton(
                              style: ButtonStyle(
                                backgroundColor:
                                    MaterialStateProperty.all(BrandColors.brandColor),
                              ),
                              onPressed: () => Navigator.of(context).pop(false),
                              child: const Text('Cancel',
                                  style: TextStyle(color: Colors.white)),
                            ),
                          ),
                          SizedBox(width: 5),
                          Expanded(
                            child: OutlinedButton(
                              style: ButtonStyle(
                                  // backgroundColor: MaterialStateProperty.all(
                                  //     Colors.blue.withOpacity(0.2)),
                                  ),
                              onPressed: () => Navigator.of(context).pop(true),
                              child: const Text('Yes',
                                  style: TextStyle(color: Colors.black)),
                            ),
                          ),
                          SizedBox(width: 5),
                        ],
                      ),
                      // SizedBox(height: 20),
                    ],
                  ),
                ) ??
                false;
          }
    
    Login or Signup to reply.
  2. WillPopScope is deprecrated, so it is better to use PopScope.

    return SafeArea(child: PopScope(
      canPop: false,
      onPopInvoked: (bool didPop) async{
        if(didPop)
        {
          return;
        }
        if (controller.currentIndex.value == 0) {
            AppDialog.appAlertDialog(
                context: context,
                description: "Are you sure you want to exitnfrom the app? ",
                question: 'Exit app',
                questionTextStyle: TextStyle(fontSize: 14, color: AppColor.grey, fontWeight: FontWeight.w700),
                onTap: () {
                  Get.put(SocketController()).disconnectFromSocket();
                  exit(0);
                });
          } else {
            controller.currentIndex.value = 0;
            controller.update();
          }
          return Future.value(false);
        }
      },
      child: Scaffold(
        appBar: AppBar(),
        body: Container(
          child: Center(
            child: TextButton(onPressed: (){
              Navigator.pop(context);
            }, child: const Text('Back')),
          ),
        ),
      ),
    ));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search