Back to quest
Flutter Widgets BasicLevel 4 / 7

Simple Example Widgets

Understand the core widgets: Text, Container, and SizedBox.

In this lesson, we will cover the fundamental building blocks of Flutter UI: Text, Container, and SizedBox.

Text Widget

The Text widget displays a string of text with single style. You can customize font, size, color, weight, and more.

Styling Text

example.dart
1 
2Center(
3 child: Text(
4 'Styled Text!',
5 style: TextStyle(
6 fontSize: 24,
7 fontWeight: FontWeight.bold,
8 color: Colors.blue,
9 letterSpacing: 2.0,
10 ),
11 ),
12)
13

HintThis creates a centered text "Styled Text!" with size 24, bold weight, blue color, and letter spacing.

Container Widget

Container is a convenience widget that combines common painting, positioning, and sizing widgets. It can have a background color, border, padding, margin, and constraints.

Container Example

example.dart
1 
2Container(
3 padding: EdgeInsets.all(20.0),
4 margin: EdgeInsets.symmetric(horizontal: 10.0),
5 decoration: BoxDecoration(
6 color: Colors.amber,
7 borderRadius: BorderRadius.circular(15.0),
8 boxShadow: [
9 BoxShadow(
10 color: Colors.black26,
11 blurRadius: 5.0,
12 offset: Offset(0, 3),
13 ),
14 ],
15 ),
16 child: Text('I am in a container!'),
17)
18

HintThis container has padding, margin, an amber background, rounded corners, and a shadow.

SizedBox Widget

SizedBox is a box with a specified size. It is often used to add fixed spacing between widgets or to constrain a child widget to a specific size.

SizedBox for Spacing

example.dart
1 
2Column(
3 children: [
4 Text('Item 1'),
5 SizedBox(height: 20), // Adds 20 pixels of vertical space
6 Text('Item 2'),
7 ],
8)
9

HintSizedBox creates a 20-pixel gap between the two Text widgets.

Loot collected

Key takeaways
  • Text widget is used for displaying strings.
  • Container is versatile for styling, padding, and layout.
  • SizedBox is efficient for adding fixed spacing or sizing constraints.
Party chat

Comments