Learn how to build a simple counter app using Flutter's StatefulWidget and setState for state management. This project introduces young coders to the basics of stateful programming in Flutter.
Welcome to the exciting world of Flutter state management! In this mini-project, we’ll guide you through creating a simple yet powerful counter app. This app will help you understand how to manage state in Flutter using StatefulWidget
and setState
. By the end of this project, you’ll have a fun app that displays a number on the screen and allows you to increase or decrease it with the click of a button.
Our counter app will have a number displayed prominently on the screen. You’ll be able to increase or decrease this number using buttons. We’ll also add a reset button to set the count back to zero. This project will teach you how to update the user interface dynamically as the app’s state changes.
First, let’s create a new Flutter project. Open your terminal or command prompt and run the following command:
flutter create counter_app
Navigate into the project directory:
cd counter_app
Open the project in your preferred code editor.
Now, let’s create the main widget for our app. Replace the contents of lib/main.dart
with the following code:
import 'package:flutter/material.dart';
class CounterApp extends StatefulWidget {
@override
_CounterAppState createState() => _CounterAppState();
}
class _CounterAppState extends State<CounterApp> {
int count = 0;
void increment() {
setState(() {
count++;
});
}
void decrement() {
setState(() {
count--;
});
}
void reset() {
setState(() {
count = 0;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Counter App'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Count:',
style: TextStyle(fontSize: 24),
),
Text(
'$count',
style: TextStyle(fontSize: 48, fontWeight: FontWeight.bold),
),
SizedBox(height: 20),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: increment,
child: Text('Increase'),
),
SizedBox(width: 20),
ElevatedButton(
onPressed: decrement,
child: Text('Decrease'),
),
SizedBox(width: 20),
ElevatedButton(
onPressed: reset,
child: Text('Reset'),
),
],
),
],
),
),
);
}
}
void main() => runApp(MaterialApp(
home: CounterApp(),
));
To see your app in action, run the following command in your terminal:
flutter run
This will launch the app on an emulator or connected device. Try pressing the “Increase” and “Decrease” buttons to see the count change. The “Reset” button will set the count back to zero.
Let’s make our app even more exciting with a few enhancements:
Styling Buttons: You can customize the buttons by changing their colors or shapes. For example, you can make the “Increase” button green and the “Decrease” button red to visually distinguish their functions.
Add Animations: Introduce simple animations to make the count change more visually appealing. You can use Flutter’s built-in animation widgets to achieve this.
Here’s how you can style the buttons:
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.green, // Background color
),
onPressed: increment,
child: Text('Increase'),
),
It’s important to test your app thoroughly. Make sure all buttons work as expected and the UI updates correctly. If you encounter any issues, use Flutter’s debugging tools to identify and fix them.
Here’s the complete code for your counter app, including enhancements:
import 'package:flutter/material.dart';
class CounterApp extends StatefulWidget {
@override
_CounterAppState createState() => _CounterAppState();
}
class _CounterAppState extends State<CounterApp> {
int count = 0;
void increment() {
setState(() {
count++;
});
}
void decrement() {
setState(() {
count--;
});
}
void reset() {
setState(() {
count = 0;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Counter App'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Count:',
style: TextStyle(fontSize: 24),
),
Text(
'$count',
style: TextStyle(fontSize: 48, fontWeight: FontWeight.bold),
),
SizedBox(height: 20),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.green,
),
onPressed: increment,
child: Text('Increase'),
),
SizedBox(width: 20),
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.red,
),
onPressed: decrement,
child: Text('Decrease'),
),
SizedBox(width: 20),
ElevatedButton(
onPressed: reset,
child: Text('Reset'),
),
],
),
],
),
),
);
}
}
void main() => runApp(MaterialApp(
home: CounterApp(),
));
Now it’s your turn to get creative! Here are some ideas to customize your counter app:
Below is a simple diagram showing the structure of your counter app:
graph TD; A[CounterApp] --> B[StatefulWidget]; B --> C[_CounterAppState]; C --> D[Scaffold]; D --> E[AppBar]; D --> F[Center]; F --> G[Column]; G --> H[Text: Count]; G --> I[Text: $count]; G --> J[Row]; J --> K[ElevatedButton: Increase]; J --> L[ElevatedButton: Decrease]; J --> M[ElevatedButton: Reset];
This diagram illustrates how the different components of your app are connected.