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;
  if (createDependency) {
    scope = context.dependOnInheritedWidgetOfExactType<_FocusInheritedScope>();
  } else {
    scope = context.getInheritedWidgetOfExactType<_FocusInheritedScope>();
  }
  final FocusNode? node = scope?.notifier;
  if (node == null) {
    return null;
  }
  if (!scopeOk && node is FocusScopeNode) {
    return null;
  }
  return node;
}