BuildContext翻譯

///小部件樹中小部件位置的句柄。
///
///這個類提供了一組可以從StatelessWidget中使用的方法柄粹。[State]對象上的方法。
///
/// [BuildContext]對象被傳遞給[WidgetBuilder]函數(shù)(例如[StatelessWidget.build]),并且可以從[State. build]中獲得。上下文)成員妨猩。
///一些靜態(tài)函數(shù)(例如:[showDialog], [Theme.]的秽褒,等等)也獲取構(gòu)建上下文壶硅,以便它們可以代表調(diào)用小部件,或者獲取特定于給定上下文的數(shù)據(jù)销斟。
///
///每個小部件都有自己的[BuildContext]庐椒,它成為[StatelessWidget. widget返回的小部件的父部件。建立]或[狀態(tài)]蚂踊。構(gòu)建函數(shù)约谈。
///(類似地,[RenderObjectWidget]的任何子元素的父元素。)
///
///特別地棱诱,這意味著在一個構(gòu)建方法中泼橘,構(gòu)建方法的小部件的構(gòu)建上下文與該構(gòu)建方法返回的小部件的構(gòu)建上下文不相同。這可能會導(dǎo)致一些棘手的情況迈勋。
///例如炬灭,[Theme.of(context)]查找給定構(gòu)建上下文的最近的封閉[Theme]。如果小部件Q的構(gòu)建方法在其返回的小部件樹中包含[Theme]靡菇,并嘗試使用[Theme]重归。]傳遞自己的上下文,Q的構(gòu)建方法將找不到[Theme]對象镰官。相反提前,它會找到任何[Theme]是小部件q的祖先。如果需要返回樹的子部分的構(gòu)建上下文泳唠,可以使用[Builder]小部件:傳遞給[Builder.]的構(gòu)建上下文狈网。builder]回調(diào)函數(shù)將是[builder]本身的回調(diào)函數(shù)。
///
///例如笨腥,在下面的代碼片段中拓哺,[ScaffoldState. html]在構(gòu)建方法本身創(chuàng)建的[Scaffold]小部件上調(diào)用showBottomSheet方法。如果沒有使用[Builder]脖母,而是使用了構(gòu)建方法本身的' context '參數(shù)士鸥,則不會找到[Scaffold],并且[腳手架谆级。函數(shù)返回null烤礁。

@override
Widget build(BuildContext context) {
  // here, Scaffold.of(context) returns null
  return Scaffold(
    appBar: const AppBar(title: Text('Demo')),
    body: Builder(
      builder: (BuildContext context) {
        return TextButton(
          child: const Text('BUTTON'),
          onPressed: () {
            Scaffold.of(context).showBottomSheet<void>(
              (BuildContext context) {
                return Container(
                  alignment: Alignment.center,
                  height: 200,
                  color: Colors.amber,
                  child: Center(
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      children: <Widget>[
                        const Text('BottomSheet'),
                        ElevatedButton(
                          child: const Text('Close BottomSheet'),
                          onPressed: () {
                            Navigator.pop(context),
                          },
                        )
                      ],
                    ),
                  ),
                );
              },
            );
          },
        );
      },
    )
  );
}

///
///當(dāng)小部件在樹中移動時(shí),特定小部件的[BuildContext]可以隨時(shí)間改變位置肥照。因此脚仔,從該類的方法返回的值不應(yīng)該在單個同步函數(shù)執(zhí)行之后被緩存。
///
/// {@youtube 560 315 https://www.youtube.com/watch?v=rIaaH87z1-g}
///
/// [BuildContext]對象實(shí)際上是[Element]對象舆绎。[BuildContext]接口用于阻止對[Element]對象的直接操作鲤脏。

abstract class BuildContext {
  /// [Element]的當(dāng)前配置,即[BuildContext]吕朵。
  Widget get widget;

  /// 這個上下文的[BuildOwner]猎醇。[BuildOwner]負(fù)責(zé)管理這個上下文的渲染管道。
  BuildOwner? get owner;

