Skip to content

Commit

Permalink
Remove periodic timer from Scaffold snackbar logic (flutter#20317)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonahwilliams authored Aug 8, 2018
1 parent db58c66 commit 5177a37
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions packages/flutter/lib/src/material/scaffold.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,7 @@ class ScaffoldState extends State<Scaffold> with TickerProviderStateMixin {
final Queue<ScaffoldFeatureController<SnackBar, SnackBarClosedReason>> _snackBars = new Queue<ScaffoldFeatureController<SnackBar, SnackBarClosedReason>>();
AnimationController _snackBarController;
Timer _snackBarTimer;
bool _accessibleNavigation;

/// Shows a [SnackBar] at the bottom of the scaffold.
///
Expand Down Expand Up @@ -1396,6 +1397,23 @@ class ScaffoldState extends State<Scaffold> with TickerProviderStateMixin {
super.didUpdateWidget(oldWidget);
}

@override
void didChangeDependencies() {
final MediaQueryData mediaQuery = MediaQuery.of(context);
// If we transition from accessible navigation to non-accessible navigation
// and there is a SnackBar that would have timed out that has already
// completed its timer, dismiss that SnackBar. If the timer hasn't finished
// yet, let it timeout as normal.
if (_accessibleNavigation == true
&& !mediaQuery.accessibleNavigation
&& _snackBarTimer != null
&& !_snackBarTimer.isActive) {
hideCurrentSnackBar(reason: SnackBarClosedReason.timeout);
}
_accessibleNavigation = mediaQuery.accessibleNavigation;
super.didChangeDependencies();
}

@override
void dispose() {
_snackBarController?.dispose();
Expand Down Expand Up @@ -1482,20 +1500,20 @@ class ScaffoldState extends State<Scaffold> with TickerProviderStateMixin {
final MediaQueryData mediaQuery = MediaQuery.of(context);
final ThemeData themeData = Theme.of(context);
final TextDirection textDirection = Directionality.of(context);
_accessibleNavigation = mediaQuery.accessibleNavigation;

if (_snackBars.isNotEmpty) {
final ModalRoute<dynamic> route = ModalRoute.of(context);
if (route == null || route.isCurrent) {
if (_snackBarController.isCompleted && _snackBarTimer == null) {
final SnackBar snackBar = _snackBars.first._widget;
_snackBarTimer = new Timer.periodic(snackBar.duration, (Timer timer) {
_snackBarTimer = new Timer(snackBar.duration, () {
assert(_snackBarController.status == AnimationStatus.forward ||
_snackBarController.status == AnimationStatus.completed);
// Look up MediaQuery again in case the setting changed.
final MediaQueryData mediaQuery = MediaQuery.of(context);
if (mediaQuery.accessibleNavigation && snackBar.action != null)
return;
timer.cancel();
hideCurrentSnackBar(reason: SnackBarClosedReason.timeout);
});
}
Expand Down

0 comments on commit 5177a37

Please sign in to comment.