Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace state callbacks with FlowCallback #65

Merged
merged 1 commit into from
Oct 13, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions lib/flow_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import 'package:flutter/widgets.dart';
/// and the current [List<Page>].
typedef OnGeneratePages<T> = List<Page> Function(T, List<Page>);

/// Signature for function which given an input flow state [T] will
/// output a new flow state [T].
///
/// It is used to compute the next flow state with [FlowController.update] and
/// [FlowController.complete].
typedef FlowCallback<T> = T Function(T state);

/// {@template flow_builder}
/// [FlowBuilder] abstracts navigation and exposes a declarative routing API
/// based on a [state].
Expand Down Expand Up @@ -258,7 +265,7 @@ class FlowController<T> implements Listenable {
///
/// When [update] is called, the `builder` method of the corresponding
/// [FlowBuilder] will be called with the new flow state.
void update(T Function(T) callback) {
void update(FlowCallback<T> callback) {
_notifier.value = callback(_notifier.value);
}

Expand All @@ -267,7 +274,7 @@ class FlowController<T> implements Listenable {
/// and is responsible for returning the new flow state.
///
/// When [complete] is called, the flow is popped with the new flow state.
void complete([T Function(T)? callback]) {
void complete([FlowCallback<T>? callback]) {
_completed = true;
final state = callback?.call(_notifier.value) ?? _notifier.value;
if (state == _notifier.value) {
Expand Down Expand Up @@ -322,12 +329,12 @@ class FakeFlowController<T> extends FlowController<T> {
bool get completed => _completed;

@override
void update(T Function(T) callback) {
void update(FlowCallback<T> callback) {
_state = callback(_state);
}

@override
void complete([T Function(T)? callback]) {
void complete([FlowCallback<T>? callback]) {
_completed = true;
if (callback != null) {
_state = callback(_state);
Expand Down