Files
SpotiFLAC-Mobile/lib/widgets/skip_autofocus.dart
T
zarzet 2e77f63fe6 fix(desktop): keep Space for playback next to button groups
M3EButtonGroup autofocuses its first segment. On desktop that took focus
from the shortcuts' idle node, so Space pressed the segment instead of
toggling playback on the Library songs and albums pages, in Settings rows
with a segmented choice and in the equalizer.

MaterialExpressiveScope can now keep its child from taking focus as it
mounts. The group's segments stay unfocusable until the mounting frame's
autofocus pass has run, then Tab and clicks reach them as before.

Also drops two unneeded imports from the songs table test.
2026-10-21 22:24:11 +07:00

44 lines
1.2 KiB
Dart

import 'dart:async';
import 'package:flutter/widgets.dart';
/// Mounts [child] without letting it take keyboard focus.
///
/// `M3EButtonGroup` autofocuses its first enabled segment. On desktop that
/// moves focus off the app's idle node, so Space presses the segment instead
/// of toggling playback. The descendants stay unfocusable until the
/// autofocus pass of the mounting frame has run; after that, Tab and
/// clicks reach them as usual.
class SkipAutofocus extends StatefulWidget {
const SkipAutofocus({super.key, required this.child});
final Widget child;
@override
State<SkipAutofocus> createState() => _SkipAutofocusState();
}
class _SkipAutofocusState extends State<SkipAutofocus> {
var _focusable = false;
@override
void initState() {
super.initState();
// Autofocus applies in a microtask the mounting frame's build queues. A
// microtask queued after that frame runs after it.
WidgetsBinding.instance.addPostFrameCallback((_) {
scheduleMicrotask(() {
if (mounted) setState(() => _focusable = true);
});
});
}
@override
Widget build(BuildContext context) => Focus(
canRequestFocus: false,
skipTraversal: true,
descendantsAreFocusable: _focusable,
child: widget.child,
);
}