Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mobile BuildContex+;小部件树_Mobile_Dart_Flutter - Fatal编程技术网

Mobile BuildContex+;小部件树

Mobile BuildContex+;小部件树,mobile,dart,flutter,Mobile,Dart,Flutter,我之所以这么做是因为我不清楚如何使用这个类以及为什么使用这个类 Widget build(BuildContext context) { // here, Scaffold.of(context) returns null return Scaffold( appBar: AppBar(title: Text('Demo')), body: Builder( builder: (BuildContext context) {

我之所以这么做是因为我不清楚如何使用这个类以及为什么使用这个类

  Widget build(BuildContext context) {
    // here, Scaffold.of(context) returns null
    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.')
              ));
            }
          );
        }
      )
    );
  }
我不明白这段话:

特定小部件的BuildContext可以随时间改变位置 当小部件在树上移动时。因此,价值观 从此类上的方法返回的不应缓存到 单个同步函数的执行

BuildContext对象实际上是元素对象。构建上下文 接口用于阻止对元素的直接操作 对象

当小部件在树上移动时->这是如何发生的


根据我的理解(如果我错了,请纠正我),小部件树基本上是小部件如何“堆叠”以及它们如何相互构建的。由于不建议在CustomWidget类中将子小部件作为属性引用,因此如何更改生成方法期间返回的小部件在树中的位置(或者由于框架的原因,默认情况下是如何发生的)

当生成方法有条件地生成其子代时,通常会发生此树更改

例如:

Widget build(BuildContext context) {
  return condition
    ? Foo()
    : Bar(child: Foo());
}
使用这种构建方法,
Foo
小部件的构建上下文会随着
条件的改变而改变