  /// [widget]當(dāng)前是否正在更新小部件或呈現(xiàn)樹努溃。
  ///
  /// 對于[StatelessWidget]和[StatelessWidget]硫嘶,當(dāng)它們各自的構(gòu)建方法正在執(zhí)行時(shí),此標(biāo)志為真梧税。
  /// [RenderObjectWidget]在創(chuàng)建或配置相關(guān)的[RenderObject]時(shí)將此設(shè)置為true音半。
  /// 其他[Widget]類型可以將此設(shè)置為true则拷,用于它們生命周期中概念相似的階段。
  ///
  /// 當(dāng)這為真時(shí)曹鸠,[widget]通過調(diào)用[dependOnInheritedElement]或[dependOnInheritedWidgetOfExactType]建立對[InheritedWidget]的依賴是安全的。
  ///
  /// 在釋放模式下訪問該標(biāo)志無效斥铺。
  bool get debugDoingBuild;

  /// The current [RenderObject] for the widget. If the widget is a
  /// [RenderObjectWidget], this is the render object that the widget created
  /// for itself. Otherwise, it is the render object of the first descendant
  /// [RenderObjectWidget].
  ///
  /// This method will only return a valid result after the build phase is
  /// complete. It is therefore not valid to call this from a build method.
  /// It should only be called from interaction event handlers (e.g.
  /// gesture callbacks) or layout or paint callbacks. It is also not valid to
  /// call if [State.mounted] returns false.
  ///
  /// If the render object is a [RenderBox], which is the common case, then the
  /// size of the render object can be obtained from the [size] getter. This is
  /// only valid after the layout phase, and should therefore only be examined
  /// from paint callbacks or interaction event handlers (e.g. gesture
  /// callbacks).
  ///
  /// For details on the different phases of a frame, see the discussion at
  /// [WidgetsBinding.drawFrame].
  ///
  /// Calling this method is theoretically relatively expensive (O(N) in the
  /// depth of the tree), but in practice is usually cheap because the tree
  /// usually has many render objects and therefore the distance to the nearest
  /// render object is usually short.

  ///小部件的當(dāng)前[RenderObject]彻桃。如果小部件是[RenderObjectWidget],這是小部件為自己創(chuàng)建的渲染對象晾蜘。否則邻眷,它是第一個后代[RenderObjectWidget]的渲染對象。
  ///
  ///該方法只在構(gòu)建階段完成后才返回有效結(jié)果剔交。因此肆饶,從構(gòu)建方法中調(diào)用它是無效的。
  ///它只能從交互事件處理程序(例如手勢回調(diào))或布局或油漆回調(diào)調(diào)用岖常。調(diào)用if [State]也是無效的驯镊。Mounted]返回false。
  ///
  ///如果渲染對象是[RenderBox],這是常見的情況,那么渲染對象的大小可以從[size] getter中獲得残吩。這只在布局階段之后有效税迷,因此應(yīng)該只從油漆回調(diào)或交互事件處理程序(例如,手勢回調(diào))中檢查目代。
  ///
  ///關(guān)于幀的不同階段的詳細(xì)信息,請參見[WidgetsBinding.drawFrame]中的討論。
  ///
  ///調(diào)用這個方法理論上是比較昂貴的(在樹的深度中是O(N))裆馒,但實(shí)際上通常是便宜的,因?yàn)闃渫ǔS泻芏噤秩緦ο筘で樱虼说阶罱匿秩緦ο蟮木嚯x通常很短喷好。
  RenderObject? findRenderObject();

  /// The size of the [RenderBox] returned by [findRenderObject].
  ///
  /// This getter will only return a valid result after the layout phase is
  /// complete. It is therefore not valid to call this from a build method.
  /// It should only be called from paint callbacks or interaction event
  /// handlers (e.g. gesture callbacks).
  ///
  /// For details on the different phases of a frame, see the discussion at
  /// [WidgetsBinding.drawFrame].
  ///
  /// This getter will only return a valid result if [findRenderObject] actually
  /// returns a [RenderBox]. If [findRenderObject] returns a render object that
  /// is not a subtype of [RenderBox] (e.g., [RenderView]), this getter will
  /// throw an exception in debug mode and will return null in release mode.
  ///
  /// Calling this getter is theoretically relatively expensive (O(N) in the
  /// depth of the tree), but in practice is usually cheap because the tree
  /// usually has many render objects and therefore the distance to the nearest
  /// render object is usually short.

