flame-engine / flame-engine/flame
WillPopScope on Android Doesn't Work After Opening Keyboard on a Dialog
- Dominant language
- Dart
- Stars
- 10.8k
- Forks
- 1k
- Avg merge
- 1d 20h
- Merged PRs (30d)
- 21
Description
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:
```
import 'package:flutter/material.dart';
import 'package:flame/events.dart';
import 'package:flame/game.dart';
void main() {
runApp(MaterialApp(home: Scaffold(body: GameWidget.controlled(
gameFactory: Engine.new,
initialActiveOverlays: const ["Page 1"],
overlayBuilderMap: {
"Page 1": (_, engine) => Page1(engine: engine),
"Page 2": (_, engine) => Page2(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"),
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 WillPopScope(
onWillPop: () async {
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: [TextButton(
onPressed: () {
print("Hello ${field.text}!");
Navigator.pop(context);
},
child: const Text("OK"),
)],
);
});
},
child: const Text("Show Dialog"),
)),
);
}
}
```
# Flutter doctor output
```
[√] Flutter (Channel stable, 3.13.0, on Microsoft Windows [Version 10.0.22621.2283], locale en-US)
• Flutter version 3.13.0 on channel stable at C:\Users\MSI\Documents\Flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision efbf63d9c6 (5 weeks ago), 2023-08-15 21:05:06 -0500
• Engine revision 1ac611c64e
• Dart version 3.1.0
• DevTools version 2.25.0
[√] Windows Version (Installed version of Windows is version 10 or higher)
[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
• Android SDK at C:\Users\MSI\AppData\Local\Android\sdk
• Platform android-34, build-tools 34.0.0
• Java binary at: C:\Program Files\Android\Android Studio\jbr\bin\java
• Java version OpenJDK Runtime Environment (build 17.0.6+0-b2043.56-10027231)
• All Android licenses accepted.
[X] Chrome - develop for the web (Cannot find Chrome executable at .\Google\Chrome\Application\chrome.exe)
! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.
[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.6.5)
• Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community
• Visual Studio Community 2022 version 17.6.33829.357
• Windows 10 SDK version 10.0.22000.0
[√] Android Studio (version 2022.3)
• Android Studio at C:\Program Files\Android\Android Studio
• Flutter plugin can be installed from:
https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 17.0.6+0-b2043.56-10027231)
[√] Connected device (2 available)
• Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.22621.2283]
• Edge (web) • edge • web-javascript • Microsoft Edge 117.0.2045.31
[√] Network resources
• All expected network resources are available.
! Doctor found issues in 1 category.
```
# More environment information
* Flutter 3.13.0
* Platform affected: Android
* Platform version affected: Android 11
* Flame: ^1.8.2
# Log information
There's no error on the console
Contributor guide
Assessment
This issue has not been assessed yet.