Skip to content

Commit

Permalink
Fix FutureResultDialog navigating multiple time by accident
Browse files Browse the repository at this point in the history
  • Loading branch information
Pante committed Sep 8, 2023
1 parent 70e32e8 commit e55a046
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions stevia/lib/src/widgets/async/dialog/future_result_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ Future<Result<S, F>> showFutureResultDialog<S extends Object, F extends Object>(


/// A [FutureResultDialog]. See [showFutureValueDialog] for more details.
class FutureResultDialog<S extends Object, F extends Object> extends StatelessWidget {
class FutureResultDialog<S extends Object, F extends Object> extends StatefulWidget {

/// The asynchronous computation.
final Future<Result<S, F>> future;
Expand Down Expand Up @@ -128,20 +128,37 @@ class FutureResultDialog<S extends Object, F extends Object> extends StatelessWi
this.child,
});

@override
State<FutureResultDialog<S, F>> createState() => _State();

}

class _State<S extends Object, F extends Object> extends State<FutureResultDialog<S, F>> {

bool popped = false;

@override
Widget build(BuildContext context) => FutureResultBuilder(
future: (_) => future,
builder: builder ?? _defaultBuilder,
failureBuilder: failureBuilder ?? _defaultBuilder,
future: (_) => widget.future,
builder: widget.builder ?? _defaultBuilder,
failureBuilder: widget.failureBuilder ?? _defaultBuilder,
emptyBuilder: (context, future, child) => WillPopScope(
onWillPop: () async => false,
child: emptyBuilder?.call(context, future!, child) ?? const SizedBox(),
child: widget.emptyBuilder?.call(context, future!, child) ?? const SizedBox(),
),
child: child,
child: widget.child,
);

Widget _defaultBuilder(BuildContext context, dynamic value, Widget? child) {
WidgetsBinding.instance.addPostFrameCallback((_) => Navigator.of(context).pop());
WidgetsBinding.instance.scheduleFrameCallback((_) {
// On iOS, additional frames may be rendered between the call to Navigation.pop()
// and the actual navigation. The callback needs to be latched to prevent multiple
// calls to Navigation.Pop.
if (!popped) {
popped = true;
Navigator.of(context).pop();
}
});
return const SizedBox();
}

Expand Down

0 comments on commit e55a046

Please sign in to comment.