Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an element in Flutter?

Tags:

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.

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?

like image 735
richalot Avatar asked Mar 08 '18 03:03

richalot


2 Answers

Flutter creates element for widget(including child widgets) it's displayed on the screen and it has a reference to the widget. Flutter framework creates the element tree which represents the rendered widget on the screen. An Element represents the use of a widget to configure a specific location in the tree. The element can change if the parent widget rebuilds and creates a new widget for this location. Element(mutable property) are created by createElement() method. The framework calls mount method to add the newly created element to the tree at a given slot in a given parent.

For more info, see this documentation

enter image description here

like image 198
Vinoth Vino Avatar answered Oct 03 '22 02:10

Vinoth Vino


In Flutter widget is the configuration of the UI, the main class which represents the element or UI displayed on the screen is Element. i.e Widget is actually the configuration data of Element and the Widget tree is actually a configuration tree, for the Element tree which is automatically generated whenever we create a widget tree.

More Info here - https://api.flutter.dev/flutter/widgets/Element-class.html

like image 22
Shubhamhackz Avatar answered Oct 03 '22 00:10

Shubhamhackz