The arrival of Navigator 2.0 in Flutter 1.22 was a long-anticipated addition to the Flutter ecosystem. With this update, we can now access the pages API, which gives us more freedom for navigating through different flows in an application.
We built Flow Builder to make navigating with Navigator 2.0 even simpler by taking out all of the manual steps you would have to do when using just the pages API. Flow Builder also maintains a declarative API, allowing you to decouple the routing logic from the UI. This gives you the freedom to implement complex routing for deep links and custom navigation flows.
Since Flow Builder represents navigation as a function of state, it can be used with whatever state management solution you have chosen. Most importantly, it is fully testable.
The package also handles things that you would normally have to manually account for with the pages API, such as pressing the hardware back button on Android and swiping from left on iOS. Additionally, it allows developers to define complex nested flows driven by independent state.
How to Use Flow Builder
There are three different use cases built into the package, including an example onboarding flow, profile creation, and location selection. We’ll walk through the use case for profile creation here:
First, define a flow state that will drive the navigation.
Then, create a FlowBuilder.
Define your onGeneratePages function, which will be responsible for determining the navigation stack based on the full state.
Now you can plug in an existing state management solution to maintain and manipulate the flow state OR you can use the built-in FlowController to manipulate the flow state.
To complete a flow, use the complete API on FlowController and optionally modify the flow state as needed.
With that, you’ve completed your first flow!
For more examples, check out the repo here.
We want to hear from you!
Flow Builder is currently under development, but we've tested it in a few projects internally at VGV to verify the abstraction. Before we release a stable version, we would love to hear from you about how to make this better and any additional examples you’d like to see added to the repo! Please open an issue here with feedback and suggestions.