  /// [findRenderObject]返回的[RenderBox]的大小。
  ///
  ///該getter只在布局階段完成后才返回有效結(jié)果响逢。因此绒窑,從構(gòu)建方法中調(diào)用它是無效的。
  ///它只能從油漆回調(diào)或交互事件處理程序(例如舔亭,手勢回調(diào))調(diào)用些膨。
  ///
  ///關(guān)于幀的不同階段的詳細(xì)信息,請參見[WidgetsBinding.drawFrame]中的討論钦铺。
  ///
  ///只有當(dāng)[findRenderObject]返回一個[RenderBox]時(shí)订雾,getter才會返回一個有效的結(jié)果。如果[findRenderObject]返回的渲染對象不是[RenderBox]的子類型(例如矛洞,[RenderView])洼哎,此getter將在調(diào)試模式拋出異常烫映,并在發(fā)布模式返回null。
  ///
  ///調(diào)用這個getter在理論上是相對昂貴的(在樹的深度中是O(N))噩峦,但在實(shí)踐中通常是便宜的锭沟,因?yàn)闃渫ǔS性S多渲染對象,因此到最近的渲染對象的距離通常很短识补。  
  Size? get size;

  ///將這個構(gòu)建上下文注冊到[祖宗]族淮,這樣當(dāng)[祖宗]的小部件改變時(shí),這個構(gòu)建上下文就會被重新構(gòu)建凭涂。
  /// Registers this build context with [ancestor] such that when [ancestor]'s widget changes this build context is rebuilt.
  ///
  ///返回'祖宗.widget '祝辣。
  /// Returns `ancestor.widget`.
  ///
  ///這個方法很少被直接調(diào)用。大多數(shù)應(yīng)用程序應(yīng)該使用[dependOnInheritedWidgetOfExactType]切油,它在找到適當(dāng)?shù)腫InheritedElement]祖先后調(diào)用此方法蝙斜。 
  /// This method is rarely called directly.  Most applications should use [dependOnInheritedWidgetOfExactType], which calls this method after finding the appropriate [InheritedElement] ancestor.
  ///
  ///當(dāng)[dependOnInheritedWidgetOfExactType]可以被調(diào)用的所有限定條件也適用于這個方法。 
  /// All of the qualifications about when [dependOnInheritedWidgetOfExactType] can be called apply to this method as well.  
  InheritedWidget dependOnInheritedElement(InheritedElement ancestor, { Object aspect });

  ///獲取最近的給定類型' T '的小部件澎胡,它必須是具體的[InheritedWidget]子類的類型孕荠,并將此構(gòu)建上下文注冊到該小部件,以便當(dāng)該小部件更改時(shí)(或引入該類型的新小部件滤馍,或小部件消失)岛琼,此構(gòu)建上下文將重新構(gòu)建,以便它可以從該小部件獲取新值巢株。
  /// Obtains the nearest widget of the given type `T`, which must be the type of a concrete [InheritedWidget] subclass, and registers this build context with that widget such that when that widget changes (or a new widget of that type is introduced, or the widget goes away), this build context is rebuilt so that it can obtain new values from that widget.
  ///
  ///這通常由' of() '靜態(tài)方法隱式調(diào)用槐瑞,例如[Theme.of]。
  /// This is typically called implicitly from `of()` static methods, e.g.[Theme.of].
  ///
  ///該方法不能從小部件構(gòu)造函數(shù)或[State. conf]調(diào)用阁苞。initState]方法困檩,因?yàn)槿绻^承的值發(fā)生變化,這些方法將不會被再次調(diào)用那槽。為了確保小部件在繼承的值改變時(shí)正確地更新自己悼沿,只能從構(gòu)建方法、布局和繪制回調(diào)或從[State.didChangeDependencies]調(diào)用這個(直接或間接)骚灸。
  /// This method should not be called from widget constructors or from [State.    initState] methods, because those methods would not get called again if the inherited value were to change.     To ensure that the widget correctly updates itself when the inherited value changes, only call this (directly or indirectly) from build methods, layout and paint callbacks, or from [State.didChangeDependencies].
  ///
  ///不應(yīng)該從[State.]調(diào)用此方法糟趾。因?yàn)樵貥湓谀莻€時(shí)候不再穩(wěn)定。要從該方法引用一個祖先甚牲,請?jiān)赱State.didChangeDependencies]中保存對祖先的引用义郑。
  ///從[State.deactivate]中使用這個方法是安全的,當(dāng)小部件從樹中移除時(shí)丈钙,這個方法就會被調(diào)用非驮。
  /// This method should not be called from [State.    dispose] because the element tree is no longer stable at that time.     To refer to an ancestor from that method, save a reference to the ancestor in [State.didChangeDependencies].
  /// It is safe to use this method from [State.deactivate], which is called whenever the widget is removed from the tree.
  ///
  ///也可以從交互事件處理程序(例如,手勢回調(diào))或計(jì)時(shí)器調(diào)用此方法雏赦,如果該值不會被緩存并在以后重用劫笙,則只獲取一次該值芙扎。
  /// It is also possible to call this method from interaction event handlers (e.g. gesture callbacks) or timers, to obtain a value once, if that value is not going to be cached and reused later.
  ///
  ///調(diào)用這個方法是O(1)和一個小的常數(shù)因子,但會導(dǎo)致小部件被更頻繁地重建填大。
  /// Calling this method is O(1) with a small constant factor, but will lead to the widget being rebuilt more often.
  ///
  ///一旦一個小部件通過調(diào)用這個方法注冊了一個特定類型的依賴項(xiàng)戒洼,它將被重新構(gòu)建,并且[State. conf]每當(dāng)與該小部件相關(guān)的更改發(fā)生時(shí)允华,都會調(diào)用didChangeDependencies]施逾,直到下次小部件或其祖先之一被移動時(shí)(例如,因?yàn)樘砑踊騽h除了一個祖先)例获。
  /// Once a widget registers a dependency on a particular type by calling this method, it will be rebuilt, and [State.    didChangeDependencies] will be called, whenever changes occur relating to that widget until the next time the widget or one of its ancestors is moved (for example, because an ancestor is added or removed).
  ///
  ///只有當(dāng)' T '是一個支持部分更新的[InheritedWidget]子類時(shí),才會使用[aspect]參數(shù)曹仗,比如[InheritedModel]榨汤。它指定此上下文所依賴的繼承小部件的哪個“方面”。
  /// The [aspect] parameter is only used when `T` is an [InheritedWidget] subclasses that supports partial updates, like [InheritedModel].     It specifies what "aspect" of the inherited widget this context depends on.
  T? dependOnInheritedWidgetOfExactType<T extends InheritedWidget>({ Object? aspect });

