Getting Started with Flutter Widgets: Building Your First UI
Flutter is an open-source UI toolkit that allows developers to create natively compiled applications for mobile, web, and desktop from a single codebase. Whether you’re building a simple app or a complex interface, Flutter’s powerful widgets make UI creation simple and efficient. In this blog, we’ll walk you through the core concepts of Flutter’s UI development, covering topics such as the widget tree, hot reload, rows and columns, padding, margins, and more.

The Widget Tree
Flutter applications are composed of widgets, which are the basic building blocks of a Flutter app. Every UI element is a widget, including layout elements like rows and columns, text, buttons, and images. These widgets form a hierarchical structure called the widget tree.
Understanding the Widget Tree
In Flutter, everything is a widget. Widgets form the structure of your app and can be nested inside one another, creating what’s known as the widget tree. Each widget in this tree can have child widgets, resulting in a hierarchy.
Example: A Simple Widget Tree
Let’s create a basic widget tree with a Scaffold
, AppBar
, and a centered text widget.
Scaffold(
appBar: AppBar(
title: Text('First UI'),
),
body: Center(
child: Text('Hello Flutter!'),
),
);
Here, the Scaffold
is the root of our widget tree, containing an AppBar
at the top and a centered Text
widget as the body.


Basic UI Components
Flutter provides a rich set of widgets to help you build interactive and responsive UIs. Some of the essential widgets include:
- Text: Displays text.
- Button: Multiple types are available, like
ElevatedButton
for clickable actions. - Column and Row: These are layout widgets that arrange their children vertically and horizontally, respectively.
Example: Creating a Simple Layout
Here’s how you can combine these widgets into a simple layout with a text label and button:
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Welcome to Flutter!'),
ElevatedButton(
onPressed: () {},
child: Text('Click Me'),
),
],
);
This code creates a column layout with a text label on top and a clickable button below it.
Stateful vs. Stateless Widgets
Flutter widgets are divided into two types: Stateless and Stateful.
- Stateless Widgets are immutable, meaning their state can’t change during runtime.
- Stateful Widgets can change their state dynamically and rebuild the UI when the state changes.
Example: Converting a Widget from Stateless to Stateful
Let’s modify our button to update a counter every time it’s pressed by converting our widget from stateless to stateful.
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Counter App'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Button pressed $_counter times'),
ElevatedButton(
onPressed: _incrementCounter,
child: Text('Press Me'),
),
],
),
),
);
}
}
In this example, the _incrementCounter
function updates the _counter
variable whenever the button is pressed. The setState()
method ensures the UI is rebuilt to reflect the updated counter.
Hot Reload: A Developer’s Best Friend
One of Flutter’s most powerful features is Hot Reload. It allows developers to instantly see changes they make in the code without restarting the app. This rapid feedback cycle speeds up the development process and makes it easier to experiment with different UI designs.
To use hot reload, simply save your changes, and the updated UI will immediately reflect in your app.
Creating Layouts with Rows and Columns
Layouts in Flutter are managed using widgets like Row
and Column
. The Row
widget arranges its children horizontally, while the Column
widget arranges its children vertically.
Example: Using Row and Column Widgets
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Column(
children: [
Icon(Icons.phone, size: 40),
Text('Phone'),
],
),
Column(
children: [
Icon(Icons.email, size: 40),
Text('Email'),
],
),
Column(
children: [
Icon(Icons.share, size: 40),
Text('Share'),
],
),
],
);
In this example, we combined Row
and Column
widgets to create a structured layout with icons and labels.
Spacing and Alignment: Padding and Margins
To make your UI look clean, you often need to add spacing around widgets. Flutter provides two common ways to handle this: padding and margins.
Padding Example
Padding(
padding: const EdgeInsets.all(16.0),
child: Text('This text has padding around it'),
);
Padding adds space inside the boundary of a widget. In this example, 16 pixels of padding are added around the text.
Margins Example
Margins add space outside the boundary of a widget, achieved by wrapping the widget in a Container
.
Container(
margin: const EdgeInsets.all(16.0),
child: Text('This text has margin around it'),
);
This example adds a 16-pixel margin around the text.
Expanded and Flexible Widgets
When you want widgets to take up more or less space within a row or column, you can use the Expanded
and Flexible
widgets. These allow widgets to grow or shrink in size relative to their parent.
Expanded Widget Example
Row(
children: [
Expanded(
child: Container(
color: Colors.red,
child: Text('Red Box'),
),
),
Expanded(
child: Container(
color: Colors.blue,
child: Text('Blue Box'),
),
),
],
);
In this example, two boxes share equal space in a row using Expanded
.
Making Your UI Interactive with Gesture Detectors
Interactivity is key to any modern application, and Flutter provides a GestureDetector
widget to handle user gestures like taps, swipes, and double-taps.
Tap Gesture Example
GestureDetector(
onTap: () {
print('Box tapped!');
},
child: Container(
height: 100,
width: 100,
color: Colors.green,
),
);
In this example, tapping the green box triggers a message in the console. Gesture detection can be applied to any widget, making it easy to build interactive UIs.
Conclusion
In this blog, we’ve walked through the core concepts of UI development in Flutter, including the widget tree, using rows and columns for layout, adding padding and margins for spacing, and creating interactive elements with gesture detectors. With these building blocks, you can create beautiful and responsive Flutter apps.
Explore Other Flutter Topics…
- Introduction to Flutter and Dart
- Why choose Flutter
- Installing Flutter On Your Windows Mac And Linux System
- Your first Flutter app
- Flutter project structure
- Building blocks of Flutter
- Stateful vs. Stateless Widgets Explained
- Flutter layout system
- Flutter text widget
- Creating Buttons in Flutter: ElevatedButton, TextButton, and IconButton
- Handling User Input with Flutter Forms
- Container class in Flutter
- Flutter Navigation
- Flutter – Pass Data One Screen To Another Screen
- Managing Device Orientation in Flutter
- Stateful widget lifecycle in Flutter
- Future of Flutter
- Flutter Themes
- Flutter Animations
- Flutter AppBar Customization
- ListView in Flutter
- Flutter GridView
- Flutter Expanded Widget
- Flutter BottomNavigation Bar
- Floating Action Button
- Drawer Widgets in Flutter
- Form Validation in Flutter
- Flutter TextField
- Adding AdMob ads to a Flutter app
- Building Flutter Web & Desktop Applications
- What is Async and Await in Flutter
- HTTP requests in Flutter
- Parsing JSON in Flutter
- Tinder-Style Swipe Cards in Flutter
- Flutter Tic Tac Toe Game Tutorial
- Flutter Login UI Tutorial
- Flutter Card Widget Tutorial
- Flutter music player app tutorial
- Flutter introduction screens
- Shared Preferences in Flutter
- SQLite Database in Flutter
- Firebase Authentication in Flutter
- Firebase Firestore in Flutter
- Push Notifications in Flutter
- Handling File Uploads in Flutter
- Responsive Design in Flutter
- Provider in Flutter
- Riverpod in Flutter
- Flutter BLoC Pattern Tutorial
Frequently Asked Questions (FAQs)
1. What is a Widget Tree in Flutter?
A widget tree is the hierarchy of widgets that make up the UI in a Flutter app. Each widget nests inside another, forming a tree-like structure.
2. How does Hot Reload work in Flutter?
Hot reload allows developers to make changes to the code and see those changes instantly in the app without restarting. It is one of Flutter’s key features to speed up the development process.
3. What is the difference between Padding and Margin?
Padding adds space inside the boundary of a widget, whereas margin adds space outside the boundary of the widget. Both are used to add spacing in layouts.
4. When should I use Expanded or Flexible?
Use Expanded
when you want a widget to take up all available space in its parent container, and Flexible
when you want more control over how the widget grows or shrinks.