From f81d2d06bc1ce514fd0d56fb1f7efbae022b4474 Mon Sep 17 00:00:00 2001 From: Dain Nilsson Date: Wed, 6 Jul 2022 16:56:34 +0200 Subject: [PATCH] Add workaround for device picker losing focus on device change. --- lib/app/views/device_picker_dialog.dart | 41 ++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/lib/app/views/device_picker_dialog.dart b/lib/app/views/device_picker_dialog.dart index 27bcb78b..6ec53689 100755 --- a/lib/app/views/device_picker_dialog.dart +++ b/lib/app/views/device_picker_dialog.dart @@ -40,9 +40,48 @@ class _HiddenDevicesNotifier extends StateNotifier> { } } -class DevicePickerDialog extends ConsumerWidget { +class DevicePickerDialog extends StatefulWidget { const DevicePickerDialog({super.key}); + @override + State createState() => _DevicePickerDialogState(); +} + +class _DevicePickerDialogState extends State { + late FocusScopeNode _focus; + + @override + void initState() { + super.initState(); + _focus = FocusScopeNode(); + } + + @override + void dispose() { + _focus.dispose(); + super.dispose(); + } + + @override + Widget build(BuildContext context) { + // This keeps the focus in the dialog, even if the underlying page + // changes as it does when a new device is selected. + return FocusScope( + node: _focus, + autofocus: true, + onFocusChange: (focused) { + if (!focused) { + _focus.requestFocus(); + } + }, + child: const _DevicePickerContent(), + ); + } +} + +class _DevicePickerContent extends ConsumerWidget { + const _DevicePickerContent(); + @override Widget build(BuildContext context, WidgetRef ref) { final hidden = ref.watch(_hiddenDevicesProvider);