maybeOf static method

FocusNode? maybeOf(
  1. BuildContext context, {
  2. bool scopeOk = false,
  3. bool createDependency = true,
})

Returns the focusNode of the Focus that most tightly encloses the given BuildContext.

If no Focus node is found before reaching the nearest FocusScope widget, or there is no Focus widget in scope, then this method will return null.

If createDependency is true (which is the default), calling this function creates a dependency that will rebuild the given context when the focus node gains or loses focus.

See also:

  • of, which is similar to this function, but will throw an exception if it doesn't find a Focus node, instead of returning null.

Implementation

static FocusNode? maybeOf(BuildContext context, { bool scopeOk = false, bool createDependency = true }) {
  final _FocusInheritedScope? scope = createDependency
      ? context.dependOnInheritedWidgetOfExactType<_FocusInheritedScope>()
      : context.getInheritedWidgetOfExactType<_FocusInheritedScope>();

  return switch (scope?.notifier) {
    null => null,
    FocusScopeNode() when !scopeOk => null,
    final FocusNode node => node,
  };
}