Back Button Fails to Dismiss Context Menu Toolbar on Android
Have you checked for an existing issue?
- [x] I have searched the existing issues
Flutter Quill Version
11.2.0
Steps to Reproduce
- Run on a real Android device and select some text.
- Once the context menu (AKA toolbar) is shown, press the back button
- The context menu is being ignored without dismissing it; it could lead to navigating back to the previous screen or closing the app.
Expected results
The toolbar should be dismissed instead. I don't remember this was an issue before, and I'm not sure which change made this regression.
Actual results
The context menu is still being displayed, and instead, the back button dismisses the keyboard or navigates back to the previous screen, or closes the app.
See the attached video for details.
Additional Context
Screenshots / Video demonstration
Current behavior:
https://github.com/user-attachments/assets/aa132450-83dd-4894-aa94-ee8243034d31
Expected behavior (Keep notes app as an example):
https://github.com/user-attachments/assets/9a8dc1f0-4017-45ad-a9d6-a8510f0fafc9
As a temporary fix until an official patch lands, you can wrap your QuillEditor in a WillPopScope that first checks if the editor is focused, unfocuses it (which dismisses the toolbar), and only allows back navigation when the editor is already unfocused. Wrap your QuillEditor in a WillPopScope that unfocuses the editor (hiding the toolbar) before popping the route:
WillPopScope(
onWillPop: () async {
if (_focusNode.hasFocus) {
_focusNode.unfocus();
return false;
}
return true;
},
child: QuillEditor(
controller: _controller,
focusNode: _focusNode,
scrollController: ScrollController(),
scrollable: true,
padding: EdgeInsets.all(8),
autoFocus: false,
readOnly: false,
expands: false,
),
)
This makes the first Back-press hide the toolbar and only subsequent presses navigate back.
if (_focusNode.hasFocus) { _focusNode.unfocus(); return false; } return true;
This is an Android specific issue. You might want to test this workaround on other platforms in addition to testing it on an Android emulator and apply it only when necessary. You should probably link this issue in a comment explaining the workaround. If a fix arrives, it will silently break the behavior without notice.