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.
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.
WillPopScope should still work even after opening the keyboard.
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<Engine>.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: <Widget>[TextButton(
onPressed: () {
print("Hello ${field.text}!");
Navigator.pop(context);
},
child: const Text("OK"),
)],
);
});
},
child: const Text("Show Dialog"),
)),
);
}
}
[β] 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.
There's no error on the console
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too