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
This commit is contained in:
Mehmet Fidanboylu 2017-08-29 09:39:54 -07:00 committed by GitHub
parent 6aae676441
commit a7d2f8359f
2 changed files with 16 additions and 8 deletions

View file

@ -605,14 +605,18 @@ class ScaffoldState extends State<Scaffold> with TickerProviderStateMixin {
}
/// 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 }) {
assert(reason != null);
if (_snackBars.isEmpty || _snackBarController.status == AnimationStatus.dismissed)
return;
final Completer<SnackBarClosedReason> completer = _snackBars.first._completer;
_snackBarController.reverse().then<Null>((Null _) {
assert(mounted);
if (!completer.isCompleted)
completer.complete(reason);
_snackBarController.reverse();
});
_snackBarTimer?.cancel();
_snackBarTimer = null;
}

View file

@ -374,7 +374,11 @@ void main() {
expect(actionPressed, isFalse);
await tester.tap(find.text('ACTION'));
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));
// Pop up the snack bar and then swipe downwards to dismiss it.
@ -382,21 +386,21 @@ void main() {
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.pump();
await tester.pumpAndSettle(const Duration(seconds: 1));
expect(closedReason, equals(SnackBarClosedReason.swipe));
// Pop up the snack bar and then remove it.
await tester.tap(find.text('X'));
await tester.pump(const Duration(milliseconds: 750));
scaffoldKey.currentState.removeCurrentSnackBar();
await tester.pump(const Duration(seconds: 1));
await tester.pumpAndSettle(const Duration(seconds: 1));
expect(closedReason, equals(SnackBarClosedReason.remove));
// Pop up the snack bar and then hide it.
await tester.tap(find.text('X'));
await tester.pump(const Duration(milliseconds: 750));
scaffoldKey.currentState.hideCurrentSnackBar();
await tester.pump(const Duration(seconds: 1));
await tester.pumpAndSettle(const Duration(seconds: 1));
expect(closedReason, equals(SnackBarClosedReason.hide));
// Pop up the snack bar and then let it time out.
@ -405,7 +409,7 @@ void main() {
await tester.pump(const Duration(milliseconds: 750));
await tester.pump(const Duration(milliseconds: 1500));
await tester.pump(); // begin animation
await tester.pump(const Duration(milliseconds: 750));
await tester.pumpAndSettle(const Duration(seconds: 1));
expect(closedReason, equals(SnackBarClosedReason.timeout));
});