Flutter BuildContext 探究

栏目: IOS · Android · 发布时间: 7年前

内容简介:所以

BuildContext 是Flutter的重要部分,但是目前网上讲 BuildContext 的文章太少了,所以本篇文章讲讲 BuildContext

0x01 BuildContext 介绍

BuildContext ,顾名思义,Build(构建Widget) Context(应用上下文),就是构建Widget中的应用上下文。

所以 BuildContext 只出现在两个地方:

  1. StatelessWidget.build 方法中:创建StatelessWidget的build方法
  2. State对象中:一个是创建StatefulWidget的State对象的build方法中,另一个是State的成员变量

BuildContext 实际是 Element , BuildContext 是为了阻止直接对 Element 操作而抽象出来的,所以 BuildContextElement 的抽象类,所有 Element 都继承自 BuildContext

每一个Widget都有一个 BuildContext

BuildContext 是Widget在Widget树中位置的句柄。

0x02 访问 BuildContext 实例对象

BuildContextWidgetBulder (例如:StatelessWidget.build, State.build)方法传递;

BuildContext 也是 State 的成员变量,在 State 内部,可以通过 context 直接访问

0x03 BuildContext 使用

BuildContext 的作用主要是通过上下文获取指定的数据;

例如: Theme.of(context) 或者 showDialog(context: context,....) 都需要 BuildContext 作为参数,这里的 BuildContext 就是调用这些方法的Widget的代表。

0x05 BuildContext 注意事项

每一个Widget都有一个 BuildContext 。假设有个A Widget,A Widget里肯定会有 StatelessWidget.build 或者 State.build 的build方法,build方法创建了 B Widget并返回,A Widget就是B Widget的父Widget,相应的, A Widget的 BuildContext 也是是B Widget 的 BuildContext 的父节点。

下面给一个例子加深理解:

@override
  Widget build(BuildContext context) {
    // here, Scaffold.of(context) returns null,
    //在Scaffold创建之前,如果在这里调用Scaffold.of(context)会返回null,是因为此时Scaffo//ld还没创建,所以其BuildContext也没有创建,为了在创建Scaffold的时候就取到他的BuildC//ontext,要使用Builder
    return 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.')
              ));
            }
          );
        }
      )
    );
  }
复制代码

0x04 BuildContext 内部方法解析

为什么 BuildContext 可以用来获取上下文的数据,主要是因为 BuildContext 具有的以下方法:

  1. ancestorInheritedElementForWidgetOfExactType(Type targetType) → InheritedElement
Obtains the element corresponding to the nearest widget of the given type, which must be the type of a concrete InheritedWidget subclass. [...]
复制代码
  1. ancestorRenderObjectOfType(TypeMatcher matcher) → RenderObject
Returns the RenderObject object of the nearest ancestor RenderObjectWidget widget that matches the given TypeMatcher. [...]
复制代码
  1. ancestorStateOfType(TypeMatcher matcher) → State
Returns the State object of the nearest ancestor StatefulWidget widget that matches the given TypeMatcher. [...]
复制代码
  1. ancestorWidgetOfExactType(Type targetType) → Widget
Returns the nearest ancestor widget of the given type, which must be the type of a concrete Widget subclass. [...]
复制代码
  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. [...]
复制代码
  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. [...]
复制代码
  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. [...]
复制代码
  1. rootAncestorStateOfType(TypeMatcher matcher) → State
Returns the State object of the furthest ancestor StatefulWidget widget that matches the given TypeMatcher. [...]
复制代码
  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. [...]
复制代码
  1. visitChildElements(ElementVisitor visitor) → void
Walks the children of this widget. [...]
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Web Data Mining

Web Data Mining

Bing Liu / Springer / 2006-12-28 / USD 59.95

Web mining aims to discover useful information and knowledge from the Web hyperlink structure, page contents, and usage data. Although Web mining uses many conventional data mining techniques, it is n......一起来看看 《Web Data Mining》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

在线进制转换器
在线进制转换器

各进制数互转换器

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具