  ///獲取與給定類型' T '的最近的小部件對應(yīng)的元素怎茫,該小部件必須是具體的[InheritedWidget]子類的類型收壕。
  /// Obtains the element corresponding to the nearest widget of the given type `T`, which must be the type of a concrete [InheritedWidget] subclass.
  ///
  ///如果沒有找到這樣的元素則返回null。
  /// Returns null if no such element is found.
  ///
  ///調(diào)用此方法是O(1)與一個小常數(shù)因子轨蛤。
  /// Calling this method is O(1) with a small constant factor.
  ///
  ///該方法不像[dependOnInheritedWidgetOfExactType]那樣與目標(biāo)建立關(guān)系蜜宪。
  /// This method does not establish a relationship with the target in the way that [dependOnInheritedWidgetOfExactType] does.
  ///
  ///不應(yīng)該從[State.]調(diào)用此方法。因?yàn)樵貥湓谀莻€時(shí)候不再穩(wěn)定祥山。要從該方法引用一個祖先圃验,請?jiān)赱State.didChangeDependencies]中調(diào)用[dependOnInheritedWidgetOfExactType]來保存對祖先的引用。從[State.deactivate]中使用這個方法是安全的缝呕,每當(dāng)小部件從樹中移除時(shí)澳窑,就會調(diào)用這個方法。
  /// This method should not be called from [State.dispose] because the element tree is no longer stable at that time. To refer to an ancestor from that method, save a reference to the ancestor by calling [dependOnInheritedWidgetOfExactType] in [State.didChangeDependencies]. It is safe to use this method from [State.deactivate], which is called whenever the widget is removed from the tree.
  InheritedElement? getElementForInheritedWidgetOfExactType<T extends InheritedWidget>();

