Code Monkey home page Code Monkey logo

Comments (12)

Rahul-Sharma-Github avatar Rahul-Sharma-Github commented on May 27, 2024 1

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 ! 👍

from getx.

xxqwf avatar xxqwf commented on May 27, 2024

yes, please! solutions?

from getx.

Unicellular-SU avatar Unicellular-SU commented on May 27, 2024

same problem

from getx.

leeyisoft avatar leeyisoft commented on May 27, 2024

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

from getx.

priyesh-techabbot avatar priyesh-techabbot commented on May 27, 2024

+1 Any update?

from getx.

ltq918 avatar ltq918 commented on May 27, 2024

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!")
)

from getx.

deepss1 avatar deepss1 commented on May 27, 2024

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+

from getx.

ltq918 avatar ltq918 commented on May 27, 2024

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

from getx.

guperini avatar guperini commented on May 27, 2024

Same problem here.

from getx.

jonataslaw avatar jonataslaw commented on May 27, 2024

Related to flutter/flutter#138614

from getx.

deepss1 avatar deepss1 commented on May 27, 2024

Related to flutter/flutter#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.

from getx.

ltq918 avatar ltq918 commented on May 27, 2024

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

from getx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.