Flutter Widget Basics
Widgets are the basic building blocks of the Flutter UI.
In Flutter, everything is a Widget; from simple text to complex page layouts, everything is composed of Widgets.
What is a Widget?
A Widget is a Dart class that describes the appearance and behavior of the UI. Every Flutter app is essentially a tree structure composed of Widgets, called the Widget tree.
Example: the simplest Widget example
import
'package:flutter/material.dart'
;
// Define a simple Widget
class
HelloWidget
extends
StatelessWidget
{
const
HelloWidget
(
{
super
.
key
}
)
;
@override
Widget build
(
BuildContext context
)
{
// Return a Text Widget that displays "Hello World"
return
const
Text
(
'Hello World'
)
;
}
}
StatelessWidget - stateless component
StatelessWidget is an immutable Widget; once created, its properties cannot be changed. It is suitable for displaying static content, such as text, images, icons, etc.
Basic structure
Example: StatelessWidget example
import
'package:flutter/material.dart'
;
// Define a stateless component
class
MyTextWidget
extends
StatelessWidget
{
// Constructor parameters (immutable properties)
final
String text
;
final
Color textColor
;
final
double
fontSize
;
// Use a const constructor
const
MyTextWidget
(
{
super
.
key
,
required
this
.
text
,
this
.
textColor
=
Colors.
black
,
this
.
fontSize
=
16.0
,
}
)
;
@override
Widget build
(
BuildContext context
)
{
// The build method returns another Widget
return
Text
(
text
,
style
:
TextStyle
(
color
:
textColor
,
fontSize
:
fontSize
,
)
,
)
;
}
}
// Usage example
// MyTextWidget(text: 'Hello', textColor: Colors.red)
Applicable scenarios
- Static text display
- Image display
- Icon display
- Layouts that do not change over time
StatefulWidget - stateful component
StatefulWidget is a mutable Widget that can update the UI as internal data changes. It is suitable for scenarios that need to respond to user interaction or data changes.
Basic structure
Example: StatefulWidget example
import
'package:flutter/material.dart'
;
// Define a stateful component
class
CounterWidget
extends
StatefulWidget
{
const
CounterWidget
(
{
super
.
key
}
)
;
@override
State
<
CounterWidget
createState
(
)
=>
_CounterWidgetState
(
);
}
// The State class holds mutable state
class
_CounterWidgetState
extends
State
<
CounterWidget
{// Counter state
int
_count
=
0
;
// Method to increment the counter
void
_increment
(
)
{
// Call setState to notify the framework that the state has changed
setState
(
(
)
{
_count
++
;
}
)
;
}
@override
Widget build
(
BuildContext context
)
{
return
Column
(
mainAxisAlignment
:
MainAxisAlignment.
center
,
children
:
[
// Display the current count
Text
(
'Counter: $_count'
,
style
:
const
TextStyle
(
fontSize
:
24
)
,
)
,
const
SizedBox
(
height
:
16
)
,
// Increment button
ElevatedButton
(
onPressed
:
_increment
,
child
:
const
Text
(
'Increment'
)
,
)
,
]
,
)
;
}
}
The setState method
In a StatefulWidget, if the state changes, you must call setState method to notify Flutter to rebuild the UI.
If you modify the state but do not call setState, the UI will not update. This is because Flutter uses setState to determine whether a Widget needs to be rebuilt.
Applicable scenarios
- Dynamic data such as counters and timers
- Form input
- Adding, deleting, and modifying list items
- User interaction response
build method
Every Widget must implement the build method, which returns a part of the Widget tree.
Basic rules
| Rule | Description |
|---|---|
| Return Value | Must return a Widget, cannot return null |
| Parameters | Receives a BuildContext parameter, indicating its position in the Widget tree |
| When it is called | On first build, when the parent Widget rebuilds, and when the state changes |
Example: build method example
@override
Widget build
(
BuildContext context
)
{
// BuildContext provides the ability to access the theme, theme, and other information
// For example, get the current theme color
final
theme
=
Theme.
of
(
context
)
;
// Return a Widget
return
Container
(
padding
:
const
EdgeInsets.
all
(
16.0
)
,
decoration
:
BoxDecoration
(
color
:
theme.
colorScheme
.
primaryContainer
,
borderRadius
:
BorderRadius.
circular
(
8
)
,
)
,
child
:
Text
(
'Hello Flutter'
,
style
:
TextStyle
(
color
:
theme.
colorScheme
.
onPrimaryContainer
,
fontSize
:
20
,
)
,
)
,
)
;
}
Common basic Widgets
Text - text display
Example: Text Widget
// Basic text
const
Text
(
'Hello World'
)
// Styled text
Text
(
'Hello Flutter'
,
style
:
TextStyle
(
fontSize
:
20
,
fontWeight
:
FontWeight.
bold
,
color
:
Colors.
blue
,
)
,
)
// Text with optional styling
Text.
rich
(
TextSpan
(
text
:
'Hello '
,
children
:
[
TextSpan
(
text
:
'Flutter'
,
style
:
TextStyle
(
fontWeight
:
FontWeight.
bold
)
,
)
,
]
,
)
,
)
Icon - icon
Example: Icon Widget
// Use Material Icons
const
Icon
(
Icons.
home
)
// Home icon
const
Icon
(
Icons.
star
,
color
:
Colors.
yellow
)
// A colored star
const
Icon
(
Icons.
settings
,
size
:
48
)
// A settings icon of size 48
Image - image
Example: Image Widget
// Load image from network
Image.
network
(
'https://example.com/image.png'
)
// Load image from local assets
Image.
asset
(
'assets/images/logo.png'
)
// Load an image from a file
Image.
file
(
File
(
'path/to/image.png'
)
)
// Image with parameters
Image.
network
(
'https://example.com/image.png'
,
width
:
200
,
height
:
200
,
fit
:
BoxFit.
cover
,
)
Button - button
Example: button Widget
// Text button
TextButton
(
onPressed
:
(
)
{
/* handle tap */
}
,
child
:
const
Text
(
'Text button'
)
,
)
// Floating button
FloatingActionButton
(
onPressed
:
(
)
{
/* handle tap */
}
,
child
:
const
Icon
(
Icons.
add
)
,
)
// ElevatedButton - button with shadow
ElevatedButton
(
onPressed
:
(
)
{
/* handle tap */
}
,
child
:
const
Text
(
'Submit'
)
,
)
// Button with an icon
ElevatedButton.
icon
(
onPressed
:
(
)
{
/* handle tap */
}
,
icon
:
const
Icon
(
Icons.
save
)
,
label
:
const
Text
(
'Save'
)
,
)
Widget composition
Flutter's power lies in the ability to combine Widgets. Through nesting, a simple Widget can build a complex UI.
Example: composing Widgets
class
MyCard
extends
StatelessWidget
{
const
MyCard
(
{
super
.
key
}
)
;
@override
Widget build
(
BuildContext context
)
{
return
Card
(
// Card is a card component of Material Design
elevation
:
4
,
// Shadow depth
child
:
Padding
(
padding
:
const
EdgeInsets.
all
(
16.0
)
,
child
:
Column
(
crossAxisAlignment
:
CrossAxisAlignment.
start
,
children
:
[
// Title
const
Text
(
'Card title'
,
style
:
TextStyle
(
fontSize
:
20
,
fontWeight
:
FontWeight.
bold
,
)
,
)
,
const
SizedBox
(
height
:
8
)
,
// Description text
const
Text
(
'This is the card description, which can contain multiple lines of text.'
,
)
,
const
SizedBox
(
height
:
16
)
,
// Button row
Row
(
mainAxisAlignment
:
MainAxisAlignment.
end
,
children
:
[
TextButton
(
onPressed
:
(
)
{
}
,
child
:
const
Text
(
'Cancel'
)
,
)
,
const
SizedBox
(
width
:
8
)
,
ElevatedButton
(
onPressed
:
(
)
{
}
,
child
:
const
Text
(
'Confirm'
)
,
)
,
]
,
)
,
]
,
)
,
)
,
)
;
}
}