简体   繁体   English

Flutter:可重用的小部件和 BuildContext

[英]Flutter: reusable widget and BuildContext

In a flutter app, let say I have this widget class with multiple widgets inside (ie. just a typical single long Widget build() class with multiple widgets inside).在 flutter 应用程序中,假设我有这个小部件 class,里面有多个小部件(即,只是一个典型的单个长Widget build() class,里面有多个小部件)。 Then these are split into multiple children widgets, with their own classes: As an example,然后将它们拆分为多个具有自己的类的子小部件:例如,

Before:前:

 class Parents extend StatelessWidget{
   Widget build(BuildContext context){
      //Parent
      return Column{
          children: <Widget>[
              //Child 1
              Container('something inside'),

              //Child 2
              Container('something inside'),

              //Child 3
              Container('something inside'),
              ...
          ]
      }
   }
 }

Now:现在:

 class Parents extend StatelessWidget{
   Widget build(BuildContext context){
      //Parent
      return Column{
          children: <Widget>[
              //Child 1
              myContainer('first child'),

              //Child 2
              myContainer('first child'),

              //Child 3
              myContainer('first child'),

              ...
          ]
      }
   }
 }

 class myContainer extend StatelessWidget{
   Widget build(BuildContext context){
      //child reusable widget
      return Container{
        'something inside'
      }
   }
 }

The question that I have is this Widget build(BuildContext context) .我的问题是这个Widget build(BuildContext context)

In the above example, I called the myContainer class three times in the parent class.在上面的示例中,我在父 class 中调用了myContainer class 3 次。 In my mind, it means that the build widget is called four times (one with parent, 3 times from each child).在我看来,这意味着build小部件被调用了四次(父级调用一次,每个子级调用 3 次)。

I mean, I saw a bunch of examples online that above approach is recommended, but is it really a proper way of doing it?我的意思是,我在网上看到了一堆推荐上述方法的例子,但这真的是一种正确的做法吗? I may not understand the flutter completely yet, but since it is a widget tree, would it be more efficient (in terms of performance wise) to simply pass the parent context down to the children?我可能还不完全理解 flutter,但由于它是一个小部件树,将父context简单地传递给子级会更有效(就性能而言)? like below:如下所示:

class myContainer extend StatelessWidget{
  final BuildContext parentContext;

   Widget build(parentContext){
      //child reusable widget
      return Container{
        'something inside'
      }
   }
 }

Both approaches seem to work but wanted to see if I am way off with my way of thinking.这两种方法似乎都有效,但想看看我是否偏离了我的思维方式。 I don't fully understand the mechanism of Context and any clarification would be super appreciated!我不完全理解Context的机制,任何澄清都将非常感激!

Thanks guys!多谢你们!

From thedocs :文档

Each widget has its own BuildContext, which becomes the parent of the widget returned by the StatelessWidget.build or State.build function.每个小部件都有自己的 BuildContext,它成为 StatelessWidget.build 或 State.build function 返回的小部件的父级。 (And similarly, the parent of any children for RenderObjectWidgets.) (类似地,RenderObjectWidgets 的任何子级的父级。)

In particular, this means that within a build method, the build context of the widget of the build method is not the same as the build context of the widgets returned by that build method.特别是,这意味着在构建方法中,构建方法的小部件的构建上下文与该构建方法返回的小部件的构建上下文不同。 This can lead to some tricky cases.这可能会导致一些棘手的情况。 For example, Theme.of(context) looks for the nearest enclosing Theme of the given build context.例如, Theme.of(context) 查找给定构建上下文的最近的封闭主题。 If a build method for a widget Q includes a Theme within its returned widget tree, and attempts to use Theme.of passing its own context, the build method for Q will not find that Theme object.如果小部件 Q 的构建方法在其返回的小部件树中包含主题,并尝试使用 Theme.of 传递其自己的上下文,则 Q 的构建方法将找不到该主题 object。 It will instead find whatever Theme was an ancestor to the widget Q. If the build context for a subpart of the returned tree is needed, a Builder widget can be used: the build context passed to the Builder.builder callback will be that of the Builder itself.相反,它将查找作为小部件 Q 的祖先的任何 Theme。如果需要返回树的子部分的构建上下文,则可以使用 Builder 小部件:传递给 Builder.builder 回调的构建上下文将是建造者本身。

So, this basically means that the BuildContext inside the build() method is actually that of it's parent.所以,这基本上意味着build()方法中的BuildContext实际上是它的父级。 Hence, their is no need to explicitly pass it.因此,它们不需要显式传递它。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM