Overview of Commonly Used Flutter Widgets

Tutorial 3 of 5

1. Introduction

In this tutorial, we aim to provide an overview of some of the most commonly used widgets in Flutter and how to use them effectively in your app. By the end of this tutorial, you will have a solid understanding of Flutter widgets and how to use them to create beautiful and interactive user interfaces.

What you will learn:
- Understanding of Flutter widgets
- How to use some of the most commonly used Flutter widgets
- Best practices in implementing Flutter widgets

Prerequisites:
- Basic understanding of Dart programming language
- Basic knowledge of Flutter

2. Step-by-Step Guide

Widgets are the basic building blocks of a Flutter application's user interface. Each widget is an immutable declaration of part of the user interface. Flutter includes a modern react-style framework, a 2D rendering engine, ready-made widgets, and development tools. These components work together to help you design, build, test, and debug apps.

Let's delve into some of the most commonly used Flutter widgets:

2.1 MaterialApp

The MaterialApp is usually the root of your app that directly interacts with the OS. It follows material design principles.

void main() {
  runApp(MaterialApp(
    home: Scaffold(
      appBar: AppBar(
        title: Text('My First App'),
      ),
      body: Text('Hello world'),
    ),
  ));
}

2.2 Scaffold

Scaffold is a top-level container that provides a structure to the app. It can hold various material widgets such as AppBar, BottomNavigationBar, FloatingActionButton, etc.

Scaffold(
  appBar: AppBar(
    title: Text('My First App'),
  ),
  body: Text('Hello world'),
);

2.3 Container

A Container is a convenient widget that combines common painting, positioning, and sizing widgets. It can take only one child and align it within itself.

Container(
  margin: const EdgeInsets.all(10.0),
  color: Colors.amber[600],
  width: 48.0,
  height: 48.0,
);

3. Code Examples

Here is a simple example of how you can use these widgets in a practical setting.

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    home: Scaffold(
      appBar: AppBar(
        title: Text('My First App'),
      ),
      body: Center(
        child: Container(
          margin: const EdgeInsets.all(10.0),
          color: Colors.amber[600],
          width: 48.0,
          height: 48.0,
          child: Text('Hello world'),
        ),
      ),
    ),
  ));
}

In this code:
- We first import the required Flutter material package.
- We call the runApp method and pass in an instance of MaterialApp.
- Inside MaterialApp, we define Scaffold as the home.
- The AppBar widget inside Scaffold creates a Material Design app bar.
- The body of the Scaffold holds a Container.
- The Container has a margin of 10.0, a color of amber[600], and dimensions 48.0 x 48.0.
- Inside the Container, we have a Text widget that displays 'Hello world'.

4. Summary

We've covered some of the most commonly used widgets in Flutter i.e., MaterialApp, Scaffold, and Container. You now know how to use these widgets to create basic user interfaces in Flutter.

5. Practice Exercises

  1. Exercise 1: Create a Flutter app with an AppBar entitled 'Practice App' and a body that contains a centered blue square Container of size 100.0 x 100.0.
  2. Exercise 2: Modify the app from Exercise 1 to include a FloatingActionButton that, when pressed, changes the color of the Container.

Solutions:

  1. Solution to Exercise 1:
void main() {
  runApp(MaterialApp(
    home: Scaffold(
      appBar: AppBar(
        title: Text('Practice App'),
      ),
      body: Center(
        child: Container(
          color: Colors.blue,
          width: 100.0,
          height: 100.0,
        ),
      ),
    ),
  ));
}
  1. Solution to Exercise 2:
void main() {
  runApp(MyApp());
}

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

class _MyAppState extends State<MyApp> {
  Color containerColor = Colors.blue;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Practice App'),
        ),
        body: Center(
          child: Container(
            color: containerColor,
            width: 100.0,
            height: 100.0,
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            setState(() {
              containerColor = Colors.red;
            });
          },
          child: Icon(Icons.color_lens),
        ),
      ),
    );
  }
}

For the next steps, learn how to use other Flutter widgets like ListView, GridView, Stack, etc. You can refer to the official Flutter documentation for more details and examples.