CodeBlanks Logo Icon CodeBlanks

Summary - Widget basics

Combine widget, tree and BuildContext to build a simple UI.

Widget basics summary

The Flutter UI is a tree of immutable widgets. Each widget's build method produces its appearance and uses BuildContext to reach ancestral data in the tree. You start with a Material or Cupertino root widget and nest childs to build the UI.

MaterialApp(
  home: Scaffold(
    body: Center(
      child: Builder(
        builder: (context) => Text(
          'Merhaba',
          style: Theme.of(context).textTheme.bodyLarge,
        ),
      ),
    ),
  ),
)

Practice Build the tree and run

In main, first print a message with print('agac kurulu'). Then call runApp with a MaterialApp whose home contains a Center wrapping Text('Hazir').

Starter code

main.dart
import 'package:flutter/material.dart';

void main() {
  // 'agac kurulu' mesajını print ile yazdır

  // runApp ile bir MaterialApp çağır; home içinde Center ve Text kullan

}

Sample solution

main.dart
import 'package:flutter/material.dart';

void main() {
  print('agac kurulu');
  runApp(MaterialApp(home: Center(child: Text('Hazir'))));
}

Expected output

agac kurulu

Questions and answers

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

1 What is the parameter of the build method?

  • Color
  • String
  • int
  • BuildContext Correct answer

Explanation: The build method signature is Widget build(BuildContext context).

2 Complete the build parameter

Question
Widget build(BuildContext ____) {
  return Text('Hazir');
}
Answer
Widget build(BuildContext context) {
  return Text('Hazir');
}

Explanation: The parameter passed to the build method is conventionally named context.

3 Widget basics combined

Term Meaning
build imzası Widget build(BuildContext context)
Ağaç yürüyüşü How Flutter produces pixels
Tema erişimi Via Theme.of(context)
Görsel dil seçimi Material or Cupertino

All lessons in Beginner