Skip to content

FocusDetector

Reports when keyboard focus enters or leaves its subtree.

Learn more: Focus management

Import: package:fleury/fleury.dart, or package:fleury/fleury_core.dart in browser code.

// Panel already accents itself while focus is inside it. Use a detector
// when your own chrome or state should follow the active pane.
FocusDetector(
onFocusChange: (hasFocus) => setState(() => editorActive = hasFocus),
child: Container(
border: BoxBorder(
style: theme.borderStyle,
cellStyle: editorActive
? CellStyle(foreground: theme.colorScheme.primary)
: theme.mutedStyle,
),
child: editor,
),
)

onFocusChange fires with true when focus moves onto this subtree (the child or any descendant) and false when it leaves. It also fires true on mount if focus is already inside, so state that starts out false is always correct.

Detectors nest like CSS :focus-within: focus inside an inner detector is inside every detector around it, so each of them reports it. Widgets that watch focus with their own detector, such as a list or a table, don’t hide focus from a detector wrapped around them.

Use it for focus-reactive chrome: a tooltip that shows while its target is focused, an active-pane highlight, a section that styles itself when something inside has focus. To rebuild, call setState from the callback.

ParameterTypeDefaultDescription
onFocusChange:void Function(bool hasFocus)requiredCalled when focus enters or leaves child’s subtree. More
child:WidgetrequiredThe subtree whose focus boundary is observed.
  • onFocusChange: Moving focus between descendants of child does not call this again.

FocusDetector is defined in packages/fleury/lib/src/widgets/focus.dart.

Category: Input handling & focus · All widgets