简体   繁体   English

Scaffold.of会找到遥远的窗口小部件吗?

[英]Will Scaffold.of ever find distant widget anestors?

I am trying to learn more about BuildContext and Scaffold.of() . 我正在尝试了解有关BuildContextScaffold.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 . 在这里,您正在使用MyScreenBuildContext 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) . 因此, MyScreenScaffold的祖先,无法通过Scaffold.of(context)访问它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Snackbar 和异常:scaffold.of() - Snackbar and exception: scaffold.of() 使用不包含脚手架的上下文调用的 Scaffold.of() - Scaffold.of() called with a context that does not contain a Scaffold 未处理的异常:使用不包含 Scaffold 的上下文调用 Scaffold.of() - Unhandled Exception: Scaffold.of() called with a context that does not contain a Scaffold 错误:没有名为“nullOk”的命名参数 Scaffold.of(context, nullOk: true) - Error: No named parameter with the name 'nullOk' Scaffold.of(context, nullOk: true) Flutter Scaffold.of(context).openDrawer() 不起作用 - Flutter Scaffold.of(context).openDrawer() doesn't work Flutter :此错误出现在使用不包含 Scaffold 的上下文调用的 Scaffold.of() 。 当我尝试展示小吃店时 - Flutter : this error appear Scaffold.of() called with a context that does not contain a Scaffold. when i try to show a snackbar 处理手势时引发了以下断言: Scaffold.of() 使用不包含 Scaffold 的上下文调用 - The following assertion was thrown while handling a gesture: Scaffold.of() called with a context that does not contain a Scaffold 尝试使用 Scaffold.of(context) 使用 openDrawer() 找到 2 而不是 0 时出现太多位置 arguments 错误 - Too many positional arguments error when trying to use openDrawer() using Scaffold.of(context) finding 2 instead of 0 脚手架后面的小部件 - widget behind Scaffold 使用 Scaffold 小部件画一条线 - Draw a line with Scaffold widget
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM