有人可以向我解释Builder类在Flutter中的作用吗? [英] Can someone explain to me what the Builder Class does in Flutter?

查看:175
本文介绍了有人可以向我解释Builder类在Flutter中的作用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档非常混乱且含糊.它是这样的:

The documentation is very confusing and vague. Here is what it states:

Builder类

一个柏拉图式小部件,它调用一个闭包来获取其子小部件.

A platonic widget that calls a closure to obtain its child widget.

这是我的问题:

  1. 柏拉图式"是什么意思?
  2. 关闭"是什么意思?
  3. 该课程的目的是什么?

推荐答案

在互联网上进行了长时间的广泛研究之后,我收集了一些小片段,并将它们组合在一起,以清晰,清晰地解释了Builder类的功能

After long hours of extensive hair-pulling research on the internet, I collected small snippets and combined them to put a cohesive and clear explanation of what the Builder Class does.

术语:

根据官方的flutter文档, Builder类定义为:

According to the official flutter documentation, the Builder Class is defined as:

一个柏拉图式小部件,它调用一个闭包来获取其子小部件.

A platonic widget that calls a closure to obtain its child widget.

柏拉图式的表示这种类型的最简单的东西.术语 closure 只是 lambda 函数的另一个名称.

Platonic means the simplest possible thing of that kind. The term closure is just another name for a lambda function.

目的:

这将是一个冗长的解释,但是请忍受我:

This is going to be a lengthy explanation, but please bear with me:

在Flutter框架中,每个小部件都有一个 build 方法,该方法接受 BuildContext 参数:

In the Flutter framework, every widget has a build method that accepts a BuildContext parameter:

小部件构建( BuildContext上下文 ){ ...}

Widget build ( BuildContext context ) { ... }

我们必须记住,框架会自动将 context 对象传递到小部件的 build 函数.由于该框架会自动处理该问题,因此没有任何理由让任何小部件都具有需要接受 context 的构造函数或函数(除了 build 之外). > 参数.

We have to remember that the context object is passed to the widget's build function automatically by the framework. Since the framework takes care of that automatically, there is no reason for any widget to have a constructor or function (aside from build) that would need to accept a context parameter.

因此,如果您试图将特定的 context 对象传递给孩子,则将无法进行此操作.您不能调用build()并手动传递自己的 context 对象.我的意思是,可以,但是您将两次调用build函数:

Hence, if you were trying to pass a specific context object to a child, you won't be able to. You cannot call build() and pass your own context object manually. I mean, you can, but you would be calling the build function twice:

  1. 您的手动通话.
  2. 框架自动调用.

那么,我们如何传递特定的 context 对象?这是 Builder 类的来源. Builder 类的目的仅仅是构建并返回子窗口小部件.这与其他任何小部件有何不同?啊哈!通过 Builder 类,您可以将特定的 context 对象向下传递给它的子对象. Builder 类基本上是您设置的自己的构建函数.

So, how can we pass a specific context object? This is where the Builder class comes in. The purpose of the Builder class is simply to build and return child widgets. How is that different from any other widget? Aha! The Builder class allows you to pass a specific context object down to its children. The Builder class is basically your own build function that you setup.

我为什么需要传递特定的 context 对象?让我们看一个例子:

Why would I need to pass a specific context object? Lets take a look at an example:

让我们说我们想向其返回的新 Scaffold 父窗口小部件中添加一个新的 SnackBar 小部件:

Lets say that we want to add a new SnackBar widget to its new Scaffold parent widget that is being returned:

 @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(
          title: new Text(widget.title),
        ),
        body: new Container(),
        /// Scaffold doesn't exist in this context here
        /// because the context thats passed into 'build'
        /// refers to the Widget 'above' this one in the tree,
        /// and the Scaffold doesn't exist above this exact build method
        ///
        /// This will throw an error:
        /// 'Scaffold.of() called with a context that does not contain a Scaffold.'
        floatingActionButton: new FloatingActionButton(onPressed: () {
          Scaffold.of(context).showSnackBar(
                new SnackBar(
                  content: new Text('SnackBar'),
                ),
              );
        }));
  }

上面的代码无效. Scaffold.of(context)函数将找不到 Scaffold ,因为:

This code above does not work. The Scaffold.of(context) function will not find the Scaffold because:

  1. Scaffold 小部件尚未创建.
  2. 传递给构建函数的 context 对象引用父窗口小部件,它不是 Scaffold 小部件.
  1. The Scaffold widget has not been created yet.
  2. The context object that was passed to the build function refers to the parent widget, which is not a Scaffold widget.

那么,我们如何赋予子SnackBar小部件访问父 Scaffold 小部件的权限?我们使用 Builder 类传递 Scaffold 小部件的上下文:

So, how do we give the child SnackBar widget access to the parent Scaffold widget? We use a Builder class to pass the context of the Scaffold widget:

 @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Container(),
      /// Builders let you pass context
      /// from your *current* build method
      /// Directly to children returned in this build method
      ///
      /// The 'builder' property accepts a callback
      /// which can be treated exactly as a 'build' method on any
      /// widget
      floatingActionButton: new Builder(builder: (BuildContext context) {
        return new FloatingActionButton(onPressed: () {
          Scaffold.of(context).showSnackBar(
                new SnackBar(
                  backgroundColor: Colors.blue,
                  content: new Text('SnackBar'),
                ),
              );
        });
      }),
    );
  }

请记住, Builder 类的构造函数:

Remember, the Builder class constructor:

Builder({键,@ required WidgetBuilder构建器})

Builder({Key key, @required WidgetBuilder builder })

通过将其构件委托给通过其构造函数传递的回调函数来创建构件.

creates a widget by delegating its build to the callback function passed through its constructor.

因此,在代码中:

new Builder(builder: (BuildContext context){ ... });

我们提供了一个闭包,

  1. 包含一个 BuildContext上下文 参数
  2. 根据传递的 context 构建并返回子窗口小部件.
  1. Contains a BuildContext context parameter
  2. Builds and returns child widget(s) based on that context passed.

基本上,您提供了自己的构建功能.此闭包中的 BuildContext上下文参数是Scaffold的上下文!宝贝!

Basically, you provided your own build function. The BuildContext context parameter in this closure is the Scaffold's context! Baboom!

基本上就是这样. Flutter文档完全没有提供完整的解释.我觉得比破译Flutter文档,破译古代象形文字要容易得多.

That is basically it. The Flutter documentation does not provide a thorough explanation of this at all. I feel like I would have an easier time deciphering ancient hieroglyphs than decoding the Flutter documentation.

我希望这对当前正在学习Flutter的单调乏味的人有所帮助.

I hope this helps anyone that is currently on their tedious journey in learning Flutter.

摘要:对于仍然很难理解这个概念的任何人,让我以更简洁的形式进行解释. Builder函数只是允许您获得并使用 Builder 小部件所在的小部件的 context 对象.在上面的示例中,它是 new Scaffold ()小部件.请记住,唯一可用的 context 对象是父窗口小部件(在Scaffold之上)的对象,因为尚未创建当前窗口小部件(Scaffold).我希望这对那些仍在挠头的人有所帮助.干杯!

SUMMARY: For anyone still having a hard time grasping this concept, let me explain in a more concise form. The Builder function simply allows you to attain and use the context object of the widget that the Builder widget is in. In the example above, it is the new Scaffold() widget. Remember, the only context object available to use is that of the parent widget (above Scaffold) since the current widget (Scaffold) has not been created yet. I hope that helps those were still scratching their heads. Cheers!

这篇关于有人可以向我解释Builder类在Flutter中的作用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