Basic Components of a Widget Tree in Flutter
Root Widget:
Intermediate Widgets:
Scaffold: Provides a basic structure with app bar, body, and drawers.
Column and Row: Arrange child widgets vertically or horizontally.
Center: Centers its child within its available space.
Container: Provides padding, margins, and background colors.
Leaf Widgets:
Text: Displays text.
Image: Displays images.
Icon: Displays icons.
ElevatedButton: A clickable button.
Key Concepts Hierarchy:
Example:
Dart
import 'package:flutter/material.dart';
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold( // Intermediate widget
appBar: AppBar( // Intermediate widget
title: Text('My App'), // Leaf widget
),
body: Center( // Intermediate widget
child: Text('Hello, Flutter!'), // Leaf widget
),
);
}
}
In this example, Scaffold is the root widget, followed by AppBar and Center as intermediate widgets, and finally Text as leaf widgets.
By understanding these basic components and how they interact, you can effectively build complex and dynamic user interfaces in Flutter.