Multi-Step Form
Loading preview
Starting the Flutter app
3-step form with progress and per-step validation
import 'package:flutter/material.dart';
class MultiStepForm extends StatefulWidget {
const MultiStepForm({super.key});
@override
State<MultiStepForm> createState() => _MultiStepFormState();
}
class _MultiStepFormState extends State<MultiStepForm> {
var _step = 0;
// One Form per input step. IndexedStack keeps every step mounted, so a single
// Form would also validate the hidden steps.
final _formKeys = [GlobalKey<FormState>(), GlobalKey<FormState>()];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Multi-Step Form')),
body: Column(
children: [
LinearProgressIndicator(
value: (_step + 1) / 3,
),
Expanded(
child: IndexedStack(
index: _step,
children: [
Form(key: _formKeys[0], child: _Step1()),
Form(key: _formKeys[1], child: _Step2()),
_Step3(),
],
),
),
Padding(
padding: const EdgeInsets.all(16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
if (_step > 0)
TextButton(
onPressed: () => setState(() => _step--),
child: const Text('Back'),
)
else
const SizedBox(),
FilledButton(
onPressed: () {
if (_step < 2 &&
(_formKeys[_step].currentState?.validate() ?? false)) {
setState(() => _step++);
}
},
child: Text(_step < 2 ? 'Next' : 'Submit'),
),
],
),
),
],
),
);
}
}
class _Step1 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
padding: const EdgeInsets.all(24),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Step 1: Personal Info',
style: Theme.of(context).textTheme.titleLarge),
const SizedBox(height: 16),
TextFormField(
decoration: const InputDecoration(
labelText: 'Name',
border: OutlineInputBorder(),
),
validator: (v) => v?.isEmpty ?? true ? 'Required' : null,
),
const SizedBox(height: 16),
TextFormField(
decoration: const InputDecoration(
labelText: 'Email',
border: OutlineInputBorder(),
),
keyboardType: TextInputType.emailAddress,
validator: (v) => v?.isEmpty ?? true ? 'Required' : null,
),
],
),
);
}
}
class _Step2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
padding: const EdgeInsets.all(24),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Step 2: Address',
style: Theme.of(context).textTheme.titleLarge),
const SizedBox(height: 16),
TextFormField(
decoration: const InputDecoration(
labelText: 'Street',
border: OutlineInputBorder(),
),
validator: (v) => v?.isEmpty ?? true ? 'Required' : null,
),
const SizedBox(height: 16),
TextFormField(
decoration: const InputDecoration(
labelText: 'City',
border: OutlineInputBorder(),
),
validator: (v) => v?.isEmpty ?? true ? 'Required' : null,
),
],
),
);
}
}
class _Step3 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
padding: const EdgeInsets.all(24),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Step 3: Review',
style: Theme.of(context).textTheme.titleLarge),
const SizedBox(height: 16),
const Text('Review your information and submit.'),
],
),
);
}
}
Add it to your project
Copies the Dart source into your app, so you own it. Flutter SDK only, no extra packages.
First time? Run dart pub global activate fluttertemplates_dev, or read the CLI guide.
How it works
_step selects a page in an IndexedStack, and a LinearProgressIndicator shows (_step + 1) / 3. Each input step has its own Form, so Next calls validate() only on _formKeys[_step] even though IndexedStack keeps hidden steps mounted. That same mounting preserves typed values on Back. No field has a controller, _Step3 shows no summary, and Submit does nothing.
Adapting it
- Hold field values in
_MultiStepFormStateand pass them into_Step3for review. - Add your submit call to the
FilledButtonfor when_stepreaches 2. - The Email field accepts any non-empty text, so add a format check to its
validator.