Flutter中的元素是什么? [英] What is an element in Flutter?
问题描述
我很难理解Flutter中的元素是什么.在文档中:在树中特定位置的窗口小部件的实例化". . . .我想我现在要问的是那棵树.
I'm having a hard time understanding what an element is in Flutter. From the doc: "An instantiation of a Widget at a particular location in the tree". . . . I guess now I have to ask, what is the tree.
起初,我以为树是指小部件的状态,但是StatelessWidget也有createElement,所以情况似乎并非如此.然后,我以为树指的是父母/孩子的关系,但我不清楚.最后,听起来像元素在那个特定的时间和位置有点像小部件的快照,但是相关的方法似乎并不能反映这一点.我在附近吗?
At first, I thought the tree referred to the states of widgets, but StatelessWidget also has createElement, so this doesn't seem the case. Then, I thought that the tree referred to the parent/child relationship, but it is not clear to me. Finally, it sounds like an element is sort of like a snapshot of the widget at that particular time and location, but the associated methods don't seem to reflect that. Am I anywhere close?
推荐答案
Flutter创建一个可视化的Elements树,就像可变的Widget副本一样.通常,框架不会直接处理Elements.
Flutter creates a visual tree of Elements, which are like mutable copies of Widgets. You don't normally deal directly with Elements, the framework does.
因此(非常简化的版本)您的树可能看起来像这样:
So (a very simplified version of) your tree might look something like this:
MediaQuery
-- Theme Data
---- Scaffold
------ AppBar
------ Body
--------- Column
----------- Text
----------- Text
----------- Row
------------- Button
------------- Button
------ FloatingActionButton
这些Text
可能与相同的Widget
被多次重用,但是在树中有唯一的Element
s.
Those Text
s may well be the same Widget
being reused multiple times, but in the tree there are unique Element
s.
这篇关于Flutter中的元素是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!