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
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:
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'),
),
));
}
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'),
);
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,
);
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'.
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.
Solutions:
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,
),
),
),
));
}
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.