  ///返回給定類型' T '的最近的祖先小部件供常,它必須是一個具體的[widget]子類的類型摊聋。
  /// Returns the nearest ancestor widget of the given type `T`, which must be the type of a concrete [Widget] subclass.
  ///
  ///一般來說,[dependOnInheritedWidgetOfExactType]更有用栈暇,因?yàn)槔^承的小部件會在更改時(shí)觸發(fā)消費(fèi)者重新構(gòu)建麻裁。此方法適用于交互事件處理程序(例如,手勢回調(diào))或執(zhí)行一次性任務(wù)源祈,例如斷言您是否擁有特定類型的小部件作為祖先煎源。Widget的構(gòu)建方法的返回值不應(yīng)該依賴于此方法返回的值,因?yàn)槿绻朔椒ǖ姆祷刂蛋l(fā)生變化新博,構(gòu)建上下文將不會重新構(gòu)建薪夕。這可能會導(dǎo)致構(gòu)建方法中使用的數(shù)據(jù)發(fā)生變化,但小部件沒有被重新構(gòu)建赫悄。
  /// In general, [dependOnInheritedWidgetOfExactType] is more useful, since inherited widgets will trigger consumers to rebuild when they change.  This method is appropriate when used in interaction event handlers (e.g. gesture callbacks) or for performing one-off tasks such as asserting that you have or don't have a widget of a specific type as an ancestor.  The return value of a Widget's build method should not depend on the value returned by this method, because the build context will not rebuild if the return value of this method changes.  This could lead to a situation where data used in the build method changes, but the widget is not rebuilt.
  ///
  ///調(diào)用這個方法是相對昂貴的(O(N)在樹的深度)原献。只有在這個小部件到所需祖先的距離已知很小且有界時(shí)才調(diào)用此方法馏慨。
  /// Calling this method is relatively expensive (O(N) in the depth of the tree).  Only call this method if the distance from this widget to the desired ancestor is known to be small and bounded.
  ///
  ///該方法不能從[State.deactivate]或[State.deactivate]調(diào)用。因?yàn)樾〔考湓谀莻€時(shí)候不再穩(wěn)定姑隅。要從這些方法中引用一個祖先写隶,請?jiān)赱State.didChangeDependencies]中調(diào)用[find祖宗widgetofexacttype]來保存對祖先的引用。
  /// This method should not be called from [State.deactivate] or [State. dispose] because the widget tree is no longer stable at that time.  To refer to an ancestor from one of those methods, save a reference to the ancestor by calling [findAncestorWidgetOfExactType] in [State.didChangeDependencies].
  ///
  ///如果請求類型的小部件沒有出現(xiàn)在此上下文的祖先中讲仰,則返回null慕趴。
  /// Returns null if a widget of the requested type does not appear in the ancestors of this context.
  T? findAncestorWidgetOfExactType<T extends Widget>();

  ///返回最近的祖先部件[StatefulWidget]的[State]對象,該部件是給定類型' T '的實(shí)例鄙陡。
  /// Returns the [State] object of the nearest ancestor [StatefulWidget] widget that is an instance of the given type `T`.
  ///
  ///不應(yīng)該從構(gòu)建方法中使用冕房,因?yàn)槿绻摲椒ǚ祷氐闹蛋l(fā)生變化,構(gòu)建上下文將不會被重新構(gòu)建趁矾。
  /// This should not be used from build methods, because the build context will not be rebuilt if the value that would be returned by this method changes.
  ///
  ///一般來說耙册,[dependOnInheritedWidgetOfExactType]更適合這種情況。此方法對于一次性更改祖先小部件的狀態(tài)非常有用毫捣,例如详拙,導(dǎo)致祖先滾動列表將構(gòu)建上下文的小部件滾動到視圖中,或移動焦點(diǎn)以響應(yīng)用戶交互蔓同。
  /// In general, [dependOnInheritedWidgetOfExactType] is more appropriate for such cases. This method is useful for changing the state of an ancestor widget in a one-off manner, for example, to cause an ancestor scrolling list to scroll this build context's widget into view, or to move the focus in response to user interaction.
  ///
  ///但是饶辙,一般情況下,考慮使用回調(diào)來觸發(fā)祖先中的有狀態(tài)更改斑粱,而不是使用此方法所暗示的命令式樣式弃揽。這通常會導(dǎo)致代碼更具可維護(hù)性和可重用性,因?yàn)樗鼘⑿〔考舜朔蛛x珊佣。
  /// In general, though, consider using a callback that triggers a stateful change in the ancestor rather than using the imperative style implied by this method. This will usually lead to more maintainable and reusable code since it decouples widgets from each other.
  ///
  ///調(diào)用這個方法是相對昂貴的(O(N)在樹的深度)蹋宦。只有在這個小部件到所需祖先的距離已知很小且有界時(shí)才調(diào)用此方法。
  /// Calling this method is relatively expensive (O(N) in the depth of the tree). Only call this method if the distance from this widget to the desired ancestor is known to be small and bounded.
  ///
  ///該方法不能從[State.deactivate]或[State.deactivate]調(diào)用咒锻。因?yàn)樾〔考湓谀莻€時(shí)候不再穩(wěn)定冷冗。要從這些方法中引用一個祖先,請通過調(diào)用[State.didChangeDependencies]中的[find祖宗stateoftype]來保存對祖先的引用惑艇。
  /// This method should not be called from [State.deactivate] or [State.dispose] because the widget tree is no longer stable at that time. To refer to an ancestor from one of those methods, save a reference to the ancestor by calling [findAncestorStateOfType] in [State.didChangeDependencies].
  ///
  /// {@tool snippet}
  ///
  /// ```dart
  /// ScrollableState? scrollable = context.findAncestorStateOfType<ScrollableState>();
  /// ```
  /// {@end-tool}
  T? findAncestorStateOfType<T extends State>();

