public static FocusScopeNode of(BuildContext context) { D.assert(context != null); _FocusMarker marker = context.dependOnInheritedWidgetOfExactType <_FocusMarker>(); return(marker?.notifier?.nearestScope ?? context.owner.focusManager.rootScope); }
public static FocusNode of(BuildContext context, bool nullOk = false, bool scopeOk = false) { D.assert(context != null); _FocusMarker marker = context.dependOnInheritedWidgetOfExactType <_FocusMarker>(); FocusNode node = marker?.notifier; if (node == null) { if (!nullOk) { throw new UIWidgetsError( "Focus.of() was called with a context that does not contain a Focus widget.\n" + "No Focus widget ancestor could be found starting from the context that was passed to " + "Focus.of(). This can happen because you are using a widget that looks for a Focus " + "ancestor, and do not have a Focus widget descendant in the nearest FocusScope.\n" + "The context used was:\n" + $" {context}" ); } return(null); } if (!scopeOk && node is FocusScopeNode) { if (!nullOk) { throw new UIWidgetsError( "Focus.of() was called with a context that does not contain a Focus between the given " + "context and the nearest FocusScope widget.\n" + "No Focus ancestor could be found starting from the context that was passed to " + "Focus.of() to the point where it found the nearest FocusScope widget. This can happen " + "because you are using a widget that looks for a Focus ancestor, and do not have a " + "Focus widget ancestor in the current FocusScope.\n" + "The context used was:\n" + $" {context}" ); } return(null); } return(node); }