[英]Will Scaffold.of ever find distant widget anestors?
I am trying to learn more about BuildContext
and Scaffold.of()
. 我正在尝试了解有关
BuildContext
和Scaffold.of()
更多信息。
I understand that when you are displaying a SnackBar
you often need to add a Builder
to get the context 'under' the Scaffold you have added. 我了解到,当您显示
SnackBar
,通常需要添加一个Builder
才能使上下文位于添加的Scaffold下方。
As such: 因此:
class MyScreen extends StatelessWidget {
build(BuildContext context) {
return Scaffold(
body: Builder(
builder: (context) =>
RaisedButton(
child: Text('push me'),
onPressed: () => Scaffold.of(context).showSnackBar(
SnackBar(content: Text('this will work'))),
),
));
}
}
My question is this: If Scaffold.of()
gets the closest ancestor, shouldn't it usually eventually find something higher up the widget tree, eg the SplashScreen scaffold? 我的问题是:如果
Scaffold.of()
获得最接近的祖先,通常它是否最终不应该在小部件树上找到更高的东西,例如SplashScreen支架? When we take away the builder and create this incorrect code: 当我们拿走构建器并创建此不正确的代码时:
class MyScreen extends StatelessWidget {
build(BuildContext context) {
return Scaffold(
body: RaisedButton(
child: Text('push me'),
onPressed: () => Scaffold.of(context).showSnackBar(
SnackBar(content: Text('broken'))),
),
));
}
}
then we get the standard error Scaffold.of() called with a context that does not contain a Scaffold. I/flutter ( 7750)
那么我们得到的标准错误
Scaffold.of() called with a context that does not contain a Scaffold. I/flutter ( 7750)
Scaffold.of() called with a context that does not contain a Scaffold. I/flutter ( 7750)
No Scaffold ancestor could be found starting from the context that was passed to Scaffold.of().
Scaffold.of() called with a context that does not contain a Scaffold. I/flutter ( 7750)
No Scaffold ancestor could be found starting from the context that was passed to Scaffold.of().
Even though there is no direct parent Scaffold in the BuildContext
, how can no Scaffold ancestor be found at all (for example a Grandparent ancestor)? 即使
BuildContext
没有直接的父代Scaffold,也怎么根本找不到Scaffold的祖先(例如,祖父母祖先)? All the previous pages in my application had a scaffold, are they no longer in the widget tree? 我的应用程序中的所有先前页面都有一个脚手架,它们不再位于小部件树中吗?
I understand that if it found a Scaffold and rendered the SnackBar that we wouldn't be able to see it, but I still don't understand how it doesn't find one. 我知道,如果它找到一个Scaffold并渲染了SnackBar,我们将无法看到它,但是我仍然不明白它是如何找不到的。 Perhaps I've misunderstood what 'ancestor' means.
也许我误解了“祖先”的含义。
Thank you 谢谢
BuildContext
represents the location of one specific widget in the widget tree. BuildContext
表示小部件树中一个特定小部件的位置。
As such, depending on what BuildContext
you use, the call Scaffold.of(context)
may behave differently. 这样,根据您使用的
BuildContext
,调用Scaffold.of(context)
行为可能会有所不同。
In your following example: 在您的以下示例中:
class MyScreen extends StatelessWidget {
build(BuildContext context) {
return Scaffold(
body: RaisedButton(
child: Text('push me'),
onPressed: () => Scaffold.of(context).showSnackBar(
SnackBar(content: Text('this will work'))),
),
),
);
}
}
Here, you are using the BuildContext
of MyScreen
. 在这里,您正在使用
MyScreen
的BuildContext
。 The issue is MyScreen
is the widget that creates Scaffold
. 问题是
MyScreen
是创建 Scaffold
的小部件。 As such, MyScreen
is an ancestor of Scaffold
and cannot access it through Scaffold.of(context)
. 因此,
MyScreen
是Scaffold
的祖先,无法通过Scaffold.of(context)
访问它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.