CodeBlanks Logo Icon CodeBlanks

What is StatelessWidget?

Learn that StatelessWidget has no internal state.

What is StatelessWidget?

A StatelessWidget is a widget whose internal state does not change after creation. It produces its appearance solely from data passed via constructor parameters. It suits static content like clocks, labels and info cards. The build method is only re-called when the parameters change.

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

  @override
  Widget build(BuildContext context) {
    return Text('Merhaba');
  }
}

Stateless vs Stateful

StatelessWidget has no state; StatefulWidget has state. In a stateless widget data only arrives via the constructor and never changes; in a stateful widget variables inside the State object update with user interaction and build is called again. A counter is stateful; a title is stateless.

// Stateless: data comes from constructor
class Etiket extends StatelessWidget {
  final String metin;
  const Etiket({super.key, required this.metin});

  @override
  Widget build(BuildContext context) {
    return Text(metin);
  }
}

Practice Print stateless info

In main, print two lines with print: stateless statik and build gorunum.

Starter code

main.dart
void main() {
  // iki satır yazdır:
  // stateless statik
  // build gorunum

}

Sample solution

main.dart
void main() {
  print('stateless statik');
  print('build gorunum');
}

Expected output

stateless statik
build gorunum

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 When is StatelessWidget preferred?

  • Only for animated widgets
  • For widgets exchanging data with a server
  • Only for static views based on constructor params Correct answer
  • For widgets with constantly changing state

Explanation: StatelessWidget is for widgets without state that rely only on constructor params; data that changes like a counter requires StatefulWidget.

2 Stateless class definition

Question
class Selam extends ____ {
  @override
  Widget build(BuildContext context) {
    return Text('Selam');
  }
}
Answer
class Selam extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Text('Selam');
  }
}

Explanation: Classes extending StatelessWidget define widgets without state.

3 StatelessWidget concepts

Term Meaning
StatelessWidget Stateless widget class
değişmez örnek Not updated outside constructor
no state No internal data stored
statik görünüm Suitable for StatelessWidget

All lessons in Beginner