1. Flutetr shortcuts for Android Studio
option(alt)
+enter
: selectedStatelessWidget
convertStatefulWidget
。option(alt)
+enter
: selectedWidget
The outer envelope is covered withWidget
。option(alt)
+enter
: will be selectedWidget
Removal does not affect the interior. This combination of shortcuts can do a lot of things.- AS to the right of the
FlutterLine
Select control selectextract xxx
: Extracts the associated code into a method orbuild
From the insideWidget
Extract to a separateWidget
。 - AS to the right of the
FlutterLine
, the selectedchildren
Click ^V adjust: move up and down to adjustchildren
The order. commond
+option(alt)
+b
: View all implementation classes of the abstract class.command
+n
: Quick generationgetter
.setter
.toString
.construtor
Methods.
2. How are the Flutter images rendered
Skip the native framework and feed data directly to the GPU and CPU through the SKia engine without going through intermediate states. Skia (c++ library) Google’s official graphics rendering engine.
3.StatelessWidget
andStatefulWidget
The difference between
You can find this by looking at the source code of the Widget
@immutable abstract class Widget extends DiagnosticableTree { // ... Omit code}Copy the code
Classes or subclasses specified by the @immutable annotation must be immutable, so data defined in widgets during development is immutable and should be modified with final.
3.1 StatelessWidget
: There is no state to maintainwidget
- Their data is usually written to death; from
parent widget
Is passed in and cannot be modified once passed in; fromInheritedWidget
Get data for use. - perform
build
Methods and timing:
When ourStatelessWidget
The first time is inserted intoWidget
In the tree (when it was first created); Be our fatherParent Widget
When a change occurs, the sonWidget
Will be rebuilt; If ourWidget
Rely onInheritedWidget
Some of the data of,InheritedWidget
When data is changed;
3.2 StatefulWidget
: statefulwidget
Since widgets are immutable, how do statefulwidgets store mutable state?
Flutter
willStatefulWidget
Two classes are designed: one class inherits fromStatefulWidget
As aWidget
A part of a tree; A class inherits fromState
, for recordingStatefulWidget
The changing states, and based on the changing states, new ones are constructedWidget
;- when
Flutter
In the buildWidget Tree
, will getState
“And it callsbuild
Method to obtainStatefulWidget
Wish to buildWidget
; Save the state to be saved inState
In the. StatefulWidget
Life cycle: from creation to destruction
First, executionStatefulWidget
Related methods in:- perform
StatefulWidget
Constructor to create theStatefulWidget
; - perform
StatefulWidget
thecreateState
Method to create a maintenanceStatefulWidget
theState
Object;
Second, call
createState
createState
Object is executedState
Class related methods:
- perform
State
Class Constructor to createState
Object; - perform
initState
We usually do some data initialization in this method, or we might send a network request; - perform
didChangeDependencies
Method, which is called in two cases: calledinitState
Will call; Change when you rely on some data from other objects, such as the InheritedWidget we mentioned earlier. Flutter
performbuild
Method, let’s look at our currentWidget
Which widgets need to be rendered;- The current
Widget
Is called when no longer in usedispose
Carrying out destruction; - Manual call
setState
Method is called again based on the latest state (data)build
Method to build the correspondingWidgets
; - perform
didUpdateWidget
The way is to be a fatherWidget
When rebuild is triggered, the system invokesdidUpdateWidget
Methods; - Complex version tag (see source code for details) :
mounted
isState
A property set internally;dirty state
iselement
A property of the tag force callbuild
To buildwidget
;clean state
The currentbuild
Out of theWidget
The next redraw check does not require a rebuild.
- perform
4.buildContext
What is the
BuildContext is just an abstract class with two methods, one that returns a widget and one that returns buildOwner. Everything is a widget. We write UI interfaces by building nested widgets, but a widget isn’t really what’s displayed on the screen, it’s just configuration information. It is always immutable and can be reused in many places. What is actually displayed on the view? It is the View tree element Tree.
- We are in
build
Functioncontext
In fact, it can be understood as the presentwidget
The Element object created. StatelessWidget
Inherited:widget
An override method increateElement
returnStatelessElement
. Mount the StatelesseElement object to the Element tree.StatelessElement
inheritanceComponentElement
And there arebuild
Method, passed inthis
willelement
Itself as aBuildContext
The incoming.ComponentElement
inheritanceElement
.BuildContext
Interface is used to block pairsElement
A direct operation on an object.StatefulWidget
The first is also calledStatefulWidget
thecreateElement
Method and according to thiswidget
generateStatefulElement
Object and keep itwidget
References. Will thisStatefulElement
Mount to theElement
The tree. According to the widgetcreateState
Method to createState
.StatefulElement
The objectstate
thebuild
Method, and willelement
Itself as aBuildContext
The incoming.
5. Three Trees of Flutter (render flow)
5.1 the Widget
- Widgets are description files that are constantly built as we make state changes. The framework will be compared to the previous description to update the rendering interface with minimal overhead.
5.2 Element
- Element is the detailed location of an instance of a Widget in the tree
- Widgets describe and configure what subtrees look like, while Elements actually configure specific locations in the Element tree.
5.3 RenderObject
- Renders an object in the tree
- The RenderObject layer is the core of the rendering library