Flutter University
  • πŸ‘‹Welcome to Flutter University
  • Learn Flutter
    • πŸš—Basics
      • 🧺Dart Basics
      • πŸš›Setup & Installation
      • 🐣Hello Flutter
      • πŸŒ‰Widgets
      • ⛸️Basic State Management
      • πŸ‡ΎπŸ‡ΉBasic Layout and Styling
      • 🐝Basic Interactivity
      • πŸ›£οΈNavigation
      • πŸͺ„Working with Assets
    • πŸš…Intermediate
      • 🎯Deeper into Dart
      • ⭐More on State Management
      • πŸ“ƒForm Handling
      • πŸ—ΌNetworking
      • πŸŽ‡Persistence
      • πŸ§™β€β™‚οΈAnimations
      • πŸ§ͺTesting
      • πŸ“¦Package Management
    • ✈️Professional
      • πŸŽ“Advanced Animations
      • 🎨Custom Painters
      • 🐼Continuous Integration/Continuous Deployment (CI/CD)
      • 🎭Performance Profiling
      • πŸ”¬Native Integrations
      • 🌍Accessibility and Localization
      • 🀘Understanding Design Patterns
      • πŸ“šFlutter Architecture
        • The Layer Model
        • Reactive User Interfaces
        • Flutter Widgets
        • The Rendering Process
        • Platform Embedders Overview
        • Integrating with Other Code
        • Support for the Web
  • Tutorials
    • 🌈UI
      • 🏚️Clubhouse Clone
      • πŸ”‰Netflix Clone
    • βš”οΈFull Stack
    • ⛓️Blockchain
    • πŸ€–AI/ML
  • Miscellaneous
    • πŸ–₯️100 Days of Flutter
    • 🎨Join Community
Powered by GitBook
On this page
  • How Widgets Organize Themselves
  • Example of a Simple Widget Tree
  • Updating the UI
  • Flutter's Approach to UI
  • Widget Composition
  • Building Widgets
  • Stateless vs. Stateful Widgets
  • Managing State
  • Key Takeaways

Was this helpful?

  1. Learn Flutter
  2. Professional
  3. Flutter Architecture

Flutter Widgets

Imagine widgets as the building blocks of a Flutter app's user interface. Everything you see in a Flutter app, from buttons to padding and even the app itself, is a widget. Widgets describe what their view should look like with their current configuration and state.

How Widgets Organize Themselves

Widgets in Flutter are organized in a tree-like hierarchy. This means every widget nests inside a parent widget, receiving a context that helps it know where it is within the overall widget tree. The tree starts with a root widget, which could be a MaterialApp or CupertinoApp widget, setting the stage for the entire application.

Example of a Simple Widget Tree

Consider this basic Flutter app structure and it's widget tree:

import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('My Home Page'),
        ),
        body: Center(
          child: Column(
            children: [
              const Text('Hello World'),
              const SizedBox(height: 20),
              ElevatedButton(
                onPressed: () {
                  print('Click!');
                },
                child: const Text('A button'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

This code snippet introduces us to a simple Flutter app containing a MaterialApp with a Scaffold, displaying a text and a button on the screen. Each element, from the MaterialApp to the Text widget, plays a specific role, contributing to the app's overall UI.

Updating the UI

Flutter apps respond to events like user interactions by replacing widgets in the UI with new widget instances. Flutter's engine then compares the new widgets with the old ones and updates the UI where necessary. This efficient method ensures smooth and fast app performance.

Flutter's Approach to UI

Flutter does not rely on the native UI components of the platform it runs on. Instead, it uses its own set of widgets. This approach has several benefits:

  • Customization: Developers can easily customize widgets to fit their needs.

  • Performance: Drawing the UI directly in Flutter removes the need to communicate with the platform's native components, leading to better performance.

  • Consistency: The app's appearance remains consistent across all platforms.

Widget Composition

In Flutter, complex widgets are built by combining many simpler, single-purpose widgets. This compositional approach encourages reusability and simplifies the process of creating custom widgets. For instance, the Container widget, which is commonly used for decoration and alignment, is composed of several simpler widgets like Padding, Align, and DecoratedBox.

Building Widgets

Widgets are defined by their build() method, which describes how the widget looks and functions. Stateless widgets are simple and immutable, while Stateful widgets can change over time, responding to user input or other factors.

Stateless vs. Stateful Widgets

  • Stateless Widgets: Do not change over time. Examples include text labels and icons.

  • Stateful Widgets: Can change based on interactions or internal state changes. They use the setState() method to trigger a rebuild with new data.

Managing State

Key Takeaways

  • Widgets are the fundamental UI elements in Flutter.

  • They are organized in a hierarchical structure.

  • Flutter's engine updates the UI by replacing old widgets with new ones as needed.

  • Widgets can be either stateless (immutable) or stateful (mutable).

  • Effective state management is essential for creating dynamic and complex applications.

  • Flutter's unique approach to UI design allows for unparalleled customization and consistency across platforms.

Last updated 1 year ago

Was this helpful?

Managing state is crucial in Flutter apps. Local state management is straightforward with stateful widgets, but as apps grow, global state management becomes necessary. Flutter offers several ways to manage state effectively, including using the package, , and other state management solutions like . This is a huge topic in itself, we'll cover this in a separate article.

✈️
πŸ“š
Provider
InheritedWidget
flutter_bloc
Widget tree of the example code.