  ///返回最遠(yuǎn)祖先[StatefulWidget]小部件的[State]對象蒿辙,該小部件是給定類型' T '的實(shí)例。
  /// Returns the [State] object of the furthest ancestor [StatefulWidget] widget that is an instance of the given type `T`.
  ///
  ///與[findancestry orstateoftype]的功能相同滨巴,但一直訪問后續(xù)的祖先思灌,直到?jīng)]有' T '的類型實(shí)例。
  ///返回找到的最后一個恭取。
  /// Functions the same way as [findAncestorStateOfType] but keeps visiting subsequent ancestors until there are none of the type instance of `T` remaining.
  /// Then returns the last one found.
  ///
  ///這個操作也是O(N)泰偿,盡管N是整個小部件樹而不是子樹。
  /// This operation is O(N) as well though N is the entire widget tree rather than a subtree.
  T? findRootAncestorStateOfType<T extends State>();

  ///返回最近的祖先[RenderObjectWidget]小部件的[RenderObject]對象蜈垮,該小部件是給定類型' T '的實(shí)例耗跛。
  /// Returns the [RenderObject] object of the nearest ancestor [RenderObjectWidget] widget that is an instance of the given type `T`.
  ///
  ///不應(yīng)該從構(gòu)建方法中使用裕照,因?yàn)槿绻摲椒ǚ祷氐闹蛋l(fā)生變化,構(gòu)建上下文將不會被重新構(gòu)建调塌。
  ///一般來說晋南,[dependOnInheritedWidgetOfExactType]更適合這種情況。這種方法只在一些復(fù)雜的情況下有用羔砾,比如小部件需要導(dǎo)致一個祖先改變它的布局或繪制行為负间。例如,它被[Material]使用姜凄,這樣[InkWell]小部件就可以觸發(fā)[Material]實(shí)際渲染對象上的墨水飛濺政溃。
  /// This should not be used from build methods, because the build context will not be rebuilt if the value that would be returned by this method changes.
  /// In general, [dependOnInheritedWidgetOfExactType] is more appropriate for such cases. This method is useful only in esoteric cases where a widget needs to cause an ancestor to change its layout or paint behavior. For example, it is used by [Material] so that [InkWell] widgets can trigger the ink splash on the [Material]'s actual render object.
  ///
  ///調(diào)用這個方法是相對昂貴的(O(N)在樹的深度)。只有在這個小部件到所需祖先的距離已知很小且有界時(shí)才調(diào)用此方法态秧。
  /// Calling this method is relatively expensive (O(N) in the depth of the tree). Only call this method if the distance from this widget to the desired ancestor is known to be small and bounded.
  ///
  ///該方法不能從[State.deactivate]或[State.deactivate]調(diào)用玩祟。因?yàn)樾〔考湓谀莻€時(shí)候不再穩(wěn)定。要從這些方法中引用一個祖先屿聋,請?jiān)赱State.didChangeDependencies]中調(diào)用[find祖宗renderobjectoftype]來保存對祖先的引用。
  /// This method should not be called from [State.deactivate] or [State.dispose] because the widget tree is no longer stable at that time. To refer to an ancestor from one of those methods, save a reference to the ancestor by calling [findAncestorRenderObjectOfType] in [State.didChangeDependencies].
  T? findAncestorRenderObjectOfType<T extends RenderObject>();

