Add keyboard navigation to search results

Keep the search field focused while selecting results with Control or arrow keys, and open the selected result with Enter. Document the field-scoped shortcuts and cover navigation, reset, submission, and typing behavior with regression tests.
This commit is contained in:
2026-11-13 15:51:00 +00:00
parent eeb07e5645
commit ccc80a9a8d
9 changed files with 372 additions and 44 deletions
+37 -32
View File
@@ -18,6 +18,7 @@ class AppSearchField extends StatelessWidget {
this.onCleared,
this.autofocus = false,
this.focusNode,
this.onKeyEvent,
this.inBar = false,
});
@@ -37,6 +38,7 @@ class AppSearchField extends StatelessWidget {
final VoidCallback? onCleared;
final bool autofocus;
final FocusNode? focusNode;
final FocusOnKeyEventCallback? onKeyEvent;
final bool inBar;
void _clear() {
@@ -58,38 +60,41 @@ class AppSearchField extends StatelessWidget {
);
return ValueListenableBuilder<TextEditingValue>(
valueListenable: controller,
builder: (context, value, _) => TextField(
key: fieldKey,
controller: controller,
focusNode: focusNode,
autofocus: autofocus,
textInputAction: TextInputAction.search,
onChanged: onChanged,
onSubmitted: onSubmitted,
decoration: InputDecoration(
hintText: placeholder,
prefixIcon: const Icon(Icons.search),
suffixIcon: value.text.isEmpty
? null
: IconButton(
key: clearKey,
tooltip: 'Clear search',
onPressed: _clear,
icon: const Icon(Icons.close),
),
filled: !inBar,
fillColor: scheme.surfaceContainerHigh,
isDense: true,
border: stadium,
enabledBorder: stadium,
// In the bar the pill is the field's outline; on a page the focused
// field shows the host's focus ring.
focusedBorder: inBar
? stadium
: OutlineInputBorder(
borderRadius: radius,
borderSide: BorderSide(color: scheme.primary, width: 2),
),
builder: (context, value, _) => Focus(
onKeyEvent: onKeyEvent,
child: TextField(
key: fieldKey,
controller: controller,
focusNode: focusNode,
autofocus: autofocus,
textInputAction: TextInputAction.search,
onChanged: onChanged,
onSubmitted: onSubmitted,
decoration: InputDecoration(
hintText: placeholder,
prefixIcon: const Icon(Icons.search),
suffixIcon: value.text.isEmpty
? null
: IconButton(
key: clearKey,
tooltip: 'Clear search',
onPressed: _clear,
icon: const Icon(Icons.close),
),
filled: !inBar,
fillColor: scheme.surfaceContainerHigh,
isDense: true,
border: stadium,
enabledBorder: stadium,
// In the bar the pill is the field's outline; on a page the focused
// field shows the host's focus ring.
focusedBorder: inBar
? stadium
: OutlineInputBorder(
borderRadius: radius,
borderSide: BorderSide(color: scheme.primary, width: 2),
),
),
),
),
);