Skip to content

Commit a0adef9

Browse files
committed
feat!: include pages in onGeneratePages
1 parent 71e793c commit a0adef9

7 files changed

Lines changed: 38 additions & 23 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 0.0.1-dev.7
2+
3+
- **BREAKING**: update `onGeneratePages` to include previous `pages`.
4+
15
# 0.0.1-dev.6
26

37
- feat: add `onComplete`

example/lib/location_flow/location_flow.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export 'models/models.dart';
99
export 'repository/location_repository.dart';
1010
export 'widgets/widgets.dart';
1111

12-
List<Page> onGenerateLocationPages(Location state) {
12+
List<Page> onGenerateLocationPages(Location state, List<Page> pages) {
1313
return [
1414
CountrySelection.page(),
1515
if (state.country != null) StateSelection.page(country: state.country),

example/lib/onboarding_flow.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'package:flutter/material.dart';
22
import 'package:flow_builder/flow_builder.dart';
33

4-
List<Page> onGenerateOnboardingPages(OnboardingSteps state) {
4+
List<Page> onGenerateOnboardingPages(OnboardingSteps state, List<Page> pages) {
55
switch (state) {
66
case OnboardingSteps.step1:
77
return [Step1.page()];

example/lib/profile_flow.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import 'package:equatable/equatable.dart';
22
import 'package:flutter/material.dart';
33
import 'package:flow_builder/flow_builder.dart';
44

5-
List<Page> onGenerateProfilePages(Profile profile) {
5+
List<Page> onGenerateProfilePages(Profile profile, List<Page> pages) {
66
return [
77
MaterialPage<void>(child: ProfileNameForm()),
88
if (profile.name != null) MaterialPage<void>(child: ProfileAgeForm()),

lib/flow_builder.dart

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ typedef Update<T> = void Function(T Function(T));
1010
/// Signature for [FlowController] `complete` function.
1111
typedef Complete<T> = void Function(T Function(T));
1212

13-
/// Signature for function which generates a [List<Page>] given an input of [T].
14-
typedef OnGeneratePages<T> = List<Page> Function(T);
13+
/// Signature for function which generates a [List<Page>] given an input of [T]
14+
/// and the current [List<Page>].
15+
typedef OnGeneratePages<T> = List<Page> Function(T, List<Page>);
1516

1617
/// {@template flow_builder}
1718
/// [FlowBuilder] abstracts navigation and exposes a declarative routing API
@@ -26,7 +27,7 @@ typedef OnGeneratePages<T> = List<Page> Function(T);
2627
/// ```dart
2728
/// FlowBuilder<MyFlowState>(
2829
/// state: MyFlowState.initial(),
29-
/// onGeneratePages: (state) {...},
30+
/// onGeneratePages: (state, pages) {...},
3031
/// onComplete: (state) {
3132
/// // do something when flow is completed...
3233
/// }
@@ -68,7 +69,7 @@ class _FlowBuilderState<T> extends State<FlowBuilder<T>> {
6869
void initState() {
6970
super.initState();
7071
_state = widget.state;
71-
_pages = widget.onGeneratePages(_state);
72+
_pages = widget.onGeneratePages(_state, List.of(_pages));
7273
_history.add(_state);
7374
}
7475

@@ -77,7 +78,7 @@ class _FlowBuilderState<T> extends State<FlowBuilder<T>> {
7778
super.didUpdateWidget(oldWidget);
7879
if (oldWidget.state != widget.state) {
7980
_state = widget.state;
80-
_pages = widget.onGeneratePages(_state);
81+
_pages = widget.onGeneratePages(_state, List.of(_pages));
8182
_history
8283
..clear()
8384
..add(_state);
@@ -97,7 +98,7 @@ class _FlowBuilderState<T> extends State<FlowBuilder<T>> {
9798
final state = update(_state);
9899
setState(() {
99100
_state = state;
100-
_pages = widget.onGeneratePages(_state);
101+
_pages = widget.onGeneratePages(_state, List.of(_pages));
101102
_history.add(state);
102103
});
103104
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: Flutter Flows made easy! A Flutter package which simplifies flows w
33
repository: https://github.com/felangel/flow_builder
44
homepage: https://github.com/felangel/flow_builder
55

6-
version: 0.0.1-dev.6
6+
version: 0.0.1-dev.7
77

88
environment:
99
sdk: ">=2.7.0 <3.0.0"

test/flow_builder_test.dart

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,23 @@ void main() {
1414

1515
test('does not throw when state is null', () async {
1616
expect(
17-
() => FlowBuilder(onGeneratePages: (dynamic __) => [], state: null),
17+
() => FlowBuilder(
18+
onGeneratePages: (dynamic _, List<Page> __) => [],
19+
state: null,
20+
),
1821
isNot(throwsAssertionError),
1922
);
2023
});
2124

2225
testWidgets('renders correct navigation stack w/one page', (tester) async {
2326
const targetKey = Key('__target__');
27+
var lastPages = <Page>[];
2428
await tester.pumpWidget(
2529
MaterialApp(
2630
home: FlowBuilder<int>(
2731
state: 0,
28-
onGeneratePages: (state) {
32+
onGeneratePages: (state, pages) {
33+
lastPages = pages;
2934
return const <Page>[
3035
MaterialPage<void>(child: SizedBox(key: targetKey)),
3136
];
@@ -34,6 +39,7 @@ void main() {
3439
),
3540
);
3641
expect(find.byKey(targetKey), findsOneWidget);
42+
expect(lastPages, isEmpty);
3743
});
3844

3945
testWidgets('renders correct navigation stack w/multi-page',
@@ -44,7 +50,7 @@ void main() {
4450
MaterialApp(
4551
home: FlowBuilder<int>(
4652
state: 0,
47-
onGeneratePages: (state) {
53+
onGeneratePages: (state, pages) {
4854
return const <Page>[
4955
MaterialPage<void>(child: SizedBox(key: box1Key)),
5056
MaterialPage<void>(child: SizedBox(key: box2Key)),
@@ -65,7 +71,7 @@ void main() {
6571
MaterialApp(
6672
home: FlowBuilder<int>(
6773
state: 0,
68-
onGeneratePages: (state) {
74+
onGeneratePages: (state, pages) {
6975
return <Page>[
7076
const MaterialPage<void>(child: SizedBox(key: box1Key)),
7177
if (state >= 1)
@@ -83,12 +89,14 @@ void main() {
8389
const buttonKey = Key('__button__');
8490
const boxKey = Key('__box__');
8591
var numBuilds = 0;
92+
var lastPages = <Page>[];
8693
await tester.pumpWidget(
8794
MaterialApp(
8895
home: FlowBuilder<int>(
8996
state: 0,
90-
onGeneratePages: (state) {
97+
onGeneratePages: (state, pages) {
9198
numBuilds++;
99+
lastPages = pages;
92100
return <Page>[
93101
MaterialPage<void>(
94102
child: Builder(
@@ -109,13 +117,15 @@ void main() {
109117
expect(numBuilds, 1);
110118
expect(find.byKey(buttonKey), findsOneWidget);
111119
expect(find.byKey(boxKey), findsNothing);
120+
expect(lastPages, isEmpty);
112121

113122
await tester.tap(find.byKey(buttonKey));
114123
await tester.pumpAndSettle();
115124

116125
expect(numBuilds, 2);
117126
expect(find.byKey(buttonKey), findsNothing);
118127
expect(find.byKey(boxKey), findsOneWidget);
128+
expect(lastPages.length, equals(1));
119129
});
120130

121131
testWidgets('complete terminates the flow', (tester) async {
@@ -135,7 +145,7 @@ void main() {
135145
MaterialPageRoute<int>(
136146
builder: (_) => FlowBuilder<int>(
137147
state: 0,
138-
onGeneratePages: (state) {
148+
onGeneratePages: (state, pages) {
139149
numBuilds++;
140150
return <Page>[
141151
MaterialPage<void>(
@@ -196,7 +206,7 @@ void main() {
196206
MaterialPageRoute<int>(
197207
builder: (_) => FlowBuilder<int>(
198208
state: 0,
199-
onGeneratePages: (state) {
209+
onGeneratePages: (state, pages) {
200210
numBuilds++;
201211
return <Page>[
202212
MaterialPage<void>(
@@ -246,7 +256,7 @@ void main() {
246256
MaterialApp(
247257
home: FlowBuilder<int>(
248258
state: 0,
249-
onGeneratePages: (state) {
259+
onGeneratePages: (state, pages) {
250260
numBuilds++;
251261
return <Page>[
252262
MaterialPage<void>(
@@ -301,7 +311,7 @@ void main() {
301311
MaterialApp(
302312
home: FlowBuilder<int>(
303313
state: 0,
304-
onGeneratePages: (state) {
314+
onGeneratePages: (state, pages) {
305315
numBuilds++;
306316
return <Page>[
307317
MaterialPage<void>(
@@ -362,7 +372,7 @@ void main() {
362372
MaterialApp(
363373
home: FlowBuilder<int>(
364374
state: 0,
365-
onGeneratePages: (state) {
375+
onGeneratePages: (state, pages) {
366376
numBuilds++;
367377
return <Page>[
368378
MaterialPage<void>(
@@ -423,7 +433,7 @@ void main() {
423433
MaterialApp(
424434
home: FlowBuilder<int>(
425435
state: 0,
426-
onGeneratePages: (state) {
436+
onGeneratePages: (state, pages) {
427437
return <Page>[
428438
MaterialPage<void>(
429439
child: Scaffold(
@@ -470,7 +480,7 @@ void main() {
470480
MaterialApp(
471481
home: FlowBuilder<int>(
472482
state: 0,
473-
onGeneratePages: (state) {
483+
onGeneratePages: (state, pages) {
474484
return <Page>[
475485
MaterialPage<void>(
476486
child: Scaffold(
@@ -503,7 +513,7 @@ void main() {
503513
builder: (context, setState) {
504514
return FlowBuilder<int>(
505515
state: flowState,
506-
onGeneratePages: (state) {
516+
onGeneratePages: (state, pages) {
507517
numBuilds++;
508518
return <Page>[
509519
MaterialPage<void>(

0 commit comments

Comments
 (0)