Flutter – Line ProgressBar with Card Widget using Buttons
Last Updated :
26 Dec, 2023
In this Flutter application, we’ve created a user interface using a Card with a linear progress bar. The progress bar is contained within the card, and three buttons (Decrease, Increase, and Reset) allow users to dynamically control the progress displayed. The LinearProgressIndicator widget visually represents the progress as a line, with its state-managed through the Flutter framework. In this article, we are going to create a Line ProgressBar with a Card Widget using Buttons. A sample video is given below to get an idea about what we are going to do in this article.
Step By Step Implementation
Step 1: Create a New Project in Android Studio
To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.
Step 2: Import the Package
First of all import material.dart file.
import 'package:flutter/material.dart';
Step 3: Execute the main Method
Here the execution of our app starts.
Dart
void main() {
runApp(MyApp());
}
|
Step 4: Create MyApp Class
In this class we are going to implement the MaterialApp and the Scaffold , here we are also set the Theme of our App.
Dart
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.green,
),
debugShowCheckedModeBanner: false ,
home: MyHomePage(),
);
}
}
|
Step 5: Create MyHomePage Class
The MyHomePage class defines a Flutter widget with a user interface that includes a card with a linear progress indicator and buttons to control the progress—allowing users to decrease, increase, or reset the progress level. The state management ensures that the UI reflects the current state of the progress variable.
Dart
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
double progress = 0.0;
void updateProgress( double value) {
setState(() {
progress = value;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text( 'Line ProgressBar with CardView' ),
),
body: Center(
child: Card(
elevation: 5.0,
margin: EdgeInsets.all(16.0),
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
LinearProgressIndicator(
value: progress,
backgroundColor: Colors.grey,
valueColor: AlwaysStoppedAnimation<Color>(Colors.green),
),
SizedBox(height: 16.0),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
onPressed: () {
if (progress > 0) {
updateProgress(progress - 0.1);
}
},
child: Text( 'Decrease' ),
),
ElevatedButton(
onPressed: () {
if (progress < 1.0) {
updateProgress(progress + 0.1);
}
},
child: Text( 'Increase' ),
),
ElevatedButton(
onPressed: () {
updateProgress(0.0);
},
child: Text( 'Reset' ),
),
],
),
],
),
),
),
),
);
}
}
|
Here is the full Code of main.dart file
Dart
import 'package:flutter/material.dart' ;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.green,
),
debugShowCheckedModeBanner: false ,
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
double progress = 0.0;
void updateProgress( double value) {
setState(() {
progress = value;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text( 'Line ProgressBar with CardView' ),
),
body: Center(
child: Card(
elevation: 5.0,
margin: EdgeInsets.all(16.0),
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
LinearProgressIndicator(
value: progress,
backgroundColor: Colors.grey,
valueColor: AlwaysStoppedAnimation<Color>(Colors.green),
),
SizedBox(height: 16.0),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
onPressed: () {
if (progress > 0) {
updateProgress(progress - 0.1);
}
},
child: Text( 'Decrease' ),
),
ElevatedButton(
onPressed: () {
if (progress < 1.0) {
updateProgress(progress + 0.1);
}
},
child: Text( 'Increase' ),
),
ElevatedButton(
onPressed: () {
updateProgress(0.0);
},
child: Text( 'Reset' ),
),
],
),
],
),
),
),
),
);
}
}
|
Output:
Share your thoughts in the comments
Please Login to comment...