import 'package:flutter/widgets.dart'; import 'destination.dart'; import 'shell_bar.dart'; /// The mounted Search screen publishes its field focus without clearing query /// state. The shell requests it after switching destinations has settled. class ShellSearchFocus { Object? _owner; VoidCallback? _focus; void request() => _focus?.call(); void _set(Object owner, VoidCallback focus) { _owner = owner; _focus = focus; } void _clear(Object owner) { if (_owner != owner) return; _owner = null; _focus = null; } } class ShellSearchShortcut extends StatefulWidget { const ShellSearchShortcut({ super.key, required this.onFocus, required this.child, }); final VoidCallback onFocus; final Widget child; @override State createState() => _ShellSearchShortcutState(); } class _ShellSearchShortcutState extends State { final _owner = Object(); ShellBarScope? _scope; @override void didChangeDependencies() { super.didChangeDependencies(); final scope = ShellBarScope.maybeOf(context); if (scope == _scope) return; _scope?.registry.searchFocus._clear(_owner); _scope = scope; if (scope?.destination == AppDestination.search) { scope!.registry.searchFocus._set(_owner, () => widget.onFocus()); } } @override void dispose() { _scope?.registry.searchFocus._clear(_owner); super.dispose(); } @override Widget build(BuildContext context) => widget.child; }