Flutter BuildContext 探究
0x00 前言
BuildContext
是Flutter的重要部分,但是目前网上讲BuildContext
的文章太少了,所以本篇文章讲讲BuildContext
。
0x01 BuildContext 介绍
BuildContext
,顾名思义,Build(构建Widget) Context(应用上下文),就是构建Widget中的应用上下文。
所以BuildContext
只出现在两个地方:
- StatelessWidget.build 方法中:创建StatelessWidget的build方法
- State对象中:一个是创建StatefulWidget的State对象的build方法中,另一个是State的成员变量
BuildContext
实际是Element
,BuildContext
是为了阻止直接对Element
操作而抽象出来的,所以BuildContext
是Element
的抽象类,所有Element
都继承自BuildContext
。
每一个Widget都有一个BuildContext
。
BuildContext
是Widget在Widget树中位置的句柄。
0x02 访问 BuildContext 实例对象
BuildContext
被WidgetBulder
(例如: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
的父节点。
下面给一个例子加深理解:
@overrideWidget build(BuildContext context) {// here, Scaffold.of(context) returns null,//在Scaffold创建之前,如果在这里调用Scaffold.of(context)会返回null,是因为此时Scaffo//ld还没创建,所以其BuildContext也没有创建,为了在创建Scaffold的时候就取到他的BuildC//ontext,要使用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 ScaffoldScaffold.of(context).showSnackBar(SnackBar(content: Text('Hello.')));});}));}
复制代码
0x04 BuildContext 内部方法解析
为什么BuildContext
可以用来获取上下文的数据,主要是因为BuildContext
具有的以下方法:
- 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. [...]
复制代码
- ancestorRenderObjectOfType(TypeMatcher matcher) → RenderObject
Returns the RenderObject object of the nearest ancestor RenderObjectWidget widget that matches the given TypeMatcher. [...]
复制代码
- ancestorStateOfType(TypeMatcher matcher) → State
Returns the State object of the nearest ancestor StatefulWidget widget that matches the given TypeMatcher. [...]
复制代码
- ancestorWidgetOfExactType(Type targetType) → Widget
Returns the nearest ancestor widget of the given type, which must be the type of a concrete Widget subclass. [...]
复制代码
- 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. [...]
复制代码
- inheritFromElement(InheritedElement ancestor, { Object aspect }) → InheritedWidget
Registers this build context with ancestor such that when ancestor's widget changes this build context is rebuilt. [...]
复制代码
- 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. [...]
复制代码
- rootAncestorStateOfType(TypeMatcher matcher) → State
Returns the State object of the furthest ancestor StatefulWidget widget that matches the given TypeMatcher. [...]
复制代码
- 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. [...]
复制代码
- visitChildElements(ElementVisitor visitor) → void
Walks the children of this widget. [...]
复制代码
0x05 参考文献
docs.flutter.io/flutter/wid…
Flutter BuildContext 探究相关推荐
- 给Java开发者的Flutter开发基础---Dart语言
接近半年没有在简书冒泡了.这段时间一是忙于使用云信IM开发相应项目,二是整理和收集相关Flutter的相关资料进行学习.国内关于Flutter的资料还是太过于稀少,以至于我只能去YouTube和Ude ...
- 从Flutter路由报错引申BuildContext详解
前言 在使用flutter路由跳转是出现如下错误: 代码: import 'package:flutter/material.dart';void main() => runApp(MyApp( ...
- Flutter 中由 BuildContext 引发的血案
今天和各位分享一个博主在实际开发中遇到的问题,以及解决方法.废话不多说,我们先来看需求: 我们要做一个iOS风格的底部菜单弹出组件,具体涉及showCupertinoModalPopup()方法,该方 ...
- Flutter Error: The method ‘inheritFromWidgetOfExactType‘ isn‘t defined for the class ‘BuildContext‘.
异常 Androidstudio 编译运行导入别人的Flutter项目时报错,具体异常信息: D:/IDE/Flutter_windows_2.0.0-stable/flutter/.pub-cach ...
- 走近科学,探究阿里闲鱼团队通过数据提升Flutter体验的真相
背景 闲鱼客户端的flutter页面已经服务上亿级用户,这个时候Flutter页面的用户体验尤其重要,完善Flutter性能稳定性监控体系,可以及早发现线上性能问题,也可以作为用户体验提升的衡量标准. ...
- [-Flutter趣玩篇-] 出神入化的Align+动画
龙少:上一个Align玩的出神入化.现在有个需求,让一个组件以某个函数图像动起来.你说咱们要不先去找块砖头再和设计谈谈. - - 捷特: 别激动,都是成年人.多大点事,有哥在. 龙少:有什么好主意. ...
- 移植一个抖音贴纸组件到Flutter
本文首发于微信公众号--世界上有意思的事,搬运转载请注明出处,否则将追究版权责任.交流qq群:859640274 大家好久不见,又有一个多月没有发文章了,所以今天发一篇来刷刷存在感.最近 Flutte ...
- [-Flutter 自组篇-] 蛛网图+绘制+动画实践
在Android的时候自定义过蛛网图,花了半天时间.复刻到Flutter只用了不到20分钟 不得不说Flutter中的Canvas对安卓玩家还是非常友好的,越来越觉得Flutter非常有趣. 在视图方 ...
- Bolt 的 Flutter 路由管理实践(页面解耦,流程控制、功能拓展等)
在各大移动开发框架(Android.iOS.Flutter.React Native-)中,路由管理始终是 UI 架构最具热议的话题之一. 一大原因就是应用程序的页面会 不可避免的多,我们可以使用 B ...
最新文章
- 设计模式之策略模式学习笔记
- 人工机器:作为归纳系统的深度学习
- idea的pom变成橙色的xml文件
- java衍生作用_java-如何从AffineTransform衍生的形状对象中“...
- ASP.NET AJAX入门系列(10):Timer控件简单使用
- beanutils.copyproperties属性值丢失_[ASP.NET Core 3.1]浏览器嗅探解决部分浏览器丢失Cookie问...
- IDEA两步删除版本控制
- php下载隐藏,php – 隐藏下载URL
- python mro文件_Python面向对象之MRO
- 2018年下半年各省软考报名时间及网址
- 学习java第14天
- IDEA 显示Cannot resolve plugin org.apache.maven.pluginsmaven-site-plugin3.3
- mybatis----xml常用的标签
- Web大学生网页作业成品:个人博客主页 (纯HTML+CSS代码)
- 1134: 小青蛙,跳跳跳
- 关于曲面方程的法向量的求法
- 为Electron 打包后的桌面程序添加自定义向导
- 即时通讯技术文集(第5期):零基础通信技术入门 [共15篇]
- delegate与event的区别
- 封城第14日,囤了一波好货