Open In App

Flutter – Implement Stepper Widget Inside an AlertDialog

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:



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.




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.




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




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




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:


Article Tags :