Open In App

Flutter - Implement Stepper Widget Inside an AlertDialog

Last Updated : 14 Jun, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

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 the user's 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 demo video is given below to get an idea of what we are going to do in this article.

Demo Video

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.
  • Flutter Installed
  • Flutter plugin for VS Code / Android Studio.

Step-by-Step Implementations

Step 1: Create a new Flutter Application

Create a new Flutter application using the command Prompt. To create a new app, write the following command and run it.

flutter create app_name

To know more about it refer this article: 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

Call the runApp() method in the main() method to start the app.

Dart
void main() {
    runApp(MyApp());
}


Step 4: Create MyApp Class

In this class, we are going to implement the MaterialApp, Here, we are also setting 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 step. 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'),
),
],
);
},
);
}


Code:

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'),
        ),
      ),
    );
  }
}


Complete Source Code

main.dart:

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