-
-
Notifications
You must be signed in to change notification settings - Fork 872
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WillPopScope on Android Doesn't Work After Opening Keyboard on a Dialog #2752
Comments
Can you try with Flutter 3.13.4? |
@spydon Still doesn't work 😞 |
Can you try to reproduce this in pure Flutter? This doesn't seem like a Flame issue. |
@spydon Yes, the keyboard closes, and the dialog closes too. But it won't go back to page 1. It works fine in pure flutter:
|
Thanks for testing in pure flutter too, @flame-engine/flame-admin any clue what the issue could be here? |
humm, that is weird, at a first glance I thought this could be that common mistake of using the wrong context, but as I am looking on your code, you are indeed doing a pop using the correct one. We will probably need to debug this to understand better, as I can't understand what in Flame could lead to this. |
@spydon No problem 😊 @erickzanardo I found a more minimal way to reproduce the error. Apparently it's not specific to WillPopScope. Usually pressing the back button will close the app. But after opening a keyboard on a dialog box, the back is just not responding (the app won't close). More minimal:
|
|
@ufrshubham Still doesn't work 😞 |
I have just now tried it with the latest version of both Flutter Maybe you can check once if this issue still persists @hilmiyafia |
I'll close this and then we can open it up if anyone is able to reproduce the issue again |
@spydon @immadisairaj No, it's not fixed. Please reopen the issue. Here I made a video of the problem: YouTube |
Ohh.. my bad. Sorry. I only saw the second example and thought the keyboard closure is happening correctly. Well, I checked it now and the issue still exists. Adding to it, there is another which I saw. import 'package:flame/events.dart';
import 'package:flame/game.dart';
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
home: Scaffold(
body: PopScope(
canPop: false,
onPopInvoked: (_) {
print('------ main pop scope called');
},
child: GameWidget<Engine>.controlled(
gameFactory: Engine.new,
initialActiveOverlays: const ["Page 1"],
overlayBuilderMap: {
"Page 1": (_, engine) =>
Page1(key: const ValueKey('JUST_TO_TEST_1'), engine: engine),
"Page 2": (_, engine) =>
Page2(key: const ValueKey('JUST_TO_TEST_2'), engine: engine),
},
),
),
),
),
);
}
class Engine extends FlameGame with ScaleDetector {
void changePage(String add, String remove) {
overlays.remove(remove);
overlays.add(add);
}
}
class Page1 extends StatelessWidget {
final Engine engine;
const Page1({
super.key,
required this.engine,
});
@override
Widget build(BuildContext context) {
return Center(
child: ElevatedButton(
onPressed: () {
engine.changePage("Page 2", "Page 1");
// Navigator.of(context).push(MaterialPageRoute(builder: (_) => Page2()));
},
child: const Text("Go to Page 2"),
));
}
}
class Page2 extends StatelessWidget {
final Engine engine;
final TextEditingController field = TextEditingController();
Page2({
super.key,
required this.engine,
});
@override
Widget build(BuildContext context) {
return PopScope(
// canPop: false,
onPopInvoked: (_) {
print('------pop invoked');
engine.changePage("Page 1", "Page 2");
// return false;
},
child: Center(
child: ElevatedButton(
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text("Input your name:"),
content: TextField(controller: field),
actions: <Widget>[
TextButton(
onPressed: () {
print("Hello ${field.text}!");
Navigator.pop(context);
},
child: const Text("OK"),
)
],
);
});
},
child: const Text("Show Dialog"),
)),
);
}
} Output: # when keyboard isn't opened at all
I/flutter ( 8431): ------ main pop scope called
I/flutter ( 8431): Hello !
I/flutter ( 8431): ------ main pop scope called
I/flutter ( 8431): ------pop invoked
# when keyboard is open and Ok is clicked
I/AssistStructure( 8431): Flattened final assist data: 388 bytes, containing 1 windows, 3 views
I/flutter ( 8431): Hello !/
# trying to press the back button
# the pop scope isn't getting called at all The issue still exists. I see the pop scope isn't invoked at all when the keyboard comes up and closed. Removing the Flame widgets, with only navigators, the pop scope works as expected. Edit: After the keyboard not working, tried hot restarting the app, then the page doesn't come to 1 even if keyboard is not opened. |
@immadisairaj Thank you for checking it again 😊 |
After a bit more investigating, I am probably thinking that there is something wrong with Navigator pop invocation when back button pressed. I tried to add another button (with onPressed: () {
Navigator.maybePop(context);
}, And guess what, the |
Have you tried reproducing this in pure flutter? I doubt that this is a Flame bug. |
I tried with pure Flutter. And it works fine there (both with navigation or a stateful widget and swap child). Yeah, seems to be a Flame bug. Maybe related to some bindings? I am a bit confused on where to look.. |
Alright, thanks for checking! Maybe it has something to do with where the focus lays. |
Looks like! I had did something in the example code something like this and the back button works after dialog is closed. showDialog(...).then((_) => FocusScope.of(context).requestFocus(FocusNode())); This requests the new focus node. It doesn't seem to be a permanent fix. But, tells us that the issue is really with the Focus. Edit: showDialog(...).then((value) => focusNode.unfocus()); // focusNode is the one attached to GameWidget As a work around, keeping PS: This can be dirty |
Another update: GameWidget(focusNode: FocusNode(canRequestFocus: false), .....), Here we are making the GameWidget to not receive any focus. The problem I see is that when keyboard is opened and closed, the focus comes onto the GameWidget where it doesn't seem to have the back button handled (not sure)? But, once we tell the GameWidget that don't request the focus scope, then the focus shifts to parent and works as expected. |
Please let us know if it works on what you are working @hilmiyafia (if it is not affecting anything other in the game) Also, how should we proceed with this? Any suggestions @spydon?
|
I would go for number three, if anyone want to write a few line of docs for it. :) |
@immadisairaj Yes it works now, thank you so much 😊 |
I have 2 overlay pages on my game.
There is a button on the first page that takes you to the second page.
On the second page there's a button that opens up a dialog that ask you to input a name.
To go back to the first page, I use WillPopScope to detect the back key being pressed.
Current bug behavior
After opening the keyboard to input a name, the WillPopScope doesn't work.
Only if I don't select the TextField (thus not opening the keyboard) the WillPopScope will work.
Just opening the dialog does not cause the error.
Expected behavior
WillPopScope should still work even after opening the keyboard.
Steps to reproduce
Run this program on Android:
Flutter doctor output
More environment information
Log information
There's no error on the console
The text was updated successfully, but these errors were encountered: