fix(focus): back over a dialog no longer pops the underlying screen
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -379,12 +379,6 @@ class _OverlaySheetHostState extends State<OverlaySheetHost> 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();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user