Skip to content

Commit a38294e

Browse files
committed
feat: use async step results
1 parent 436151a commit a38294e

File tree

5 files changed

+155
-108
lines changed

5 files changed

+155
-108
lines changed

example/pubspec.lock

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -85,26 +85,26 @@ packages:
8585
dependency: transitive
8686
description:
8787
name: characters
88-
sha256: "04a925763edad70e8443c99234dc3328f442e811f1d8fd1a72f1c8ad0f69a605"
88+
sha256: f71061c654a3380576a52b451dd5532377954cf9dbd272a78fc8479606670803
8989
url: "https://pub.dev"
9090
source: hosted
91-
version: "1.3.0"
91+
version: "1.4.0"
9292
clock:
9393
dependency: transitive
9494
description:
9595
name: clock
96-
sha256: cb6d7f03e1de671e34607e909a7213e31d7752be4fb66a86d29fe1eb14bfb5cf
96+
sha256: fddb70d9b5277016c77a80201021d40a2247104d9f4aa7bab7157b7e3f05b84b
9797
url: "https://pub.dev"
9898
source: hosted
99-
version: "1.1.1"
99+
version: "1.1.2"
100100
collection:
101101
dependency: transitive
102102
description:
103103
name: collection
104-
sha256: a1ace0a119f20aabc852d165077c036cd864315bd99b7eaa10a60100341941bf
104+
sha256: "2f5709ae4d3d59dd8f7cd309b4e023046b57d8a6c82130785d2b0e5868084e76"
105105
url: "https://pub.dev"
106106
source: hosted
107-
version: "1.19.0"
107+
version: "1.19.1"
108108
cross_file:
109109
dependency: transitive
110110
description:
@@ -149,10 +149,10 @@ packages:
149149
dependency: transitive
150150
description:
151151
name: fake_async
152-
sha256: "511392330127add0b769b75a987850d136345d9227c6b94c96a04cf4a391bf78"
152+
sha256: "5368f224a74523e8d2e7399ea1638b37aecfca824a3cc4dfdf77bf1fa905ac44"
153153
url: "https://pub.dev"
154154
source: hosted
155-
version: "1.3.1"
155+
version: "1.3.3"
156156
ffi:
157157
dependency: transitive
158158
description:
@@ -379,26 +379,26 @@ packages:
379379
dependency: transitive
380380
description:
381381
name: leak_tracker
382-
sha256: "7bb2830ebd849694d1ec25bf1f44582d6ac531a57a365a803a6034ff751d2d06"
382+
sha256: "33e2e26bdd85a0112ec15400c8cbffea70d0f9c3407491f672a2fad47915e2de"
383383
url: "https://pub.dev"
384384
source: hosted
385-
version: "10.0.7"
385+
version: "11.0.2"
386386
leak_tracker_flutter_testing:
387387
dependency: transitive
388388
description:
389389
name: leak_tracker_flutter_testing
390-
sha256: "9491a714cca3667b60b5c420da8217e6de0d1ba7a5ec322fab01758f6998f379"
390+
sha256: "1dbc140bb5a23c75ea9c4811222756104fbcd1a27173f0c34ca01e16bea473c1"
391391
url: "https://pub.dev"
392392
source: hosted
393-
version: "3.0.8"
393+
version: "3.0.10"
394394
leak_tracker_testing:
395395
dependency: transitive
396396
description:
397397
name: leak_tracker_testing
398-
sha256: "6ba465d5d76e67ddf503e1161d1f4a6bc42306f9d66ca1e8f079a47290fb06d3"
398+
sha256: "8d5a2d49f4a66b49744b23b018848400d23e54caf9463f4eb20df3eb8acb2eb1"
399399
url: "https://pub.dev"
400400
source: hosted
401-
version: "3.0.1"
401+
version: "3.0.2"
402402
lottie:
403403
dependency: transitive
404404
description:
@@ -419,10 +419,10 @@ packages:
419419
dependency: transitive
420420
description:
421421
name: matcher
422-
sha256: d2323aa2060500f906aa31a895b4030b6da3ebdcc5619d14ce1aada65cd161cb
422+
sha256: dc58c723c3c24bf8d3e2d3ad3f2f9d7bd9cf43ec6feaa64181775e60190153f2
423423
url: "https://pub.dev"
424424
source: hosted
425-
version: "0.12.16+1"
425+
version: "0.12.17"
426426
material_color_utilities:
427427
dependency: transitive
428428
description:
@@ -435,10 +435,10 @@ packages:
435435
dependency: transitive
436436
description:
437437
name: meta
438-
sha256: bdb68674043280c3428e9ec998512fb681678676b3c54e773629ffe74419f8c7
438+
sha256: e3641ec5d63ebf0d9b41bd43201a66e3fc79a65db5f61fc181f04cd27aab950c
439439
url: "https://pub.dev"
440440
source: hosted
441-
version: "1.15.0"
441+
version: "1.16.0"
442442
mime:
443443
dependency: transitive
444444
description:
@@ -459,10 +459,10 @@ packages:
459459
dependency: transitive
460460
description:
461461
name: path
462-
sha256: "087ce49c3f0dc39180befefc60fdb4acd8f8620e5682fe2476afd0b3688bb4af"
462+
sha256: "75cca69d1490965be98c73ceaea117e8a04dd21217b37b292c9ddbec0d955bc5"
463463
url: "https://pub.dev"
464464
source: hosted
465-
version: "1.9.0"
465+
version: "1.9.1"
466466
platform:
467467
dependency: transitive
468468
description:
@@ -528,18 +528,18 @@ packages:
528528
dependency: transitive
529529
description:
530530
name: stack_trace
531-
sha256: "9f47fd3630d76be3ab26f0ee06d213679aa425996925ff3feffdec504931c377"
531+
sha256: "8b27215b45d22309b5cddda1aa2b19bdfec9df0e765f2de506401c071d38d1b1"
532532
url: "https://pub.dev"
533533
source: hosted
534-
version: "1.12.0"
534+
version: "1.12.1"
535535
stream_channel:
536536
dependency: transitive
537537
description:
538538
name: stream_channel
539-
sha256: ba2aa5d8cc609d96bbb2899c28934f9e1af5cddbd60a827822ea467161eb54e7
539+
sha256: "969e04c80b8bcdf826f8f16579c7b14d780458bd97f56d107d3950fdbeef059d"
540540
url: "https://pub.dev"
541541
source: hosted
542-
version: "2.1.2"
542+
version: "2.1.4"
543543
stream_transform:
544544
dependency: transitive
545545
description:
@@ -591,10 +591,10 @@ packages:
591591
dependency: transitive
592592
description:
593593
name: test_api
594-
sha256: "664d3a9a64782fcdeb83ce9c6b39e78fd2971d4e37827b9b06c3aa1edc5e760c"
594+
sha256: "522f00f556e73044315fa4585ec3270f1808a4b186c936e612cab0b565ff1e00"
595595
url: "https://pub.dev"
596596
source: hosted
597-
version: "0.7.3"
597+
version: "0.7.6"
598598
typed_data:
599599
dependency: transitive
600600
description:
@@ -679,10 +679,10 @@ packages:
679679
dependency: transitive
680680
description:
681681
name: vector_math
682-
sha256: "80b3257d1492ce4d091729e3a67a60407d227c27241d6927be0130c98e741803"
682+
sha256: d530bd74fea330e6e364cda7a85019c434070188383e1cd8d9777ee586914c5b
683683
url: "https://pub.dev"
684684
source: hosted
685-
version: "2.1.4"
685+
version: "2.2.0"
686686
video_player:
687687
dependency: transitive
688688
description:
@@ -748,5 +748,5 @@ packages:
748748
source: hosted
749749
version: "3.0.4"
750750
sdks:
751-
dart: ">=3.6.0 <4.0.0"
751+
dart: ">=3.8.0-0 <4.0.0"
752752
flutter: ">=3.27.0"

