“上下文”是什么? “窗口小部件构建(BuildContext上下文)”的在Flutter中意味着什么? [英] What does "context" of "Widget build(BuildContext context)" mean in Flutter?

查看:276
本文介绍了“上下文”是什么? “窗口小部件构建(BuildContext上下文)”的在Flutter中意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL; DR:

是构建方法的第二个参数,还是以常规方式对BuildContext参数进行名称声明?

我正在通过分解小部件构建(BuildContext上下文)的每个元素来学习Flutter中的构建方法的基本语法。

I'm learning the basic syntax of build method in Flutter by breaking down each element of "Widget build(BuildContext context)".

我的到目前为止的理解如下:

My understanding so far is below:



  • @override:注释

  • 小部件build(){}:构建方法返回小部件

  • BuildContext :一个参数/参数,其中包含有关此小部件在其上构建的树中位置的信息

但是,即使在我阅读了相关的SO问题和官方文档之后,上下文的最后一词到底是什么意思还不清楚。

However, it's not been clear what the last word "context" exactly means even after I read related SO questions and official documents.

到目前为止,我确定的是名称 context可以更改为任何您想要的名称,这意味着它不必是相同的名称

What I made sure so far is that the name "context" is changeable to whatever you want, meaning it doesn't have to be the same name as "context".

推荐答案

在文档中, BuildContext 是:


小部件树中小部件位置的句柄。

A handle to the location of a widget in the widget tree.

context 是一个 BuildContext 实例,该实例按顺序传递给小部件的生成器以便让它知道它在应用程序的小部件树中的位置。

context is a BuildContext instance which gets passed to the builder of a widget in order to let it know where it is inside the Widget Tree of your app.

一种常见用法是将其传递给 of 继承的窗口小部件的c $ c>方法。

One of the common uses is passing it to the of method when using an Inherited Widget.

调用 Something.of(context),例如,返回 Something 相对于树中最近的窗口小部件,可以为您提供某事

Calling Something.of(context), for example, returns the Something relative to the closest widget in the tree that can provide you that Something.

您可以阅读有关 BuildContext 此处电子文档

这篇关于“上下文”是什么? “窗口小部件构建(BuildContext上下文)”的在Flutter中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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