How to Build a Custom Stepper Widget in Flutter

Learn to build a reusable Flutter custom stepper with step navigation, validation, form integration, and practical accessibility tips.

ASOasis
5 min read
How to Build a Custom Stepper Widget in Flutter

Image used for representation purposes only.

What Is a Custom Stepper Widget?

A stepper guides users through a sequence of related tasks, such as creating an account, entering delivery details, or completing a checkout. Flutter’s Material Stepper provides a ready-made version, with configurable steps and callbacks. Building a custom stepper is useful when you need a different visual style, custom validation rules, or behavior that the standard component doesn’t provide. (api.flutter.dev )

This article walks through a reusable, vertical custom stepper. The example includes numbered progress indicators, one visible step at a time, optional per-step validation, and Previous, Continue, and Finish actions.

Decide Where State Belongs

The current step is user-interface state: it changes as the user moves through the flow. This example keeps it inside the custom widget using StatefulWidget and setState. If the surrounding page or another part of your app must control the step, expose the current index and change handler as widget properties instead. Flutter’s guidance is to choose state ownership based on how a widget will be used; user data often belongs higher in the widget tree. (docs.flutter.dev )

Keep the data for each step separate from the stepper’s layout. Here, CustomStep contains a title, a content widget, and an optional validator. This makes the visual component reusable with different forms and workflows.

Build the Custom Stepper

The following implementation uses only Flutter’s Material library. It displays the active step’s content and lets users return to earlier steps. Validation runs when the user presses Continue or Finish; returning to a previous step does not require validation.

import 'package:flutter/material.dart';

class CustomStep {
  const CustomStep({
    required this.title,
    required this.content,
    this.validator,
  });

  final String title;
  final Widget content;
  final bool Function()? validator;
}

class CustomStepper extends StatefulWidget {
  const CustomStepper({
    super.key,
    required this.steps,
    required this.onComplete,
    this.initialStep = 0,
    this.onStepChanged,
  }) : assert(steps.length > 0);

  final List<CustomStep> steps;
  final VoidCallback onComplete;
  final int initialStep;
  final ValueChanged<int>? onStepChanged;

  @override
  State<CustomStepper> createState() => _CustomStepperState();
}

class _CustomStepperState extends State<CustomStepper> {
  late int _currentStep;

  @override
  void initState() {
    super.initState();
    _currentStep = widget.initialStep.clamp(0, widget.steps.length - 1);
  }

  void _moveTo(int index) {
    setState(() => _currentStep = index);
    widget.onStepChanged?.call(index);
  }

  void _continue() {
    final isValid = widget.steps[_currentStep].validator?.call() ?? true;
    if (!isValid) return;

    if (_currentStep == widget.steps.length - 1) {
      widget.onComplete();
    } else {
      _moveTo(_currentStep + 1);
    }
  }

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);

    return Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: [
        for (var index = 0; index < widget.steps.length; index++)
          Padding(
            padding: const EdgeInsets.only(bottom: 12),
            child: Row(
              children: [
                CircleAvatar(
                  radius: 16,
                  backgroundColor: index <= _currentStep
                      ? theme.colorScheme.primary
                      : theme.colorScheme.surfaceContainerHighest,
                  foregroundColor: index <= _currentStep
                      ? theme.colorScheme.onPrimary
                      : theme.colorScheme.onSurfaceVariant,
                  child: index < _currentStep
                      ? const Icon(Icons.check, size: 18)
                      : Text('${index + 1}'),
                ),
                const SizedBox(width: 12),
                Text(
                  widget.steps[index].title,
                  style: theme.textTheme.titleMedium?.copyWith(
                    fontWeight: index == _currentStep
                        ? FontWeight.bold
                        : FontWeight.normal,
                  ),
                ),
              ],
            ),
          ),
        const SizedBox(height: 16),
        widget.steps[_currentStep].content,
        const SizedBox(height: 24),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            TextButton(
              onPressed: _currentStep == 0
                  ? null
                  : () => _moveTo(_currentStep - 1),
              child: const Text('Previous'),
            ),
            FilledButton(
              onPressed: _continue,
              child: Text(
                _currentStep == widget.steps.length - 1
                    ? 'Finish'
                    : 'Continue',
              ),
            ),
          ],
        ),
      ],
    );
  }
}

The widget owns its current index, calls the active step’s validator before moving forward, and calls onComplete after the final step passes validation. For stronger input handling, each step can contain a Form, with a GlobalKey<FormState> used to validate its fields. Flutter’s FormState supports validation through a form key or Form.of from an appropriate descendant context. (api.flutter.dev )

Use It in a Form Flow

Create the step data in a parent widget and pass it to CustomStepper. The example below uses a form key to validate the first step. Keep the key in the parent State object so it remains stable across rebuilds.

final _profileFormKey = GlobalKey<FormState>();

late final steps = [
  CustomStep(
    title: 'Profile',
    content: Form(
      key: _profileFormKey,
      child: TextFormField(
        decoration: const InputDecoration(labelText: 'Full name'),
        validator: (value) =>
            value == null || value.trim().isEmpty ? 'Enter your name' : null,
      ),
    ),
    validator: () => _profileFormKey.currentState!.validate(),
  ),
  const CustomStep(
    title: 'Preferences',
    content: Text('Add preference fields here.'),
  ),
  const CustomStep(
    title: 'Review',
    content: Text('Review the information before finishing.'),
  ),
];

CustomStepper(
  steps: steps,
  onComplete: () {
    // Submit the completed workflow.
  },
)

In a real application, declare _profileFormKey and steps in a State class rather than directly in a build method. That avoids recreating the key and step configuration on every rebuild. You can give each form step its own key and validator, then provide a completion callback that saves or submits the collected data.

Design and Accessibility Considerations

  • Show progress clearly. Use a distinct active state, completed icon, and clear step titles. Don’t rely on color alone to communicate status.
  • Support small screens. The example uses a vertical list of titles, which can accommodate longer labels better than a tightly packed horizontal row. Test with large text settings and narrow layouts.
  • Make errors visible. Put validation messages next to the fields that need attention, and keep the user on the invalid step.
  • Handle dynamic steps deliberately. If the list changes after the widget is created, consider how to keep the current index in range and whether the flow should restart.
  • Keep navigation predictable. Decide whether users can revisit completed steps, whether skipping is allowed, and what should happen when they edit earlier answers.
  • Test the flow. Cover initial display, invalid and valid submissions, backward navigation, and the final completion callback.

When to Use Flutter’s Built-In Stepper

A custom implementation is not always necessary. Flutter’s Material Stepper is designed to display progress through ordered tasks, and its parent supplies the current step based on application logic and callbacks. Start with it when its appearance and interaction model fit your design; build a custom widget when you need substantially different visuals or flow behavior. (api.flutter.dev )

Conclusion

A custom Flutter stepper combines a small amount of state management with reusable step data and explicit navigation rules. Begin with the essential behavior—show the current step, validate before continuing, and signal completion—then refine styling and accessibility for your application. By keeping each step’s content and validation separate from the stepper’s presentation, you can reuse the same component across onboarding, checkout, and multi-part forms.

Related Posts