From 7f1eb3a818fa5e290027b42c10ba19a9f95553cf Mon Sep 17 00:00:00 2001 From: edde746 <86283021+edde746@users.noreply.github.com> Date: Fri, 12 Jun 2026 13:19:29 +0200 Subject: [PATCH] fix(focus): back over a dialog no longer pops the underlying screen --- lib/focus/dpad_navigator.dart | 28 +++++----------------------- lib/focus/key_event_utils.dart | 2 -- lib/widgets/overlay_sheet.dart | 6 ------ 3 files changed, 5 insertions(+), 31 deletions(-) diff --git a/lib/focus/dpad_navigator.dart b/lib/focus/dpad_navigator.dart index fed6b658..ddcdc589 100644 --- a/lib/focus/dpad_navigator.dart +++ b/lib/focus/dpad_navigator.dart @@ -99,35 +99,17 @@ class SelectKeyUpSuppressor { /// Global helper to suppress the next BACK key-up event. /// -/// Use this when a modal (bottom sheet, dialog) closes to prevent -/// the BACK key-up from propagating to the underlying screen. +/// Armed when a modal (dialog, sheet) closes while a back key is still held — +/// e.g. by [BackKeySuppressorObserver] when a route pops mid-press — so the +/// in-flight key-up doesn't propagate to the underlying screen's back handler. class BackKeyUpSuppressor { static final _instance = _KeyUpSuppressor((k) => k.isBackKey); - static bool _closedViaBackKey = false; - /// Mark that a modal is being closed via back key press. - /// Call this before Navigator.pop() in back key handlers. - static void markClosedViaBackKey() { - _closedViaBackKey = true; - } - - /// Request suppression of back key-up events. - /// Suppression is skipped if the modal was closed via back key - /// (since the key-up already triggered the close). - static void suppressBackUntilKeyUp() { - if (_closedViaBackKey) { - _closedViaBackKey = false; - return; - } - _instance.suppress(); - } + static void suppressBackUntilKeyUp() => _instance.suppress(); /// Clear any pending suppression. Call when opening a new modal /// to ensure stale suppression from previous closes doesn't affect it. - static void clearSuppression() { - _instance.clearSuppression(); - _closedViaBackKey = false; - } + static void clearSuppression() => _instance.clearSuppression(); static bool consumeIfSuppressed(KeyEvent event) => _instance.consumeIfSuppressed(event); } diff --git a/lib/focus/key_event_utils.dart b/lib/focus/key_event_utils.dart index 9d974972..e50fbcc1 100644 --- a/lib/focus/key_event_utils.dart +++ b/lib/focus/key_event_utils.dart @@ -79,8 +79,6 @@ KeyEventResult handleBackKeyAction(KeyEvent event, VoidCallback onBack) { if (event is KeyUpEvent) { BackKeyCoordinator.markHandled(); - // Mark that we're closing via back key so suppressBackUntilKeyUp() knows to skip - BackKeyUpSuppressor.markClosedViaBackKey(); onBack(); return KeyEventResult.handled; } diff --git a/lib/widgets/overlay_sheet.dart b/lib/widgets/overlay_sheet.dart index 28c76d35..7971ae61 100644 --- a/lib/widgets/overlay_sheet.dart +++ b/lib/widgets/overlay_sheet.dart @@ -379,12 +379,6 @@ class _OverlaySheetHostState extends State with SingleTickerPr _sheetHorizontalAnchor = null; }); widget.onOpenChanged?.call(false); - // Clear stale back-key flags. handleBackKeyAction sets - // markClosedViaBackKey() expecting a route pop, but the overlay - // doesn't pop a route. Without clearing, the flag leaks into the - // next real route pop and disables KeyUp suppression, causing a - // double-pop on the underlying screen. - BackKeyUpSuppressor.clearSuppression(); }); }