Flutter Widgets 101: A Comprehensive Guide to Building Beautiful UIs

SHARJEEL UR REHMAN
5 min readSep 17, 2023

--

Introduction:

Flutter, Google’s open-source UI toolkit, has taken the mobile app development world by storm. It allows developers to build natively compiled applications for mobile, web, and desktop from a single codebase. One of its most powerful features is the rich set of widgets it provides for building beautiful, responsive, and customizable user interfaces. In this article, we’ll explore some essential Flutter widgets and provide code snippets to demonstrate their usage.

  1. StatelessWidget and StatefulWidget

Widgets in Flutter fall into two main categories: StatelessWidget and StatefulWidget. A StatelessWidget represents immutable UI elements, while a StatefulWidget represents UI elements that can change over time.

class MyStatelessWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
child: Text("I am a StatelessWidget"),
);
}
}

class MyStatefulWidget extends StatefulWidget {
@override
_MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
@override
Widget build(BuildContext context) {
return Container(
child: Text("I am a StatefulWidget"),
);
}
}

In the above code snippets, we have defined two widgets: MyStatelessWidget and MyStatefulWidget. The build method is where the UI of the widget is defined. The MyStatelessWidget returns a Container with a Text widget as its child. Similarly, the MyStatefulWidget returns a Container with a Text widget as its child.

2. Scaffold Widget

The Scaffold widget provides a basic structure for a Material Design app. It includes app bars, navigation drawers, and more.

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('My App'),
),
body: Center(
child: Text('Hello, Flutter!'),
),
),
);
}
}

In this code snippet, we have defined a Scaffold widget inside the MaterialApp widget. The Scaffold widget has an AppBar with a title and a body that contains a centered text.

3. Container Widget

The Container widget is a versatile widget that allows you to create boxes with various properties such as padding, margins, alignment, and more.

Container(
padding: EdgeInsets.all(16.0),
margin: EdgeInsets.all(8.0),
alignment: Alignment.center,
color: Colors.blue,
child: Text('Container Widget'),
)

In this code snippet, we have defined a Container widget with padding, margin, alignment, color properties and a child which is a Text widget.

4. ListView Widget

The ListView widget is perfect for creating scrollable lists of items.

ListView(
children: <Widget>[
ListTile(title: Text('Item 1')),
ListTile(title: Text('Item 2')),
// Add more list items here
],
)

In this code snippet, we have defined a ListView widget with two children which are both ListTile widgets.

5. Card Widget

Card widgets are used to create cards with rounded corners and shadows.

Card(
elevation: 4.0,
margin: EdgeInsets.all(8.0),
child: ListTile(
title: Text('Card Title'),
subtitle: Text('Card Subtitle'),
),
)

In this code snippet, we have defined a Card widget with an elevation property and a child which is a ListTile.

6. RaisedButton and FlatButton Widgets

Buttons are fundamental for user interaction in your app. Flutter provides RaisedButton and FlatButton widgets.

RaisedButton(
onPressed: () {
// Add button click functionality here
},
child: Text('Raised Button'),
)

FlatButton(
onPressed: () {
// Add button click functionality here
},
child: Text('Flat Button'),
)

In these code snippets, we have defined two buttons — RaisedButton and FlatButton — each with an onPressed property (which will contain the functionality to be executed when the button is pressed) and a child which is a Text widget.

Advanced Widgets

Stack Widget:

The Stack widget allows you to overlay several children in a simple way.

Stack(
alignment: const Alignment(0.6, 0.6),
children: [
CircleAvatar(
backgroundImage: AssetImage('images/pic.jpg'),
radius: 100,
),
Container(
decoration:
BoxDecoration(color: Colors.black45),
child: Text(
'Mia B',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
),
],
)

In this code snippet, we have defined a Stack widget with two children — a CircleAvatar and a Container. The Stack widget allows the Container to overlay on top of the CircleAvatar.

GridView Widget

The GridView widget allows you to create a scrollable grid of items.

GridView.count(
crossAxisCount: 2,
children: List.generate(100, (index) {
return Center(
child: Text(
'Item $index',
style: Theme.of(context).textTheme.headline5,
),
);
}),
)

In this code snippet, we have defined a GridView widget with a crossAxisCount of 2, which means it will create a grid with 2 items across its cross axis. The children are generated using the List.generate constructor.

State Management in Flutter

State management is a complex topic in Flutter. It deals with how data flows in your app and how UI updates when data changes. There are several ways to manage state in Flutter, including:

  • Provider: This is the recommended way of managing state by the Flutter team. It uses InheritedWidget in an easy-to-use way.
  • Riverpod: This is a more advanced state management solution that aims to overcome the limitations of Provider.
  • Bloc: This is another popular state management solution in Flutter that uses streams.

Widget Lifecycle

Understanding the lifecycle of widgets, especially StatefulWidgets, is crucial for managing state and resources within those widgets. The lifecycle includes methods like createState, initState, didChangeDependencies, build, didUpdateWidget, and dispose.

Conclusion

Flutter’s extensive library of widgets empowers developers to create stunning and responsive user interfaces with ease. In this article, we’ve explored some essential widgets and provided code snippets to help you get started. As you continue your journey with Flutter, don’t hesitate to explore more widgets and customize them to suit your app’s unique needs. Remember to follow best practices when using these widgets and structuring your widget tree. Happy coding!

Note:
Are you looking to bring your ideas to life? Do you want to create a mobile app that’s both beautiful and functional? If so, you’ve come to the right place. By utilizing my Flutter services, you’ll have access to top-notch mobile app development that’s fast, flexible, and tailor-made to suit your needs. Flutter is a UI toolkit known for its natively compiled applications for mobile from a single codebase. This means you’ll get a stunning, high-performance app that looks and feels beautiful on any mobile platform. So why wait? Bring your vision to life today. Click on the link to get started!

--

--

SHARJEEL UR REHMAN
SHARJEEL UR REHMAN

Written by SHARJEEL UR REHMAN

Experienced Flutter dev with 3 yrs exp. Specializes in mobile apps. Skilled in , Clean Architecture, Bloc,Focuses on scalable, maintainable code.

No responses yet