Flutter / Introduction to Flutter

Widget Usage

This tutorial focuses on how to use widgets in Flutter. It covers understanding what a widget is, the different types of widgets, and how to use them in a Flutter app.

Tutorial 1 of 4 4 resources in this section

Section overview

4 resources

Introduce the Flutter SDK and its basic principles.

Widget Usage in Flutter

Introduction

In this tutorial, we aim to provide a comprehensive guide on how to use widgets in Flutter.

By the end of this tutorial, you will learn:
- What a widget is in Flutter
- Different types of widgets and their usage
- How to use them in a Flutter application

Prerequisites: Basic understanding of Flutter and Dart Programming language. Familiarity with object-oriented programming will be helpful.

Step-by-Step Guide

What is a Widget?

In Flutter, everything is a widget. Widgets are the basic building blocks of a Flutter application's user interface. It describes what the view should look like given its current configuration and state.

Types of Widgets

There are mainly two types of widgets in Flutter:
1. Stateless Widgets: These are static widgets. They describe a part of the user interface which can't change over time.
2. Stateful Widgets: These are dynamic widgets. They can change dynamically, i.e., they can redraw themselves multiple times within their life cycle.

Using Widgets

Widgets are used in a tree of nodes called the Widget Tree. This tree consists of two types of objects: Widgets and Elements. A Widget is an immutable description of part of the user interface; an Element is a mutable instantiation of a Widget over time.

Code Examples

Stateless Widget Example

Here's an example of a stateless widget that creates a simple Material App.

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

// Creating a stateless widget
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter Stateless Widget Example'),
        ),
        body: Center(
          child: Text('Hello World!'),
        ),
      ),
    );
  }
}

Stateful Widget Example

Here's an example of a stateful widget that creates a counter.

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

// Creating a stateful widget
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: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter Stateful Widget Example'),
        ),
        body: Center(
          child: Text('Counter value: $_counter'),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: _incrementCounter,
          tooltip: 'Increment',
          child: Icon(Icons.add),
        ),
      ),
    );
  }
}

Summary

In this tutorial, we have learned about widgets, the two types of widgets, and how to use them in a Flutter app.

For further learning, you can explore more about managing state in stateful widgets and the widget lifecycle.

Practice Exercises

  1. Create a stateless widget which displays 'Hello, Flutter!' on the screen.
  2. Create a stateful widget that has a button that changes the text displayed on the screen when clicked.
  3. Create a stateful widget that has a counter which increments when a button is pressed and displays the count on the screen.

Solutions:
1. This exercise is similar to the Stateless Widget example provided above.
2. & 3. These exercises are similar to the Stateful Widget example provided above. Just replace the counter with the required functionality.

Happy Fluttering!

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

HTML Minifier & Formatter

Minify or beautify HTML code.

Use tool

Countdown Timer Generator

Create customizable countdown timers for websites.

Use tool

Favicon Generator

Create favicons from images.

Use tool

Backlink Checker

Analyze and validate backlinks.

Use tool

Random Number Generator

Generate random numbers between specified ranges.

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