CodeBlanks Logo Icon CodeBlanks

What is a widget?

Learn that a widget is the core building block of Flutter.

What is a widget?

A widget is Flutter's fundamental building block. Everything on screen — text, button, padding — is a widget. Widgets are immutable: they do not change after creation. Each widget has a build method that produces its appearance.

Text('Merhaba')
Padding(
  padding: EdgeInsets.all(8.0),
  child: Text('Dolu'),
)

The build method

Every widget's build method describes how it appears on screen. build takes a BuildContext and returns a Widget. When the widget changes, build is called again and returns a new widget tree.

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

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

Practice Print the build signature

In main, print two lines with print: donus Widget and parametre BuildContext.

Starter code

main.dart
void main() {
  // iki satır yazdır:
  // donus Widget
  // parametre BuildContext

}

Sample solution

main.dart
void main() {
  print('donus Widget');
  print('parametre BuildContext');
}

Expected output

donus Widget
parametre BuildContext

Questions and answers

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

1 What property do widgets have?

  • Run on the server
  • Mutable at runtime
  • Can only display text
  • Immutable after creation Correct answer

Explanation: Widgets are immutable; after creation their content cannot change. For changes a new widget is produced.

2 build return type

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

Explanation: The build method returns a value of type Widget.

3 Widget concepts

Term Meaning
Widget Immutable building block
immutable Does not change after creation
build Method that produces the appearance
StatelessWidget Class that implements build

All lessons in Beginner