  ///遍歷祖先鏈藏鹊,從這個構(gòu)建上下文的小部件的父組件開始润讥,調(diào)用每個祖先的參數(shù)∨坦眩回調(diào)被賦予一個對祖先小部件對應(yīng)的[Element]對象的引用楚殿。遍歷在到達(dá)根小部件或回調(diào)返回false時(shí)停止「吞担回調(diào)不能返回null脆粥。
  /// Walks the ancestor chain, starting with the parent of this build context's widget, invoking the argument for each ancestor. The callback is given a reference to the ancestor widget's corresponding [Element] object. The walk stops when it reaches the root widget or when the callback returns false. The callback must not return null.
  ///
  ///這對于檢查小部件樹很有用。
  /// This is useful for inspecting the widget tree.
  ///
  ///調(diào)用這個方法是相對昂貴的(O(N)在樹的深度)影涉。
  /// Calling this method is relatively expensive (O(N) in the depth of the tree).
  ///
  ///該方法不能從[State.deactivate]或[State.deactivate]調(diào)用变隔。因?yàn)樵貥湓谀莻€時(shí)候不再穩(wěn)定。要從這些方法中引用一個祖先蟹倾,請調(diào)用[State.didChangeDependencies]中的[visit祖宗lements]來保存對祖先的引用匣缘。
  /// This method should not be called from [State.deactivate] or [State.dispose] because the element tree is no longer stable at that time. To refer to an ancestor from one of those methods, save a reference to the ancestor by calling [visitAncestorElements] in [State.didChangeDependencies].
  void visitAncestorElements(bool Function(Element element) visitor);

  ///遍歷這個小部件的子部件。
  /// Walks the children of this widget.
  ///
  ///這對于在構(gòu)建子代之后應(yīng)用更改而不等待下一幀是有用的鲜棠,特別是如果子代是已知的肌厨,特別是如果只有一個子代(就像對于[StatefulWidget]s或[StatelessWidget]s總是這樣)。
  /// This is useful for applying changes to children after they are built without waiting for the next frame, especially if the children are known, and especially if there is exactly one child (as is always the case for [StatefulWidget]s or [StatelessWidget]s).
  ///
  ///對于與[StatelessWidget]s或[StatelessWidget]s (O(1)對應(yīng)的構(gòu)建上下文來說豁陆,調(diào)用這個方法非常便宜柑爸,因?yàn)橹挥幸粋€孩子)。
  /// Calling this method is very cheap for build contexts that correspond to [StatefulWidget]s or [StatelessWidget]s (O(1), since there's only one child).
  ///
  ///調(diào)用此方法可能會導(dǎo)致構(gòu)建上下文與[RenderObjectWidget]s (O(N) in number of child)相對應(yīng)盒音。
  /// Calling this method is potentially expensive for build contexts that correspond to [RenderObjectWidget]s (O(N) in the number of children).
  ///
  ///遞歸調(diào)用這個方法是非常昂貴的(O(N)在后代的數(shù)量)表鳍,如果可能的話應(yīng)該避免馅而。通常,使用[InheritedWidget]并讓后代向下拉數(shù)據(jù)要比使用[visitChildElements]遞歸地將數(shù)據(jù)向下推到后代要便宜得多进胯。
  /// Calling this method recursively is extremely expensive (O(N) in the number of descendants), and should be avoided if possible.  Generally it is significantly cheaper to use an [InheritedWidget] and have the descendants pull data down, than it is to use [visitChildElements] recursively to push data down to them.
  void visitChildElements(ElementVisitor visitor);

  ///在給定的構(gòu)建上下文開始冒泡此通知用爪。
  /// Start bubbling this notification at the given build context.
  ///
  ///通知將被傳遞給任何[NotificationListener]小部件,這些小部件具有適當(dāng)?shù)念愋蛥?shù)胁镐,是給定[BuildContext]的祖先偎血。
  /// The notification will be delivered to any [NotificationListener] widgets with the appropriate type parameters that are ancestors of the given [BuildContext].
  void dispatchNotification(Notification notification);

  ///返回與當(dāng)前構(gòu)建上下文相關(guān)的[Element]的描述。
  /// Returns a description of the [Element] associated with the current build context.
  ///
  ///“name”通常是類似于“The element being rebuild was”的東西盯漂。
  /// The `name` is typically something like "The element being rebuilt was".
  ///
  /// See also:
  ///
  /// * [Element.describeElements]颇玷,可以用來描述一個元素列表。
  ///  * [Element.describeElements], which can be used to describe a list of elements.
  DiagnosticsNode describeElement(String name, {DiagnosticsTreeStyle style = DiagnosticsTreeStyle.errorProperty});

