Flutter Advanced
This section will introduce some advanced Flutter concepts to help you improve your development skills.
Rendering Principles
Flutter uses the Skia rendering engine (gradually switching to Impeller since 3.0) to draw UI directly, without using native components.
Widget Tree and Element Tree
- Widget Tree: describes the UI configuration information (immutable)
- Element Tree: the instantiated object of a Widget (mutable)
- RenderObject Tree: responsible for actual rendering
Understanding rendering principles helps optimize UI performance and avoid unnecessary rebuilds.
InheritedWidget - Data Passing
InheritedWidget allows data to be passed down the Widget tree, and child Widgets can access data from their ancestors.
Example: Custom InheritedWidget
// Define data container
class
MyData
extends
InheritedWidget
{
final
int
value
;
const
MyData
(
{
super
.
key
,
required
this
.
value
,
required
super
.
child
,
}
)
;
// Convenience method: get the nearest MyData
static
MyData of
(
BuildContext context
)
{
return
context.
dependOnInheritedWidgetOfExactType
<
MyData
(
)!
;
}
// Determine whether child Widgets need to rebuild
@override
bool
updateShouldNotify
(
MyData oldWidget
)
{
return
value
!=
oldWidget.
value
;
}
}
// Use
class
ParentWidget
extends
StatelessWidget
{
const
ParentWidget
(
{
super
.
key
}
)
;
@override
Widget build
(
BuildContext context
)
{
return
MyData
(
value
:
42
,
child
:
const
ChildWidget
(
)
,
)
;
}
}
class
ChildWidget
extends
StatelessWidget
{
const
ChildWidget
(
{
super
.
key
}
)
;
@override
Widget build
(
BuildContext context
)
{
// Get the ancestor's MyData
final
myData
=
MyData.
of
(
context
)
;
return
Text
(
'Value: ${myData.value}'
)
;
}
}
RepaintBoundary - Local Repaint
Using RepaintBoundary can limit the repaint area and improve performance.
Example: Using RepaintBoundary
class
MyWidget
extends
StatelessWidget
{
const
MyWidget
(
{
super
.
key
}
)
;
@override
Widget build
(
BuildContext context
)
{
return
RepaintBoundary
(
child
:
Container
(
// Painting in this area does not affect the outside
color
:
Colors.
red
,
child
:
const
Text
(
'Independent Area'
)
,
)
,
)
;
}
}
Understanding Keys in Depth
Keys help Flutter identify Widgets, distinguishing new Widgets from existing ones.
ValueKey vs ObjectKey vs UniqueKey
| Type | Description |
|---|---|
| ValueKey | Using a value as a key (such as ID, number, string) |
| ObjectKey | Using an object reference as a key |
| UniqueKey | Generating a unique key on every build, breaking rebuilds |
CustomPainter - Custom Painting
Using CustomPainter, you can achieve fully custom graphics painting.
Example: Using CustomPainter
class
CustomPainterExample
extends
StatelessWidget
{
const
CustomPainterExample
(
{
super
.
key
}
)
;
@override
Widget build
(
BuildContext context
)
{
return
CustomPaint
(
painter
:
MyCirclePainter
(
)
,
size
:
const
Size
(
200
,
200
)
,
)
;
}
}
class
MyCirclePainter
extends
CustomPainter
{
@override
void
paint
(
Canvas canvas
,
Size size
)
{
// Draw a circle
final
paint
=
Paint
(
)
..
color
=
Colors.
blue
..
style
=
PaintingStyle.
fill
;
final
center
=
Offset
(
size.
width
/
2
,
size.
height
/
2
)
;
final
radius
=
size.
width
/
2
10
;
canvas.
drawCircle
(
center
,
radius
,
paint
)
;
// Draw a border
final
borderPaint
=
Paint
(
)
..
color
=
Colors.
black
..
style
=
PaintingStyle.
stroke
..
strokeWidth
=
2
;
canvas.
drawCircle
(
center
,
radius
,
borderPaint
)
;
}
@override
bool
shouldRepaint
(
covariant CustomPainter oldDelegate
)
{
return
false
;
}
}
Stream and Future
Example: Stream Usage
// Create a Stream
Stream
<
int
countStream
(
int
max
)
async
{
for
(
int
i
=
1;
i
<=
max
;
i
++
)
{
await Future.
delayed
(
const
Duration
(
seconds
:
1
)
)
;
yield i
;
// Send a value
}
}
// Use StreamBuilder
class
StreamExample
extends
StatelessWidget
{
const
StreamExample
(
{
super
.
key
}
)
;
@override
Widget build
(
BuildContext context
)
{
return
StreamBuilder
<
int
(
stream:
countStream
(
10
)
,
builder
:
(
context
,
snapshot
)
{
if
(
snapshot.
hasData
)
{
return
Text
(
'Count: ${snapshot.data}'
)
;
}
else
if
(
snapshot.
hasError
)
{
return
Text
(
'Error: ${snapshot.error}'
)
;
}
return
const
CircularProgressIndicator
(
)
;
}
,
)
;
}
}
Summary
This Flutter introductory tutorial covers the following:
- Flutter installation and environment configuration
- Widget fundamentals (StatelessWidget and StatefulWidget)
- Layout system (Row, Column, Stack)
- User input handling
- State management (setState, Provider)
- Network requests and data storage
- Navigation and routing
- Testing and publishing
For further study, you can focus on:
- BLoC pattern
- Riverpod state management
- GetX routing and state management
- Flutter performance optimization
- Flutter Web and desktop development