0 x00 preface

BuildContext is an important part of Flutter, but there are few online articles about BuildContext, so this article will cover BuildContext.

0 x01 BuildContext is introduced

BuildContext, as the name implies, Build (Build Widget) Context, is to Build the application Context in the Widget.

So BuildContext only appears in two places:

  1. In the StatelessWidget.build method: Create the build method for the StatelessWidget
  2. In the State object: one is in the build method that creates the State object for the StatefulWidget, and the other is a State member variable

BuildContext is actually Element. BuildContext is abstracted to prevent direct operations on Element, so BuildContext is Element’s abstract class. All elements inherit from BuildContext.

Each Widget has a BuildContext.

BuildContext is a handle to the Widget’s location in the Widget tree.

0x02 accesses the BuildContext instance object

BuildContext is passed by WidgetBulder (e.g. Statelesswidget.build, state.build) methods;

BuildContext is also a member of State, and can be accessed directly from context within State

0 x03 BuildContext use

BuildContext is used primarily to get specified data from the context;

For example: Theme. Of (context) or showDialog(context: context,….) Both require BuildContext as an argument, where BuildContext is a representation of the Widget that calls these methods.

0x05 BuildContext Precautions

Each Widget has a BuildContext. If there is A Widget, there must be A build method called StatelessWidget.build or state. build. The build method creates the B Widget and returns it. Similarly, the BuildContext of A Widget is the parent of the BuildContext of B Widget.

Here’s an example to help you understand:

@override Widget build(BuildContext context) {// here, scaffold. of(context) returns null, // Before Scaffold is created, Scaffold.of(context) returns null because Scaffo// LD has not been created, so BuildContext has not been created. To get scaffoldbuildc //ontext when scaffoldis created, To use a Builderreturn Scaffold(
      appBar: AppBar(title: Text('Demo')),
      body: Builder(
        builder: (BuildContext context) {
          return FlatButton(
            child: Text('BUTTON'),
            onPressed: () {
              // here, Scaffold.of(context) returns the locally created Scaffold
              Scaffold.of(context).showSnackBar(SnackBar(
                content: Text('Hello.'))); }); })); }Copy the code

0x04 BuildContext Internal method resolution

The main reason why BuildContext can be used to get context data is because BuildContext has the following methods:

  1. – > InheritedElement ancestorInheritedElementForWidgetOfExactType (Type targetType)
Obtains the element corresponding to the nearest widget of the given type.which must be the type of a concrete InheritedWidget subclass. [...]
Copy the code
  1. – > RenderObject ancestorRenderObjectOfType (TypeMatcher matcher)
Returns the RenderObject object of the nearest ancestor RenderObjectWidget widget that matches the given TypeMatcher. [...].Copy the code
  1. AncestorStateOfType (TypeMatcher matcher) to the State
Returns the State object of the nearest ancestor StatefulWidget widget that matches the given TypeMatcher. [...]
Copy the code
  1. AncestorWidgetOfExactType (Type targetType) – > widgets
Returns the nearest ancestor widget of the given type.which must be the type of a concrete Widget subclass. [...]
Copy the code
  1. FindRenderObject () – > RenderObject
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. [...]
Copy the code
  1. InheritFromElement (InheritedElement ancestor, {Object aspect}) → InheritedWidget
Registers this build context with ancestor such that when ancestor's widget changes this build context is rebuilt. [...]
Copy the code
  1. InheritFromWidgetOfExactType (Type targetType, {Object aspect}) – > InheritedWidget
Obtains the nearest widget of the given type.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. [...]
Copy the code
  1. RootAncestorStateOfType (TypeMatcher matcher) to the State
Returns the State object of the furthest ancestor StatefulWidget widget that matches the given TypeMatcher. [...]
Copy the code
  1. VisitAncestorElements (bool Visitor (Element Element)) → void
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. [...]
Copy the code
  1. VisitChildElements (ElementVisitor visitor) – > void
Walks the children of this widget. [...]
Copy the code

0x05 References

Docs. Flutter. IO/flutter/wid…