← Back to list

Understanding Flutter Widgets: A Beginner’s Guide

Flutter is a powerful framework for building cross-platform applications, and at the heart of Flutter lies widgets. Everything in Flutter…

Sasikumar in Towards Dev · 2024-11-30 04:57 · 0 claps · 2.2 min read paywalled
#flutter-tutorial #flutter-widget #flutter-stateful-widget #flutter-stateless-widget #mobile-app-development
Open on Medium ↗
Wiki topics: 📱 · Mobile Development

Understanding Flutter Widgets: A Beginner’s Guide

Photo by Arif Riyanto on Unsplash

Photo by Arif Riyanto on Unsplash

Flutter is a powerful framework for building cross-platform applications, and at the heart of Flutter lies widgets. Everything in Flutter is a widget — buttons, text, layouts, and even the app itself. Understanding widgets and their hierarchy is crucial for mastering Flutter development.

What Are Widgets?

Widgets in Flutter are building blocks used to create the user interface of an app. They describe the how (structure and appearance) of the UI and are organized into a widget tree.

Key Features of Widgets

  • Immutable: Once created, widgets cannot be modified. To reflect changes, you create a new widget.
  • Composable: Widgets can be nested to build complex UIs.

Types of Widgets

There are two primary types of widgets in Flutter:

  1. Stateless Widgets: These widgets are immutable and don’t store any state. They are ideal for static UI elements.
  2. Stateful Widgets: These widgets maintain state, allowing the UI to update dynamically in response to changes.

Stateless Widgets

Stateless widgets are used for components that don’t change over time.

Here’s an example of a simple StatelessWidget:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Stateless Widget Example'),
        ),
        body: Center(
          child: Text(
            'Hello, Flutter!',
            style: TextStyle(fontSize: 24),
          ),
        ),
      ),
    );
  }
}

Explanation

  • MyApp extends StatelessWidget.
  • The build method returns a widget tree describing the layout.
  • The text “Hello, Flutter!” remains static.

Stateful Widgets

Stateful widgets are used for components that change over time, like user interactions or dynamic data.

Here’s an example of a simple StatefulWidget:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Stateful Widget Example'),
        ),
        body: CounterWidget(),
      ),
    );
  }
}

class CounterWidget extends StatefulWidget {
  @override
  _CounterWidgetState createState() => _CounterWidgetState();
}

class _CounterWidgetState extends State<CounterWidget> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Text(
          'You have pressed the button this many times:',
        ),
        Text(
          '$_counter',
          style: TextStyle(fontSize: 24),
        ),
        ElevatedButton(
          onPressed: _incrementCounter,
          child: Text('Increment'),
        ),
      ],
    );
  }
}

Explanation

  • CounterWidget extends StatefulWidget.
  • The _CounterWidgetState class manages the state.
  • setState is called to notify Flutter of changes, causing the UI to rebuild.

Widget Hierarchy

In Flutter, widgets are organized into a widget tree. The tree starts with the root widget (usually MaterialApp or CupertinoApp) and grows with child widgets.

Example of a Nested Widget Tree:

@override
Widget build(BuildContext context) {
  return MaterialApp(
    home: Scaffold(
      appBar: AppBar(
        title: Text('Nested Widgets Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text('Parent Widget'),
            ElevatedButton(
              onPressed: () {},
              child: Text('Child Widget'),
            ),
          ],
        ),
      ),
    ),
  );
}

Conclusion

Widgets are the foundation of Flutter apps. Understanding the difference between stateless and stateful widgets, along with their hierarchical structure, is the first step to building amazing UIs with Flutter.

Key Takeaways

  • Use StatelessWidget for static elements.
  • Use StatefulWidget for dynamic, interactive components.
  • Always think of your UI as a tree of widgets.

Mastering widgets is a journey, but with practice, you’ll find building complex UIs with Flutter both intuitive and rewarding.


메타데이터
post_id
fb30f59d8b4f
slug
understanding-flutter-widgets-a-beginners-guide-fb30f59d8b4f
url
https://towardsdev.com/understanding-flutter-widgets-a-beginners-guide-fb30f59d8b4f
canonical_url
https://towardsdev.com/understanding-flutter-widgets-a-beginners-guide-fb30f59d8b4f
author_url
https://medium.com/@sasicse990
status
ok
fetched_at
2026-08-21 22:58:37