fix(Desktop): Backspace when editing fields (#552)

Co-authored-by: PartyDonut <PartyDonut@users.noreply.github.com>
This commit is contained in:
PartyDonut 2025-10-30 22:30:26 +01:00 committed by GitHub
parent 9a7d132d24
commit a223df1e82
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -14,27 +14,37 @@ class BackIntentDpad extends StatelessWidget {
if (AdaptiveLayout.inputDeviceOf(context) == InputDevice.touch) { if (AdaptiveLayout.inputDeviceOf(context) == InputDevice.touch) {
return child; return child;
} }
return Shortcuts( return Focus(
shortcuts: <LogicalKeySet, Intent>{ canRequestFocus: false,
LogicalKeySet(LogicalKeyboardKey.backspace): const BackIntent(), onKeyEvent: (FocusNode node, KeyEvent event) {
if (event is! KeyDownEvent) {
return KeyEventResult.ignored;
}
if (event.logicalKey == LogicalKeyboardKey.backspace) {
if (_isEditableTextFocused()) {
return KeyEventResult.ignored;
} else {
context.maybePop();
return KeyEventResult.handled;
}
}
return KeyEventResult.ignored;
}, },
child: Actions( child: child,
actions: <Type, Action<Intent>>{
BackIntent: CallbackAction<BackIntent>(
onInvoke: (intent) async {
final navigator = await context.maybePop();
if (navigator) {
return true;
} else {
return false;
}
},
),
},
child: child,
),
); );
} }
bool _isEditableTextFocused() {
final focus = FocusManager.instance.primaryFocus;
if (focus == null) return false;
final ctx = focus.context;
if (ctx == null) return false;
if (ctx.widget is EditableText) return true;
return ctx.findAncestorWidgetOfExactType<EditableText>() != null;
}
} }
class BackIntent extends Intent { class BackIntent extends Intent {