Wizard class

A wizard is a flow of steps that the user can navigate through.

Wizard provides routing for classic linear wizards in a way that it eliminates dependencies between wizard pages. Wizard pages can request the next or previous page without knowing or caring what is the next or the previous wizard page. Thus, adding, removing, or re-ordering pages does not cause changes in existing pages.

wizard_router

Usage

Routes

MaterialApp(
  home: Scaffold(
    body: Wizard(
      routes: {
        '/foo': WizardRoute(builder: (context) => FooPage()),
        '/bar': WizardRoute(builder: (context) => BarPage()),
        '/baz': WizardRoute(builder: (context) => BazPage()),
      },
    ),
  ),
)

The next or the previous page is requested by calling Wizard.of(context).next() or Wizard.of(context).back(), respectively.

BarPage(
  child: ButtonBar(
    children: [
      ElevatedButton(
        onPressed: Wizard.of(context).back
        child: const Text('Back'),
      ),
      ElevatedButton(
        onPressed: Wizard.of(context).next
        child: const Text('Next'),
      ),
    ],
  ),
)

Conditions

For unconditional linear wizards, defining the routes is enough. The router follows the order the routes are defined in. If there are conditions between the wizard pages, the order can be customized with the WizardRoute.onNext and WizardRoute.onBack callbacks.

Wizard(
  routes: {
    '/foo': WizardRoute(
      builder: (context) => FooPage(),
      // conditionally skip the _Bar_ page when stepping forward from the _Foo_ page
      onNext: (settings) => skipBar ? '/baz' : null,
    ),
    '/bar': WizardRoute(builder: (context) => BarPage()),
    '/baz': WizardRoute(builder: (context) => BazPage()),
    '/qux': WizardRoute(
      builder: (context) => QuxPage(),
      // always skip the Baz page when stepping back from the Qux page
      onBack: (settings) => '/bar',
    ),
  },
)

Arguments

It is recommended to avoid such dependencies between wizard pages that make assumptions of the page order. However, sometimes it may be desirable to pass arguments to the next page. This is possible by passing them to Wizard.of(context).next(arguments). On the next page, the arguments can be queried from Wizard.of(context).arguments.

FooPage(
  onFoo: () => Wizard.of(context).next(arguments: something),
)

BarPageState extends State<BarPage>(
  @override
  void initState() {
    super.initState();

    final something = Wizard.of(context).arguments as Something;
    // ...
  }
)
Inheritance

Constructors

Wizard({Key? key, String? initialRoute, Map<String, WizardRoute>? routes, List<NavigatorObserver> observers = const [], Object? userData, WizardController? controller})
Creates an instance of a wizard. The routes argument is required.
const

Properties

controller WizardController?
final
hashCode int
The hash code for this object.
no setterinherited
initialRoute String?
The name of the first route to show.
final
key Key?
Controls how one widget replaces another widget in the tree.
finalinherited
observers List<NavigatorObserver>
final
routes Map<String, WizardRoute>?
The wizards's routing table.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
userData Object?
Additional custom data associated with this page.
final

Methods

createElement() StatefulElement
Creates a StatefulElement to manage this widget's location in the tree.
inherited
createState() State<Wizard>
Creates the mutable state for this widget at a given location in the tree.
override
debugDescribeChildren() List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children.
inherited
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toDiagnosticsNode({String? name, DiagnosticsTreeStyle? style}) DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep.
inherited
toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) String
A string representation of this object.
inherited
toStringDeep({String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a string representation of this node and its descendants.
inherited
toStringShallow({String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a one-line detailed description of the object.
inherited
toStringShort() String
A short, textual description of this widget.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

maybeOf(BuildContext context, {bool root = false}) WizardScopeState?
The wizard scope from the closest instance of this class that encloses the given context.
of(BuildContext context, {bool root = false}) WizardScopeState
The wizard scope from the closest instance of this class that encloses the given context.