@@ -2020,6 +2020,13 @@ class _InactiveElements {
2020
2020
/// this callback.
2021
2021
typedef ElementVisitor = void Function (Element element);
2022
2022
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
+
2023
2030
/// A handle to the location of a widget in the widget tree.
2024
2031
///
2025
2032
/// This class presents a set of methods that can be used from
@@ -2221,14 +2228,15 @@ abstract class BuildContext {
2221
2228
///
2222
2229
/// All of the qualifications about when [dependOnInheritedWidgetOfExactType] can
2223
2230
/// be called apply to this method as well.
2224
- InheritedWidget dependOnInheritedElement (InheritedElement ancestor, { Object aspect });
2231
+ InheritedWidget dependOnInheritedElement (InheritedElement ancestor, { Object ? aspect });
2225
2232
2226
2233
/// Obtains the nearest widget of the given type `T` , which must be the type of a
2227
2234
/// concrete [InheritedWidget] subclass, and registers this build context with
2228
2235
/// that widget such that when that widget changes (or a new widget of that
2229
2236
/// type is introduced, or the widget goes away), this build context is
2230
2237
/// rebuilt so that it can obtain new values from that widget.
2231
2238
///
2239
+ /// {@template flutter.widgets.BuildContext.dependOnInheritedWidgetOfExactType}
2232
2240
/// This is typically called implicitly from `of()` static methods, e.g.
2233
2241
/// [Theme.of] .
2234
2242
///
@@ -2262,13 +2270,15 @@ abstract class BuildContext {
2262
2270
/// [InheritedWidget] subclasses that supports partial updates, like
2263
2271
/// [InheritedModel] . It specifies what "aspect" of the inherited
2264
2272
/// widget this context depends on.
2273
+ /// {@endtemplate}
2265
2274
T ? dependOnInheritedWidgetOfExactType <T extends InheritedWidget >({ Object ? aspect });
2266
2275
2267
2276
/// Obtains the element corresponding to the nearest widget of the given type `T` ,
2268
2277
/// which must be the type of a concrete [InheritedWidget] subclass.
2269
2278
///
2270
2279
/// Returns null if no such element is found.
2271
2280
///
2281
+ /// {@template flutter.widgets.BuildContext.getElementForInheritedWidgetOfExactType}
2272
2282
/// Calling this method is O(1) with a small constant factor.
2273
2283
///
2274
2284
/// This method does not establish a relationship with the target in the way
@@ -2280,11 +2290,13 @@ abstract class BuildContext {
2280
2290
/// [dependOnInheritedWidgetOfExactType] in [State.didChangeDependencies] . It is
2281
2291
/// safe to use this method from [State.deactivate] , which is called whenever
2282
2292
/// the widget is removed from the tree.
2293
+ /// {@endtemplate}
2283
2294
InheritedElement ? getElementForInheritedWidgetOfExactType <T extends InheritedWidget >();
2284
2295
2285
2296
/// Returns the nearest ancestor widget of the given type `T` , which must be the
2286
2297
/// type of a concrete [Widget] subclass.
2287
2298
///
2299
+ /// {@template flutter.widgets.BuildContext.findAncestorWidgetOfExactType}
2288
2300
/// In general, [dependOnInheritedWidgetOfExactType] is more useful, since
2289
2301
/// inherited widgets will trigger consumers to rebuild when they change. This
2290
2302
/// method is appropriate when used in interaction event handlers (e.g.
@@ -2306,11 +2318,13 @@ abstract class BuildContext {
2306
2318
///
2307
2319
/// Returns null if a widget of the requested type does not appear in the
2308
2320
/// ancestors of this context.
2321
+ /// {@endtemplate}
2309
2322
T ? findAncestorWidgetOfExactType <T extends Widget >();
2310
2323
2311
2324
/// Returns the [State] object of the nearest ancestor [StatefulWidget] widget
2312
2325
/// that is an instance of the given type `T` .
2313
2326
///
2327
+ /// {@template flutter.widgets.BuildContext.findAncestorStateOfType}
2314
2328
/// This should not be used from build methods, because the build context will
2315
2329
/// not be rebuilt if the value that would be returned by this method changes.
2316
2330
/// In general, [dependOnInheritedWidgetOfExactType] is more appropriate for such
@@ -2332,6 +2346,7 @@ abstract class BuildContext {
2332
2346
/// because the widget tree is no longer stable at that time. To refer to
2333
2347
/// an ancestor from one of those methods, save a reference to the ancestor
2334
2348
/// by calling [findAncestorStateOfType] in [State.didChangeDependencies] .
2349
+ /// {@endtemplate}
2335
2350
///
2336
2351
/// {@tool snippet}
2337
2352
///
@@ -2344,17 +2359,20 @@ abstract class BuildContext {
2344
2359
/// Returns the [State] object of the furthest ancestor [StatefulWidget] widget
2345
2360
/// that is an instance of the given type `T` .
2346
2361
///
2362
+ /// {@template flutter.widgets.BuildContext.findRootAncestorStateOfType}
2347
2363
/// Functions the same way as [findAncestorStateOfType] but keeps visiting subsequent
2348
2364
/// ancestors until there are none of the type instance of `T` remaining.
2349
2365
/// Then returns the last one found.
2350
2366
///
2351
2367
/// This operation is O(N) as well though N is the entire widget tree rather than
2352
2368
/// a subtree.
2369
+ /// {@endtemplate}
2353
2370
T ? findRootAncestorStateOfType <T extends State >();
2354
2371
2355
2372
/// Returns the [RenderObject] object of the nearest ancestor [RenderObjectWidget] widget
2356
2373
/// that is an instance of the given type `T` .
2357
2374
///
2375
+ /// {@template flutter.widgets.BuildContext.findAncestorRenderObjectOfType}
2358
2376
/// This should not be used from build methods, because the build context will
2359
2377
/// not be rebuilt if the value that would be returned by this method changes.
2360
2378
/// In general, [dependOnInheritedWidgetOfExactType] is more appropriate for such
@@ -2371,13 +2389,16 @@ abstract class BuildContext {
2371
2389
/// because the widget tree is no longer stable at that time. To refer to
2372
2390
/// an ancestor from one of those methods, save a reference to the ancestor
2373
2391
/// by calling [findAncestorRenderObjectOfType] in [State.didChangeDependencies] .
2392
+ /// {@endtemplate}
2374
2393
T ? findAncestorRenderObjectOfType <T extends RenderObject >();
2375
2394
2376
2395
/// 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.
2381
2402
///
2382
2403
/// This is useful for inspecting the widget tree.
2383
2404
///
@@ -2387,10 +2408,12 @@ abstract class BuildContext {
2387
2408
/// because the element tree is no longer stable at that time. To refer to
2388
2409
/// an ancestor from one of those methods, save a reference to the ancestor
2389
2410
/// by calling [visitAncestorElements] in [State.didChangeDependencies] .
2390
- void visitAncestorElements (bool Function (Element element) visitor);
2411
+ /// {@endtemplate}
2412
+ void visitAncestorElements (ConditionalElementVisitor visitor);
2391
2413
2392
2414
/// Walks the children of this widget.
2393
2415
///
2416
+ /// {@template flutter.widgets.BuildContext.visitChildElements}
2394
2417
/// This is useful for applying changes to children after they are built
2395
2418
/// without waiting for the next frame, especially if the children are known,
2396
2419
/// and especially if there is exactly one child (as is always the case for
@@ -2408,6 +2431,7 @@ abstract class BuildContext {
2408
2431
/// significantly cheaper to use an [InheritedWidget] and have the descendants
2409
2432
/// pull data down, than it is to use [visitChildElements] recursively to push
2410
2433
/// data down to them.
2434
+ /// {@endtemplate}
2411
2435
void visitChildElements (ElementVisitor visitor);
2412
2436
2413
2437
/// Start bubbling this notification at the given build context.
@@ -4452,7 +4476,7 @@ abstract class Element extends DiagnosticableTree implements BuildContext {
4452
4476
}
4453
4477
4454
4478
@override
4455
- void visitAncestorElements (bool Function ( Element element) visitor) {
4479
+ void visitAncestorElements (ConditionalElementVisitor visitor) {
4456
4480
assert (_debugCheckStateIsActiveForAncestorLookup ());
4457
4481
Element ? ancestor = _parent;
4458
4482
while (ancestor != null && visitor (ancestor)) {
0 commit comments