getx icon indicating copy to clipboard operation
getx copied to clipboard

[Flutter 3.16.5][GetX-5_RC5] PopScope issue

Open deepss1 opened this issue 1 year ago • 18 comments

Describe the bug The callback value in onPopInvoked is always true. Even when the canPop is set to false.

To Reproduce Steps to reproduce the behavior:

  1. Create a GetView with GetController and DI using Bindings
  2. The build method needs to start with PopScope
  3. Go back
  4. See the issue

Expected behavior The callback value should be false when canPop is false

Flutter Version: 3.16.5

Getx Version: get: ^5.0.0-release-candidate-5

Describe on which device you found the bug: Pixel 7 : Android 14

Minimal reproduce code

class AppFormBuilderView extends GetView<AppFormBuilderController> {
  const AppFormBuilderView({super.key});

  @override
  Widget build(BuildContext context) {
    return PopScope(
      // canPop: controller.formData().formMode == FormMode.create,
      canPop: false,
      onPopInvoked: (bool didPop) async {
        logW('didPop: $didPop');
        if (didPop) {
          return;
        }
        var canPop = await controller.confirmDiscard();
        if (canPop) {
          Get.back();
        }
      },
      child: _buildMainBody(),
    );
  }
}

deepss1 avatar Dec 24 '23 01:12 deepss1

yes, please! solutions?

xxqwf avatar Dec 29 '23 06:12 xxqwf

same problem

Unicellular-SU avatar Jan 03 '24 05:01 Unicellular-SU

I have the same problem. When is it estimated that the issue can be resolved?

leeyisoft avatar Jan 04 '24 07:01 leeyisoft

+1 Any update?

priyesh-techabbot avatar Jan 12 '24 07:01 priyesh-techabbot

You can currently use BackButtonListener() as a temporary alternative, like this

BackButtonListener(
  onBackButtonPressed: () {
    var routePath = Get.currentRoute;
    if (routePath == '/') {
      if (_lastPressedAt == null ||
          DateTime.now().difference(_lastPressedAt!) >
              const Duration(seconds: 2)) {
        _lastPressedAt = DateTime.now();
        ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
          content: Text("Press again to exit the program"),
        ));
        return Future.value(true);
      }
    } else {
      Navigator.pop(context);
      return Future.value(true);
    }
    SystemNavigator.pop();
    return Future.value(false);
  },
  child:Text("Hello,World!")
)

ltq918 avatar Jan 12 '24 08:01 ltq918

You can currently use BackButtonListener() as a temporary alternative, like this

BackButtonListener(
  onBackButtonPressed: () {
    var routePath = Get.currentRoute;
    if (routePath == '/') {
      if (_lastPressedAt == null ||
          DateTime.now().difference(_lastPressedAt!) >
              const Duration(seconds: 2)) {
        _lastPressedAt = DateTime.now();
        ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
          content: Text("Press again to exit the program"),
        ));
        return Future.value(true);
      }
    } else {
      Navigator.pop(context);
      return Future.value(true);
    }
    SystemNavigator.pop();
    return Future.value(false);
  },
  child:Text("Hello,World!")
)

The BackButtonListener adds more problem when you have two of them.

Eg.

Your home screen has BottomNavigationBar, so you put a BackButtonListener to check if the user is on the 1st tab, if yes, then exit, else change the current tab to 1st one.

This works, however, if you have a popup open which needs to block the back button, you cannot rely on BackButtonListener as both the listeners will be triggered.


The idea of supporting the PopScope is to have the support for Predictive Back Gesture in Android 13+

You can currently enable this feature from the Developer Options in Android 13+

deepss1 avatar Jan 12 '24 09:01 deepss1

I've also had this problem after popScope, but I'm currently not using two BackButtonListeners. This is indeed a problem.

ltq918 avatar Jan 12 '24 09:01 ltq918

Same problem here.

guperini avatar Jan 19 '24 17:01 guperini

Related to https://github.com/flutter/flutter/issues/138614

jonataslaw avatar Jan 26 '24 03:01 jonataslaw

Related to https://github.com/flutter/flutter/issues/138614

Thanks for your time to look into the issue.

However, even if the WillPopScope is retained, can we fix the PopScope in some way ? 🫣

This can be the low priority issue as I understand you are highly occupied with the new GetX update.

deepss1 avatar Jan 26 '24 03:01 deepss1

GetX 4.6.6 does not seem to have this problem and PopScope can be used correctly

ltq918 avatar Jan 26 '24 04:01 ltq918

The Solution =

  • Show AlertDialog when user Press back Button and GoBack or Stay on the same Page According to User Action ( Yes or No ) [ Logic ] ⇒
@override
  Widget build(BuildContext context) {
    // Back Navigation Logic while using AlertDialog
    return PopScope(
      canPop: false,
      onPopInvoked: (didPop) async {
        // if true then return nothing
        if (didPop) {
          return;
        }
        // if false then return AlertDialog Widget
        await showDialog(
          context: context,
          barrierDismissible: false,
          builder: (context) => AlertDialog(
            title: const Text('Do you want to Edit this ?'),
            actions: [
              OutlinedButton(
                onPressed: () {
                  // if user click on Yes then,

                  // First close the Dialog
                  Get.back();
                  // Then close the current page and go back
                  Get.back();
                },
                child: const Text('Yes'),
              ),
              OutlinedButton(
                onPressed: () {
                  // if user click on No then,

                  // Just close the dialog
                  Get.back();
                },
                child: const Text('No'),
              ),
            ],
          ),
        );
      },
      child: Scaffold();

======================================================================

Completely Tested on Production App.

It is Working Fine ! 👍

Rahul-Sharma-Github avatar Feb 05 '24 13:02 Rahul-Sharma-Github

Same problem (Flutter version 3.19.1) didPop is always true.

jeremy-giles avatar Apr 02 '24 10:04 jeremy-giles

getx 4.6.6 Same problem,didPop is always true.

xiazhichao avatar May 14 '24 05:05 xiazhichao

Same problem (Flutter version 3.19.6) (get version 4.6.5) didPop is always true.

miaozhenkun avatar Jun 05 '24 07:06 miaozhenkun

Has anyone found any solution?

kiaxseventh avatar Jun 18 '24 22:06 kiaxseventh