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.
Section overview
5 resourcesExplore 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
- 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.
- Exercise 2: Modify the app from Exercise 1 to include a FloatingActionButton that, when pressed, changes the color of the Container.
Solutions:
- 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,
),
),
),
));
}
- 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.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI 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 articleAI 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 articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article