Sub topic 9
Building UI with StatelessWidget
Prerequisites
This chapter assumes that you have Flutter installed, can run flutter doctor, and have created a project with flutter create. You should also know the basics of Dart classes, constructors, functions, and variables.
The StatelessWidget model
A StatelessWidget is a widget whose visible output depends only on its inputs and other values that do not change during its lifetime. It does not own changing screen state.
A stateless widget implements build(). Flutter calls this method to get the widget's UI. The method returns another widget, often a tree of nested widgets.
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Profile Card',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.indigo),
),
home: const ProfilePage(
name: 'Ada Lovelace',
role: 'Computing pioneer',
),
);
}
}
class ProfilePage extends StatelessWidget {
final String name;
final String role;
const ProfilePage({
required this.name,
required this.role,
super.key,
});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Profile')),
body: Center(
child: Card(
margin: const EdgeInsets.all(24),
child: Padding(
padding: const EdgeInsets.all(24),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const CircleAvatar(
radius: 36,
child: Icon(Icons.person, size: 40),
),
const SizedBox(height: 16),
Text(
name,
style: Theme.of(context).textTheme.headlineSmall,
),
const SizedBox(height: 8),
Text(role),
],
),
),
),
),
);
}
}runApp() places the root widget into Flutter's rendering system. MaterialApp provides Material Design behavior, such as themes and navigation. Scaffold supplies a common page structure with an app bar and body.
The ProfilePage class receives name and role through final fields. These values are called properties. A parent can create another ProfilePage with different properties, but ProfilePage itself does not change those fields after construction.
Reading the widget tree
The returned value is a tree:
Flutter uses the tree to decide what to display and how to lay it out. Column places children vertically. Padding adds space around one child. Center positions its child in the available space.
| Widget | Main purpose |
|---|---|
| `Text` | Displays a string |
| `Column` | Arranges children vertically |
| `Padding` | Adds inset space |
| `Card` | Groups content with Material styling |
Common errors
- Forgetting
@overrideor givingbuild()the wrong return type. It must returnWidget. - Omitting
constfrom constructors when all values are known at compile time. This is usually a performance and lint issue, not a runtime failure. - Placing a
Columnin a space with unbounded height. This can produce layout errors when the parent does not provide a finite constraint. - Expecting a
StatelessWidgetto update itself after a button press. Changing interaction belongs in aStatefulWidgetor another state-management design.
Exercise
Which class should you change if the profile name must update after a user taps a button?
Show answer
Use a StatefulWidget with a State object, then update the value inside setState().