  ///返回與當(dāng)前構(gòu)建上下文相關(guān)的[Widget]的描述就缆。
  /// Returns a description of the [Widget] associated with the current build context.
  ///
  ///“名稱”通常是類似于“正在重建的小部件是”這樣的東西帖渠。
  /// The `name` is typically something like "The widget being rebuilt was".
  DiagnosticsNode describeWidget(String name, {DiagnosticsTreeStyle style = DiagnosticsTreeStyle.errorProperty});

  ///添加當(dāng)前構(gòu)建上下文祖先樹中缺少的特定類型小部件的描述。
  /// Adds a description of a specific type of widget missing from the current build context's ancestry tree.
  ///
  ///你可以在[debugCheckHasMaterial]中找到使用這個方法的例子竭宰。
  /// You can find an example of using this method in [debugCheckHasMaterial].
  List<DiagnosticsNode> describeMissingAncestor({ required Type expectedAncestorType });

  ///將所有權(quán)鏈的描述從一個特定的[Element]添加到錯誤報(bào)告中空郊。
  /// Adds a description of the ownership chain from a specific [Element] to the error report.
  ///
  /// 所有權(quán)鏈用于調(diào)試元素的源代碼。
  /// The ownership chain is useful for debugging the source of an element.


  DiagnosticsNode describeOwnershipChain(String name);
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末切揭,一起剝皮案震驚了整個濱河市狞甚,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌廓旬,老刑警劉巖哼审,帶你破解...
    沈念sama閱讀 222,590評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異孕豹,居然都是意外死亡涩盾,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評論 3 399
  • 文/潘曉璐 我一進(jìn)店門励背,熙熙樓的掌柜王于貴愁眉苦臉地迎上來春霍,“玉大人,你說我怎么就攤上這事叶眉≈粘” “怎么了?”我有些...
    開封第一講書人閱讀 169,301評論 0 362
  • 文/不壞的土叔 我叫張陵竟闪,是天一觀的道長离福。 經(jīng)常有香客問我,道長炼蛤,這世上最難降的妖魔是什么妖爷? 我笑而不...
    開封第一講書人閱讀 60,078評論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上絮识,老公的妹妹穿的比我還像新娘绿聘。我一直安慰自己,他們只是感情好次舌,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,082評論 6 398
  • 文/花漫 我一把揭開白布熄攘。 她就那樣靜靜地躺著,像睡著了一般彼念。 火紅的嫁衣襯著肌膚如雪挪圾。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,682評論 1 312
  • 那天逐沙,我揣著相機(jī)與錄音哲思,去河邊找鬼。 笑死吩案,一個胖子當(dāng)著我的面吹牛棚赔,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播徘郭,決...
    沈念sama閱讀 41,155評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼靠益,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了残揉?” 一聲冷哼從身側(cè)響起捆毫,我...
    開封第一講書人閱讀 40,098評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎冲甘,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體途样,經(jīng)...
    沈念sama閱讀 46,638評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡江醇,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,701評論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了何暇。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片陶夜。...
    茶點(diǎn)故事閱讀 40,852評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖裆站,靈堂內(nèi)的尸體忽然破棺而出条辟,到底是詐尸還是另有隱情,我是刑警寧澤宏胯,帶...
    沈念sama閱讀 36,520評論 5 351
  • 正文 年R本政府宣布羽嫡,位于F島的核電站,受9級特大地震影響肩袍,放射性物質(zhì)發(fā)生泄漏杭棵。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,181評論 3 335
  • 文/蒙蒙 一氛赐、第九天 我趴在偏房一處隱蔽的房頂上張望魂爪。 院中可真熱鬧先舷,春花似錦、人聲如沸滓侍。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,674評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽撩笆。三九已至捺球,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間浇衬,已是汗流浹背懒构。 一陣腳步聲響...
    開封第一講書人閱讀 33,788評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留耘擂,地道東北人胆剧。 一個月前我還...
    沈念sama閱讀 49,279評論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像醉冤,于是被迫代替她去往敵國和親秩霍。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,851評論 2 361

推薦閱讀更多精彩內(nèi)容