Commit a7d2f835 authored by Mehmet Fidanboylu's avatar Mehmet Fidanboylu Committed by GitHub

Notify the completer after the close animation completes on the snackbar (#11688)

* Notify the completer after the close animation completes on the snackbar

* Review comments

* Fix tests and analyzer warnings

* Fix analyzer warnings
parent 6aae6764
...@@ -605,14 +605,18 @@ class ScaffoldState extends State<Scaffold> with TickerProviderStateMixin { ...@@ -605,14 +605,18 @@ class ScaffoldState extends State<Scaffold> with TickerProviderStateMixin {
} }
/// Removes the current [SnackBar] by running its normal exit animation. /// Removes the current [SnackBar] by running its normal exit animation.
///
/// The closed completer is called after the animation is complete.
void hideCurrentSnackBar({ SnackBarClosedReason reason: SnackBarClosedReason.hide }) { void hideCurrentSnackBar({ SnackBarClosedReason reason: SnackBarClosedReason.hide }) {
assert(reason != null); assert(reason != null);
if (_snackBars.isEmpty || _snackBarController.status == AnimationStatus.dismissed) if (_snackBars.isEmpty || _snackBarController.status == AnimationStatus.dismissed)
return; return;
final Completer<SnackBarClosedReason> completer = _snackBars.first._completer; final Completer<SnackBarClosedReason> completer = _snackBars.first._completer;
if (!completer.isCompleted) _snackBarController.reverse().then<Null>((Null _) {
completer.complete(reason); assert(mounted);
_snackBarController.reverse(); if (!completer.isCompleted)
completer.complete(reason);
});
_snackBarTimer?.cancel(); _snackBarTimer?.cancel();
_snackBarTimer = null; _snackBarTimer = null;
} }
......
...@@ -374,7 +374,11 @@ void main() { ...@@ -374,7 +374,11 @@ void main() {
expect(actionPressed, isFalse); expect(actionPressed, isFalse);
await tester.tap(find.text('ACTION')); await tester.tap(find.text('ACTION'));
expect(actionPressed, isTrue); expect(actionPressed, isTrue);
await tester.pump(const Duration(seconds: 1)); // Closed reason is only set when the animation is complete.
await tester.pump(const Duration(milliseconds:250));
expect(closedReason, isNull);
// Wait for animation to complete.
await tester.pumpAndSettle(const Duration(seconds: 1));
expect(closedReason, equals(SnackBarClosedReason.action)); expect(closedReason, equals(SnackBarClosedReason.action));
// Pop up the snack bar and then swipe downwards to dismiss it. // Pop up the snack bar and then swipe downwards to dismiss it.
...@@ -382,21 +386,21 @@ void main() { ...@@ -382,21 +386,21 @@ void main() {
await tester.pump(const Duration(milliseconds: 750)); await tester.pump(const Duration(milliseconds: 750));
await tester.pump(const Duration(milliseconds: 750)); await tester.pump(const Duration(milliseconds: 750));
await tester.drag(find.text('snack'), const Offset(0.0, 50.0)); await tester.drag(find.text('snack'), const Offset(0.0, 50.0));
await tester.pump(); await tester.pumpAndSettle(const Duration(seconds: 1));
expect(closedReason, equals(SnackBarClosedReason.swipe)); expect(closedReason, equals(SnackBarClosedReason.swipe));
// Pop up the snack bar and then remove it. // Pop up the snack bar and then remove it.
await tester.tap(find.text('X')); await tester.tap(find.text('X'));
await tester.pump(const Duration(milliseconds: 750)); await tester.pump(const Duration(milliseconds: 750));
scaffoldKey.currentState.removeCurrentSnackBar(); scaffoldKey.currentState.removeCurrentSnackBar();
await tester.pump(const Duration(seconds: 1)); await tester.pumpAndSettle(const Duration(seconds: 1));
expect(closedReason, equals(SnackBarClosedReason.remove)); expect(closedReason, equals(SnackBarClosedReason.remove));
// Pop up the snack bar and then hide it. // Pop up the snack bar and then hide it.
await tester.tap(find.text('X')); await tester.tap(find.text('X'));
await tester.pump(const Duration(milliseconds: 750)); await tester.pump(const Duration(milliseconds: 750));
scaffoldKey.currentState.hideCurrentSnackBar(); scaffoldKey.currentState.hideCurrentSnackBar();
await tester.pump(const Duration(seconds: 1)); await tester.pumpAndSettle(const Duration(seconds: 1));
expect(closedReason, equals(SnackBarClosedReason.hide)); expect(closedReason, equals(SnackBarClosedReason.hide));
// Pop up the snack bar and then let it time out. // Pop up the snack bar and then let it time out.
...@@ -405,7 +409,7 @@ void main() { ...@@ -405,7 +409,7 @@ void main() {
await tester.pump(const Duration(milliseconds: 750)); await tester.pump(const Duration(milliseconds: 750));
await tester.pump(const Duration(milliseconds: 1500)); await tester.pump(const Duration(milliseconds: 1500));
await tester.pump(); // begin animation await tester.pump(); // begin animation
await tester.pump(const Duration(milliseconds: 750)); await tester.pumpAndSettle(const Duration(seconds: 1));
expect(closedReason, equals(SnackBarClosedReason.timeout)); expect(closedReason, equals(SnackBarClosedReason.timeout));
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment