Flutter - Implement Stepper Widget Inside an AlertDialog
Last Updated :
16 Oct, 2023
The Stepper widget in Flutter is a user interface element used to create a step-by-step process interface. It allows users to progress through a series of steps or stages, with each step typically representing a specific task or some information. An Alert Dialog is a useful way to grab users' attention. Here we can see how to implement an AlertDialog, and then we are going to implement a StepperWidget inside the AlertDialog. In this article, we will implement the AertDialog and StepperWidget. A sample video is given below to get an idea about what we are going to do in this article.
Required Tools
To build this app, you need the following items installed on your machine:
- Visual Studio Code / Android Studio
- Android Emulator / iOS Simulator / Physical Device device.
- Flutter Installed
- Flutter plugin for VS Code / Android Studio.
Step By Step Implementations
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 , here we are also set the Theme of our App.
Dart
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.green, // Set the app's primary theme color
),
title: 'Stepper Example',
home: StepperExample(),
);
}
}
Step 5: Create StepperExample Class
In this class we are going to Implement the Stepper within an alert dialog widget that will show some information in each steps. Comments are added for better understanding.
// Function to show the AlertDialog with the Stepper
void _showStepperDialog() {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Stepper in AlertDialog'),
content: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: SizedBox(
width: 300,
child: Stepper(
currentStep: _currentStep,
onStepContinue: _onStepContinue,
onStepCancel: _onStepCancel,
steps: _steps,
),
),
),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.of(context).pop(); // Close the AlertDialog
},
child: Text('Close'),
),
],
);
},
);
}
Dart
class StepperExample extends StatefulWidget {
@override
_StepperExampleState createState() => _StepperExampleState();
}
class _StepperExampleState extends State<StepperExample> {
int _currentStep = 0;
// Define the steps for the Stepper
List<Step> _steps = [
Step(
title: Text('Step 1'),
content: Text('This is the content of step 1.'),
),
Step(
title: Text('Step 2'),
content: Text('This is the content of step 2.'),
),
Step(
title: Text('Step 3'),
content: Text('This is the content of step 3.'),
),
];
// Callback when the "Continue" button in the Stepper is pressed
void _onStepContinue() {
setState(() {
if (_currentStep < _steps.length - 1) {
_currentStep++;
_showStepperDialog(); // Show the AlertDialog with the updated step
}
});
}
// Callback when the "Cancel" button in the Stepper is pressed
void _onStepCancel() {
setState(() {
if (_currentStep > 0) {
_currentStep--;
_showStepperDialog(); // Show the AlertDialog with the updated step
}
});
}
// Function to show the AlertDialog with the Stepper
void _showStepperDialog() {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Stepper in AlertDialog'),
content: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: SizedBox(
width: 300,
child: Stepper(
currentStep: _currentStep,
onStepContinue: _onStepContinue,
onStepCancel: _onStepCancel,
steps: _steps,
),
),
),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.of(context).pop(); // Close the AlertDialog
},
child: Text('Close'),
),
],
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Stepper in AlertDialog Example'),
),
body: Center(
child: ElevatedButton(
onPressed: _showStepperDialog,
child: Text('Show Stepper Dialog'),
),
),
);
}
}
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(
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.green,
),
title: 'Stepper in AlertDialog Example',
home: StepperExample(),
);
}
}
class StepperExample extends StatefulWidget {
@override
_StepperExampleState createState() => _StepperExampleState();
}
class _StepperExampleState extends State<StepperExample> {
int _currentStep = 0;
// Define the steps for the Stepper
List<Step> _steps = [
Step(
title: Text('Step 1'),
content: Text('This is the content of step 1.'),
),
Step(
title: Text('Step 2'),
content: Text('This is the content of step 2.'),
),
Step(
title: Text('Step 3'),
content: Text('This is the content of step 3.'),
),
];
// Callback when the "Continue" button in the Stepper is pressed
void _onStepContinue() {
setState(() {
if (_currentStep < _steps.length - 1) {
_currentStep++;
_showStepperDialog(); // Show the AlertDialog with the updated step
}
});
}
// Callback when the "Cancel" button in the Stepper is pressed
void _onStepCancel() {
setState(() {
if (_currentStep > 0) {
_currentStep--;
_showStepperDialog(); // Show the AlertDialog with the updated step
}
});
}
// Function to show the AlertDialog with the Stepper
void _showStepperDialog() {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Stepper in AlertDialog'),
content: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: SizedBox(
width: 300,
child: Stepper(
currentStep: _currentStep,
onStepContinue: _onStepContinue,
onStepCancel: _onStepCancel,
steps: _steps,
),
),
),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.of(context).pop(); // Close the AlertDialog
},
child: Text('Close'),
),
],
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Stepper in AlertDialog Example'),
),
body: Center(
child: ElevatedButton(
onPressed: _showStepperDialog,
child: Text('Show Stepper Dialog'),
),
),
);
}
}
Output:
Similar Reads
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Steady State Response
In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Use Case Diagram - Unified Modeling Language (UML)
A Use Case Diagram in Unified Modeling Language (UML) is a visual representation that illustrates the interactions between users (actors) and a system. It captures the functional requirements of a system, showing how different users engage with various use cases, or specific functionalities, within
9 min read
Half Wave Rectifier
A Half-wave rectifier is an electronic device that is used to convert Alternating current (AC) to Direct current (DC). A half-wave rectifier allows either a positive or negative half-cycle of AC to pass and blocks the other half-cycle. Half-wave rectifier selectively allows only one half-cycle of th
15 min read
Flutter Tutorial
This Flutter Tutorial is specifically designed for beginners and experienced professionals. It covers both the basics and advanced concepts of the Flutter framework.Flutter is Googleâs mobile SDK that builds native Android and iOS apps from a single codebase. It was developed in December 2017. When
7 min read
Top 8 Software Development Life Cycle (SDLC) Models used in Industry
Software development models are various processes or methods that are chosen for project development depending on the objectives and goals of the project. Many development life cycle models have been developed to achieve various essential objectives. Models specify the various steps of the process a
9 min read
What is Agile Methodology?
The Agile methodology is a proper way of managing the project with breaking them into smaller phases which is iteration. It basically focus on flexibility of the project which we can change and improve the team work regularly as per requirements.Table of ContentWhat is Agile?What is the Agile Method
14 min read
How to Download and Install the Google Play Store
The Google Play Store is the heartbeat of your Android experienceâhome to millions of apps, games, and updates that keep your device functional, fun, and secure. But what if your phone or tablet doesnât have it pre-installed?In this step-by-step guide, youâll learn how to safely download and install
6 min read
IEEE 802.11 Architecture
The IEEE 802.11 standard, commonly known as Wi-Fi, outlines the architecture and defines the MAC and physical layer specifications for wireless LANs (WLANs). Wi-Fi uses high-frequency radio waves instead of cables for connecting the devices in LAN. Given the mobility of WLAN nodes, they can move unr
9 min read
Transaction in DBMS
In a Database Management System (DBMS), a transaction is a sequence of operations performed as a single logical unit of work. These operations may involve reading, writing, updating, or deleting data in the database. A transaction is considered complete only if all its operations are successfully ex
10 min read