lib/src/controller/survey_controller.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:async';
2+
13
import 'package:flutter/widgets.dart';
24
import 'package:flutter_bloc/flutter_bloc.dart';
35
import 'package:survey_kit/src/presenter/survey_event.dart';
@@ -15,7 +17,7 @@ class SurveyController {
1517
///
1618
/// Returns:
1719
/// - `true` if the default behavior should still be executed after this function, `false` otherwise.
18-
final bool Function(
20+
final FutureOr<bool> Function(
1921
BuildContext context,
2022
QuestionResult Function() resultFunction,
2123
)? onNextStep;
@@ -56,12 +58,12 @@ class SurveyController {
5658
this.onCloseSurvey,
5759
});
5860

59-
void nextStep(
61+
Future<void> nextStep(
6062
BuildContext context,
6163
QuestionResult Function() resultFunction,
62-
) {
64+
) async {
6365
if (onNextStep != null) {
64-
if (!onNextStep!(context, resultFunction)) return;
66+
if (!await onNextStep!(context, resultFunction)) return;
6567
}
6668
BlocProvider.of<SurveyPresenter>(context).add(
6769
NextStep(

lib/src/presenter/survey_presenter.dart

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:async';
2+
13
import 'package:collection/collection.dart' show IterableExtension;
24
import 'package:flutter_bloc/flutter_bloc.dart';
35
import 'package:survey_kit/src/configuration/app_bar_configuration.dart';
@@ -13,7 +15,7 @@ import 'package:survey_kit/src/steps/identifier/step_identifier.dart';
1315
//TO DO: Extract gathering of the results into another class
1416
class SurveyPresenter extends Bloc<SurveyEvent, SurveyState> {
1517
final TaskNavigator taskNavigator;
16-
final Function(SurveyResult) onResult;
18+
final FutureOr<void> Function(SurveyResult) onResult;
1719

1820
Set<QuestionResult> results = {};
1921
late final DateTime startDate;
@@ -22,40 +24,44 @@ class SurveyPresenter extends Bloc<SurveyEvent, SurveyState> {
2224
required this.taskNavigator,
2325
required this.onResult,
2426
}) : super(LoadingSurveyState()) {
25-
26-
on<StartSurvey>((event, emit){
27-
emit(
28-
_handleInitialStep()
29-
);
27+
on<StartSurvey>((event, emit) {
28+
emit(_handleInitialStep());
3029
});
3130

32-
on<NextStep>((event, emit){
33-
if (state is PresentingSurveyState){
34-
emit(_handleNextStep(event, state as PresentingSurveyState));
31+
on<NextStep>((event, emit) async {
32+
if (state is PresentingSurveyState) {
33+
final newState = _handleNextStep(event, state as PresentingSurveyState);
34+
emit(newState);
35+
36+
// Call onResult after emitting the final state
37+
if (newState is SurveyResultState) {
38+
await onResult(newState.result);
39+
}
3540
}
3641
});
3742

38-
on<StepBack>((event, emit){
39-
if (state is PresentingSurveyState){
40-
emit(
41-
_handleStepBack(event, state as PresentingSurveyState)
42-
);
43+
on<StepBack>((event, emit) {
44+
if (state is PresentingSurveyState) {
45+
emit(_handleStepBack(event, state as PresentingSurveyState));
4346
}
4447
});
4548

46-
on<CloseSurvey>((event, emit){
47-
if (state is PresentingSurveyState){
48-
emit(
49-
_handleClose(event, state as PresentingSurveyState)
50-
);
49+
on<CloseSurvey>((event, emit) async {
50+
if (state is PresentingSurveyState) {
51+
final newState = _handleClose(event, state as PresentingSurveyState);
52+
emit(newState);
53+
54+
// Call onResult after emitting the final state
55+
if (newState is SurveyResultState) {
56+
await onResult(newState.result);
57+
}
5158
}
5259
});
5360

5461
this.startDate = DateTime.now();
5562
add(StartSurvey());
5663
}
5764

58-
5965
SurveyState _handleInitialStep() {
6066
Step? step = taskNavigator.firstStep();
6167
if (step != null) {

lib/src/survey_kit.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:async';
2+
13
import 'package:collection/collection.dart' show IterableExtension;
24
import 'package:flutter/material.dart';
35
import 'package:flutter_bloc/flutter_bloc.dart';
@@ -24,7 +26,7 @@ class SurveyKit extends StatefulWidget {
2426
final ThemeData? themeData;
2527

2628
/// Function which is called after the results are collected
27-
final Function(SurveyResult) onResult;
29+
final FutureOr<void> Function(SurveyResult) onResult;
2830

2931
/// [SurveyController] to override the navigation methods
3032
/// onNextStep, onBackStep, onCloseSurvey
@@ -116,7 +118,7 @@ class _SurveyKitState extends State<SurveyKit> {
116118
class SurveyPage extends StatefulWidget {
117119
final int length;
118120
final Widget Function(AppBarConfiguration appBarConfiguration)? appBar;
119-
final Function(SurveyResult) onResult;
121+
final FutureOr<void> Function(SurveyResult) onResult;
120122

121123
const SurveyPage({
122124
required this.length,
@@ -149,9 +151,6 @@ class _SurveyPageState extends State<SurveyPage>
149151
return BlocConsumer<SurveyPresenter, SurveyState>(
150152
listenWhen: (previous, current) => previous != current,
151153
listener: (context, state) async {
152-
if (state is SurveyResultState) {
153-
widget.onResult.call(state.result);
154-
}
155154
if (state is PresentingSurveyState) {
156155
tabController.animateTo(state.currentStepIndex);
157156
}

0 commit comments

Comments
 (0)