getx
getx copied to clipboard
[Flutter 3.16.5][GetX-5_RC5] PopScope issue
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:
- Create a GetView with GetController and DI using Bindings
- The build method needs to start with PopScope
- Go back
- 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(),
);
}
}
yes, please! solutions?
same problem
I have the same problem. When is it estimated that the issue can be resolved?
+1 Any update?
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!")
)
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+
I've also had this problem after popScope, but I'm currently not using two BackButtonListeners. This is indeed a problem.
Same problem here.
Related to https://github.com/flutter/flutter/issues/138614
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.
GetX 4.6.6 does not seem to have this problem and PopScope
can be used correctly
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 ! 👍
Same problem (Flutter version 3.19.1)
didPop
is always true.
getx 4.6.6 Same problem,didPop is always true.
Same problem (Flutter version 3.19.6) (get version 4.6.5) didPop is always true.
Has anyone found any solution?