feat: add D-pad navigation for phones

This commit is contained in:
randogoth 2026-10-13 11:47:01 +03:00
parent 414f8e8484
commit b3df7fba9f
6 changed files with 380 additions and 31 deletions

View file

@ -1,7 +1,7 @@
import 'dart:ui';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter/material.dart';
/// [key] with Cmd on Apple platforms and Ctrl elsewhere.
SingleActivator primaryShortcut(LogicalKeyboardKey key, {bool shift = false}) {
@ -10,6 +10,21 @@ SingleActivator primaryShortcut(LogicalKeyboardKey key, {bool shift = false}) {
return SingleActivator(key, control: !apple, meta: apple, shift: shift);
}
/// Phones with a D-pad move focus between screen areas with the arrow keys,
/// so the soft keyboard closes while browsing.
bool get dpadNavigation =>
defaultTargetPlatform == TargetPlatform.android ||
defaultTargetPlatform == TargetPlatform.iOS;
/// Opens the popup menu behind [key] with its first item focused, so the
/// D-pad can move through it.
void openMenuWithFocus(GlobalKey<PopupMenuButtonState<String>> key) {
key.currentState?.showButtonMenu();
// The menu takes focus in a microtask after its first frame, so wait for that.
WidgetsBinding.instance.addPostFrameCallback((_) =>
Future(() => FocusManager.instance.primaryFocus?.nextFocus()));
}
final quitShortcut = primaryShortcut(LogicalKeyboardKey.keyQ);
Future<void> quitApp() =>