Flutter / Flutter Widgets

Introduction to Widgets in Flutter

This tutorial introduces the concept of Widgets in Flutter. You will learn what they are, how they work, and why they are so central to Flutter apps.

Tutorial 1 of 5 5 resources in this section

Section overview

5 resources

Explore the concept of Widgets, the basic building blocks for UI in Flutter.

Introduction

Welcome to this tutorial on Widgets in Flutter! The goal of this tutorial is to introduce you to the concept of widgets in Flutter, understand how they work, and why they are so central to Flutter apps.

By the end of this tutorial, you will be able to:

  • Explain what widgets are in Flutter
  • Understand how widgets work in Flutter
  • Build basic Flutter apps using different types of widgets

Prerequisites

  • Basic knowledge of Dart programming language
  • Flutter SDK installed on your machine
  • An IDE that supports Flutter such as Visual Studio Code or Android Studio

Step-by-Step Guide

Widgets are the basic building blocks of a Flutter app's user interface. Each widget is an immutable declaration of part of the user interface. Widgets can either be stateless or stateful.

Stateless Widgets

A stateless widget describes part of the user interface which can depend on configuration information in the widget itself and in the widget’s parent but does not depend on any runtime state.

Stateless widgets are created, then drawn once and never redrawn. They are static and cannot change once rendered.

Stateful Widgets

Stateful widgets maintain state that might change during the lifetime of the widget. Implementing a stateful widget requires at least two classes: 1) a StatefulWidget class that creates an instance of 2) a State class.

Stateful widgets are dynamic. The user interface can change dynamically when the state changes.

Code Examples

Here are some code examples to help you understand widgets better:

Stateless Widget Example

import 'package:flutter/material.dart';

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

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

In this code snippet:

  • We import the flutter material package, which is a UI toolkit for building beautiful Flutter apps.
  • We define a main function that runs our MyApp widget.
  • MyApp is a StatelessWidget, which will be drawn on the screen once and will not redraw unless the configuration changes.
  • MaterialApp, Scaffold, AppBar, and Text are all widgets.
  • The Text widget has a property of 'Hello Flutter'.

When you run this code, it will display a simple app with an app bar and text in the center of the screen that says 'Hello Flutter'.

Stateful Widget Example

import 'package:flutter/material.dart';

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

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  int counter = 0;

  void incrementCounter() {
    setState(() {
      counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Counter App',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Counter App'),
        ),
        body: Center(
          child: Text('Counter: $counter'),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: incrementCounter,
          child: Icon(Icons.add),
        ),
      ),
    );
  }
}

In this code snippet:

  • MyApp is a StatefulWidget, which means it can redraw its UI when its state changes.
  • _MyAppState is the state object for MyApp.
  • The counter variable is the state that the widget maintains.
  • The incrementCounter function changes the state. When the state changes, the build method is called again with the updated state, so the UI reflects the latest state.

When you run this code, it will display a simple counter app. The counter increases each time you press the floating action button.

Summary

In this tutorial, we covered:

  • What widgets are in Flutter
  • The difference between Stateless and Stateful widgets
  • How to create Stateless and Stateful widgets

Next steps for learning:

  • Explore more types of widgets in Flutter
  • Learn how to use widget composition to build complex UI

Additional resources:

  • Flutter documentation: https://flutter.dev/docs/development/ui/widgets

Practice Exercises

  1. Create a Stateless widget that displays 'Welcome to Flutter' on the screen.
  2. Create a Stateful widget that displays a counter. The counter should decrease each time a button is pressed.

Solutions and explanations for these exercises are left for you to try. Remember, practice is key to mastering Flutter widgets. Keep practicing and building awesome apps!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

QR Code Generator

Generate QR codes for URLs, text, or contact info.

Use tool

Backlink Checker

Analyze and validate backlinks.

Use tool

CSS Minifier & Formatter

Clean and compress CSS files.

Use tool

URL Encoder/Decoder

Encode or decode URLs easily for web applications.

Use tool

Text Diff Checker

Compare two pieces of text to find differences.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help