TExcelTAD
Home › Flutter › StatelessWidget

Flutter StatelessWidget

A StatelessWidget is a widget that is fully described by the values passed to its constructor. It holds no data that changes over time — when its inputs change, the parent simply creates a new instance with new values. Most of the widgets you write will be stateless.

Anatomy of a StatelessWidget

flutter · try it yourself
import 'package:flutter/material.dart';

void main() => runApp(const MaterialApp(home: Scaffold(body: Center(child: Greeting(name: 'Ada')))));

class Greeting extends StatelessWidget {
  // 1. A const constructor with a key and named parameters
  const Greeting({super.key, required this.name});

  // 2. Final fields: a stateless widget never changes them
  final String name;

  // 3. build() returns the widget subtree
  @override
  Widget build(BuildContext context) {
    return Text(
      'Hello, $name!',
      style: const TextStyle(fontSize: 28, fontWeight: FontWeight.bold),
    );
  }
}

The three parts:

  1. Constructor — const Greeting({super.key, required this.name}). super.key is a Dart 3 super parameter that forwards the optional key to StatelessWidget. Marking it const lets callers create constant instances.
  2. Final fields — every field must be final, because widgets are immutable.
  3. build() — called by the framework whenever the widget needs to be drawn. It receives a BuildContext (the widget's location in the tree) and returns other widgets.

Note: You never call build() yourself. Flutter calls it when the widget is first inserted, when its parent rebuilds it with new arguments, or when an inherited value it depends on (like the theme) changes.

Passing data down

Parameters are how parents configure children. Use named, required parameters for mandatory values and give optional parameters a default:

flutter · try it yourself
import 'package:flutter/material.dart';

void main() => runApp(const MaterialApp(home: ProfilePage()));

class ProfilePage extends StatelessWidget {
  const ProfilePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Team')),
      body: const Column(
        children: [
          MemberRow(name: 'Linh Tran', role: 'Designer'),
          MemberRow(name: 'Omar Haddad', role: 'Developer', online: true),
          MemberRow(name: 'Sofia Rossi', role: 'Product Manager'),
        ],
      ),
    );
  }
}

class MemberRow extends StatelessWidget {
  const MemberRow({
    super.key,
    required this.name,
    required this.role,
    this.online = false,
  });

  final String name;
  final String role;
  final bool online;

  @override
  Widget build(BuildContext context) {
    return ListTile(
      leading: CircleAvatar(child: Text(name[0])),
      title: Text(name),
      subtitle: Text(role),
      trailing: Icon(Icons.circle, size: 12, color: online ? Colors.green : Colors.grey),
    );
  }
}

MemberRow is reusable: the screen describes what to show, the widget describes how.

Using the BuildContext

The context lets a widget look up things provided higher in the tree, such as the theme or the screen size:

flutter · try it yourself
import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(
      theme: ThemeData(colorSchemeSeed: Colors.teal),
      home: const Scaffold(body: Center(child: ThemedBadge(text: 'NEW'))),
    ));

class ThemedBadge extends StatelessWidget {
  const ThemedBadge({super.key, required this.text});
  final String text;

  @override
  Widget build(BuildContext context) {
    final colors = Theme.of(context).colorScheme;
    final width = MediaQuery.sizeOf(context).width;
    debugPrint('Screen width: $width');
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
      decoration: BoxDecoration(
        color: colors.primary,
        borderRadius: BorderRadius.circular(20),
      ),
      child: Text(text, style: TextStyle(color: colors.onPrimary)),
    );
  }
}
Screen width: 360.0

Because ThemedBadge reads the theme through context, changing the app's seed colour restyles every badge automatically.

When to use StatelessWidget

Use StatelessWidget when… Use StatefulWidget when…
The UI depends only on constructor arguments and inherited data The widget itself must remember something that changes
Examples: labels, icons with text, cards, list rows Examples: counters, toggles, text input, animations

Tip: Start every widget as stateless. Convert it to stateful only when it truly needs to own changing data — your editor has a "Convert to StatefulWidget" quick fix.

Test yourself

Why must the fields of a StatelessWidget be final?

  1. Dart forbids non-final fields in classes
  2. Widgets are immutable descriptions; changes come from creating a new widget
  3. Final fields render faster on the GPU
  4. So that setState() can modify them

What does super.key in const MyWidget({super.key}) do?

  1. Creates a unique random key
  2. Forwards the optional key parameter to the superclass constructor
  3. Makes the widget stateful
  4. Marks the widget as the root

Who calls a widget's build() method?

  1. Your own code, after creating the widget
  2. The Flutter framework
  3. The main() function
  4. The Dart garbage collector