Flutter / Flutter Widgets

Overview of Commonly Used Flutter Widgets

This tutorial provides an overview of some of the most commonly used widgets in Flutter. You will learn when and how to use these widgets in your own apps.

Tutorial 3 of 5 5 resources in this section

Section overview

5 resources

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

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.

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

Percentage Calculator

Easily calculate percentages, discounts, and more.

Use tool

Date Difference Calculator

Calculate days between two dates.

Use tool

EXIF Data Viewer/Remover

View and remove metadata from image files.

Use tool

Image Converter

Convert between different image formats.

Use tool

Age Calculator

Calculate age from date of birth.

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