@@ -2020,6 +2020,13 @@ class _InactiveElements {
20202020/// this callback.
20212021typedef ElementVisitor = void Function (Element element);
20222022
2023+ /// Signature for the callback to [BuildContext.visitAncestorElements] .
2024+ ///
2025+ /// The argument is the ancestor being visited.
2026+ ///
2027+ /// Return false to stop the walk.
2028+ typedef ConditionalElementVisitor = bool Function (Element element);
2029+
20232030/// A handle to the location of a widget in the widget tree.
20242031///
20252032/// This class presents a set of methods that can be used from
@@ -2221,14 +2228,15 @@ abstract class BuildContext {
22212228 ///
22222229 /// All of the qualifications about when [dependOnInheritedWidgetOfExactType] can
22232230 /// be called apply to this method as well.
2224- InheritedWidget dependOnInheritedElement (InheritedElement ancestor, { Object aspect });
2231+ InheritedWidget dependOnInheritedElement (InheritedElement ancestor, { Object ? aspect });
22252232
22262233 /// Obtains the nearest widget of the given type `T` , which must be the type of a
22272234 /// concrete [InheritedWidget] subclass, and registers this build context with
22282235 /// that widget such that when that widget changes (or a new widget of that
22292236 /// type is introduced, or the widget goes away), this build context is
22302237 /// rebuilt so that it can obtain new values from that widget.
22312238 ///
2239+ /// {@template flutter.widgets.BuildContext.dependOnInheritedWidgetOfExactType}
22322240 /// This is typically called implicitly from `of()` static methods, e.g.
22332241 /// [Theme.of] .
22342242 ///
@@ -2262,13 +2270,15 @@ abstract class BuildContext {
22622270 /// [InheritedWidget] subclasses that supports partial updates, like
22632271 /// [InheritedModel] . It specifies what "aspect" of the inherited
22642272 /// widget this context depends on.
2273+ /// {@endtemplate}
22652274 T ? dependOnInheritedWidgetOfExactType <T extends InheritedWidget >({ Object ? aspect });
22662275
22672276 /// Obtains the element corresponding to the nearest widget of the given type `T` ,
22682277 /// which must be the type of a concrete [InheritedWidget] subclass.
22692278 ///
22702279 /// Returns null if no such element is found.
22712280 ///
2281+ /// {@template flutter.widgets.BuildContext.getElementForInheritedWidgetOfExactType}
22722282 /// Calling this method is O(1) with a small constant factor.
22732283 ///
22742284 /// This method does not establish a relationship with the target in the way
@@ -2280,11 +2290,13 @@ abstract class BuildContext {
22802290 /// [dependOnInheritedWidgetOfExactType] in [State.didChangeDependencies] . It is
22812291 /// safe to use this method from [State.deactivate] , which is called whenever
22822292 /// the widget is removed from the tree.
2293+ /// {@endtemplate}
22832294 InheritedElement ? getElementForInheritedWidgetOfExactType <T extends InheritedWidget >();
22842295
22852296 /// Returns the nearest ancestor widget of the given type `T` , which must be the
22862297 /// type of a concrete [Widget] subclass.
22872298 ///
2299+ /// {@template flutter.widgets.BuildContext.findAncestorWidgetOfExactType}
22882300 /// In general, [dependOnInheritedWidgetOfExactType] is more useful, since
22892301 /// inherited widgets will trigger consumers to rebuild when they change. This
22902302 /// method is appropriate when used in interaction event handlers (e.g.
@@ -2306,11 +2318,13 @@ abstract class BuildContext {
23062318 ///
23072319 /// Returns null if a widget of the requested type does not appear in the
23082320 /// ancestors of this context.
2321+ /// {@endtemplate}
23092322 T ? findAncestorWidgetOfExactType <T extends Widget >();
23102323
23112324 /// Returns the [State] object of the nearest ancestor [StatefulWidget] widget
23122325 /// that is an instance of the given type `T` .
23132326 ///
2327+ /// {@template flutter.widgets.BuildContext.findAncestorStateOfType}
23142328 /// This should not be used from build methods, because the build context will
23152329 /// not be rebuilt if the value that would be returned by this method changes.
23162330 /// In general, [dependOnInheritedWidgetOfExactType] is more appropriate for such
@@ -2332,6 +2346,7 @@ abstract class BuildContext {
23322346 /// because the widget tree is no longer stable at that time. To refer to
23332347 /// an ancestor from one of those methods, save a reference to the ancestor
23342348 /// by calling [findAncestorStateOfType] in [State.didChangeDependencies] .
2349+ /// {@endtemplate}
23352350 ///
23362351 /// {@tool snippet}
23372352 ///
@@ -2344,17 +2359,20 @@ abstract class BuildContext {
23442359 /// Returns the [State] object of the furthest ancestor [StatefulWidget] widget
23452360 /// that is an instance of the given type `T` .
23462361 ///
2362+ /// {@template flutter.widgets.BuildContext.findRootAncestorStateOfType}
23472363 /// Functions the same way as [findAncestorStateOfType] but keeps visiting subsequent
23482364 /// ancestors until there are none of the type instance of `T` remaining.
23492365 /// Then returns the last one found.
23502366 ///
23512367 /// This operation is O(N) as well though N is the entire widget tree rather than
23522368 /// a subtree.
2369+ /// {@endtemplate}
23532370 T ? findRootAncestorStateOfType <T extends State >();
23542371
23552372 /// Returns the [RenderObject] object of the nearest ancestor [RenderObjectWidget] widget
23562373 /// that is an instance of the given type `T` .
23572374 ///
2375+ /// {@template flutter.widgets.BuildContext.findAncestorRenderObjectOfType}
23582376 /// This should not be used from build methods, because the build context will
23592377 /// not be rebuilt if the value that would be returned by this method changes.
23602378 /// In general, [dependOnInheritedWidgetOfExactType] is more appropriate for such
@@ -2371,13 +2389,16 @@ abstract class BuildContext {
23712389 /// because the widget tree is no longer stable at that time. To refer to
23722390 /// an ancestor from one of those methods, save a reference to the ancestor
23732391 /// by calling [findAncestorRenderObjectOfType] in [State.didChangeDependencies] .
2392+ /// {@endtemplate}
23742393 T ? findAncestorRenderObjectOfType <T extends RenderObject >();
23752394
23762395 /// Walks the ancestor chain, starting with the parent of this build context's
2377- /// widget, invoking the argument for each ancestor. The callback is given a
2378- /// reference to the ancestor widget's corresponding [Element] object. The
2379- /// walk stops when it reaches the root widget or when the callback returns
2380- /// false. The callback must not return null.
2396+ /// widget, invoking the argument for each ancestor.
2397+ ///
2398+ /// {@template flutter.widgets.BuildContext.visitAncestorElements}
2399+ /// The callback is given a reference to the ancestor widget's corresponding
2400+ /// [Element] object. The walk stops when it reaches the root widget or when
2401+ /// the callback returns false. The callback must not return null.
23812402 ///
23822403 /// This is useful for inspecting the widget tree.
23832404 ///
@@ -2387,10 +2408,12 @@ abstract class BuildContext {
23872408 /// because the element tree is no longer stable at that time. To refer to
23882409 /// an ancestor from one of those methods, save a reference to the ancestor
23892410 /// by calling [visitAncestorElements] in [State.didChangeDependencies] .
2390- void visitAncestorElements (bool Function (Element element) visitor);
2411+ /// {@endtemplate}
2412+ void visitAncestorElements (ConditionalElementVisitor visitor);
23912413
23922414 /// Walks the children of this widget.
23932415 ///
2416+ /// {@template flutter.widgets.BuildContext.visitChildElements}
23942417 /// This is useful for applying changes to children after they are built
23952418 /// without waiting for the next frame, especially if the children are known,
23962419 /// and especially if there is exactly one child (as is always the case for
@@ -2408,6 +2431,7 @@ abstract class BuildContext {
24082431 /// significantly cheaper to use an [InheritedWidget] and have the descendants
24092432 /// pull data down, than it is to use [visitChildElements] recursively to push
24102433 /// data down to them.
2434+ /// {@endtemplate}
24112435 void visitChildElements (ElementVisitor visitor);
24122436
24132437 /// Start bubbling this notification at the given build context.
@@ -4452,7 +4476,7 @@ abstract class Element extends DiagnosticableTree implements BuildContext {
44524476 }
44534477
44544478 @override
4455- void visitAncestorElements (bool Function ( Element element) visitor) {
4479+ void visitAncestorElements (ConditionalElementVisitor visitor) {
44564480 assert (_debugCheckStateIsActiveForAncestorLookup ());
44574481 Element ? ancestor = _parent;
44584482 while (ancestor != null && visitor (ancestor)) {
0 commit comments