From bc396d1b5cf59fee72f285ec18d4f0817cad1800 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Thu, 17 Oct 2019 15:12:25 -0700 Subject: [PATCH] Add onVisible callback to snackbar. (#42344) --- .../flutter/lib/src/material/snack_bar.dart | 130 ++++++++++++------ .../flutter/test/material/snack_bar_test.dart | 81 +++++++++++ 2 files changed, 170 insertions(+), 41 deletions(-) diff --git a/packages/flutter/lib/src/material/snack_bar.dart b/packages/flutter/lib/src/material/snack_bar.dart index 660c77e4e6..308cc86a1a 100644 --- a/packages/flutter/lib/src/material/snack_bar.dart +++ b/packages/flutter/lib/src/material/snack_bar.dart @@ -161,7 +161,7 @@ class _SnackBarActionState extends State { /// * [SnackBarThemeData], to configure the default property values for /// [SnackBar] widgets. /// * -class SnackBar extends StatelessWidget { +class SnackBar extends StatefulWidget { /// Creates a snack bar. /// /// The [content] argument must be non-null. The [elevation] must be null or @@ -176,6 +176,7 @@ class SnackBar extends StatelessWidget { this.action, this.duration = _snackBarDisplayDuration, this.animation, + this.onVisible, }) : assert(elevation == null || elevation >= 0.0), assert(content != null), assert(duration != null), @@ -245,10 +246,86 @@ class SnackBar extends StatelessWidget { /// The animation driving the entrance and exit of the snack bar. final Animation animation; + /// Called the first time that the snackbar is visible within a [Scaffold]. + final VoidCallback onVisible; + + // API for Scaffold.showSnackBar(): + + /// Creates an animation controller useful for driving a snack bar's entrance and exit animation. + static AnimationController createAnimationController({ @required TickerProvider vsync }) { + return AnimationController( + duration: _snackBarTransitionDuration, + debugLabel: 'SnackBar', + vsync: vsync, + ); + } + + /// Creates a copy of this snack bar but with the animation replaced with the given animation. + /// + /// If the original snack bar lacks a key, the newly created snack bar will + /// use the given fallback key. + SnackBar withAnimation(Animation newAnimation, { Key fallbackKey }) { + return SnackBar( + key: key ?? fallbackKey, + content: content, + backgroundColor: backgroundColor, + elevation: elevation, + shape: shape, + behavior: behavior, + action: action, + duration: duration, + animation: newAnimation, + onVisible: onVisible, + ); + } + + @override + State createState() => _SnackBarState(); +} + + +class _SnackBarState extends State { + bool _wasVisible = false; + + @override + void initState() { + super.initState(); + widget.animation.addStatusListener(_onAnimationStatusChanged); + } + + @override + void didUpdateWidget(SnackBar oldWidget) { + if (widget.animation != oldWidget.animation) { + oldWidget.animation.removeStatusListener(_onAnimationStatusChanged); + widget.animation.addStatusListener(_onAnimationStatusChanged); + } + super.didUpdateWidget(oldWidget); + } + + @override + void dispose() { + widget.animation.removeStatusListener(_onAnimationStatusChanged); + super.dispose(); + } + + void _onAnimationStatusChanged(AnimationStatus animationStatus) { + switch (animationStatus) { + case AnimationStatus.dismissed: + case AnimationStatus.forward: + case AnimationStatus.reverse: + break; + case AnimationStatus.completed: + if (widget.onVisible != null && !_wasVisible) { + widget.onVisible(); + } + _wasVisible = true; + } + } + @override Widget build(BuildContext context) { final MediaQueryData mediaQueryData = MediaQuery.of(context); - assert(animation != null); + assert(widget.animation != null); final ThemeData theme = Theme.of(context); final ColorScheme colorScheme = theme.colorScheme; final SnackBarThemeData snackBarTheme = theme.snackBarTheme; @@ -284,14 +361,14 @@ class SnackBar extends StatelessWidget { ); final TextStyle contentTextStyle = snackBarTheme.contentTextStyle ?? inverseTheme.textTheme.subhead; - final SnackBarBehavior snackBarBehavior = behavior ?? snackBarTheme.behavior ?? SnackBarBehavior.fixed; + final SnackBarBehavior snackBarBehavior = widget.behavior ?? snackBarTheme.behavior ?? SnackBarBehavior.fixed; final bool isFloatingSnackBar = snackBarBehavior == SnackBarBehavior.floating; final double snackBarPadding = isFloatingSnackBar ? 16.0 : 24.0; - final CurvedAnimation heightAnimation = CurvedAnimation(parent: animation, curve: _snackBarHeightCurve); - final CurvedAnimation fadeInAnimation = CurvedAnimation(parent: animation, curve: _snackBarFadeInCurve); + final CurvedAnimation heightAnimation = CurvedAnimation(parent: widget.animation, curve: _snackBarHeightCurve); + final CurvedAnimation fadeInAnimation = CurvedAnimation(parent: widget.animation, curve: _snackBarFadeInCurve); final CurvedAnimation fadeOutAnimation = CurvedAnimation( - parent: animation, + parent: widget.animation, curve: _snackBarFadeOutCurve, reverseCurve: const Threshold(0.0), ); @@ -308,16 +385,16 @@ class SnackBar extends StatelessWidget { padding: const EdgeInsets.symmetric(vertical: _singleLineVerticalPadding), child: DefaultTextStyle( style: contentTextStyle, - child: content, + child: widget.content, ), ), ), - if (action != null) + if (widget.action != null) ButtonTheme( textTheme: ButtonTextTheme.accent, minWidth: 64.0, padding: EdgeInsets.symmetric(horizontal: snackBarPadding), - child: action, + child: widget.action, ) else SizedBox(width: snackBarPadding), @@ -325,9 +402,9 @@ class SnackBar extends StatelessWidget { ), ); - final double elevation = this.elevation ?? snackBarTheme.elevation ?? 6.0; - final Color backgroundColor = this.backgroundColor ?? snackBarTheme.backgroundColor ?? inverseTheme.backgroundColor; - final ShapeBorder shape = this.shape + final double elevation = widget.elevation ?? snackBarTheme.elevation ?? 6.0; + final Color backgroundColor = widget.backgroundColor ?? snackBarTheme.backgroundColor ?? inverseTheme.backgroundColor; + final ShapeBorder shape = widget.shape ?? snackBarTheme.shape ?? (isFloatingSnackBar ? RoundedRectangleBorder(borderRadius: BorderRadius.circular(4.0)) : null); @@ -394,33 +471,4 @@ class SnackBar extends StatelessWidget { return ClipRect(child: snackBarTransition); } - - // API for Scaffold.addSnackBar(): - - /// Creates an animation controller useful for driving a snack bar's entrance and exit animation. - static AnimationController createAnimationController({ @required TickerProvider vsync }) { - return AnimationController( - duration: _snackBarTransitionDuration, - debugLabel: 'SnackBar', - vsync: vsync, - ); - } - - /// Creates a copy of this snack bar but with the animation replaced with the given animation. - /// - /// If the original snack bar lacks a key, the newly created snack bar will - /// use the given fallback key. - SnackBar withAnimation(Animation newAnimation, { Key fallbackKey }) { - return SnackBar( - key: key ?? fallbackKey, - content: content, - backgroundColor: backgroundColor, - elevation: elevation, - shape: shape, - behavior: behavior, - action: action, - duration: duration, - animation: newAnimation, - ); - } } diff --git a/packages/flutter/test/material/snack_bar_test.dart b/packages/flutter/test/material/snack_bar_test.dart index db5d963bca..8f98020caf 100644 --- a/packages/flutter/test/material/snack_bar_test.dart +++ b/packages/flutter/test/material/snack_bar_test.dart @@ -1065,4 +1065,85 @@ void main() { await tester.tap(find.byKey(tapTarget)); expect(tester.takeException(), isNotNull); }); + + testWidgets('Snackbar calls onVisible once', (WidgetTester tester) async { + const Key tapTarget = Key('tap-target'); + int called = 0; + await tester.pumpWidget(MaterialApp( + home: Scaffold( + body: Builder( + builder: (BuildContext context) { + return GestureDetector( + onTap: () { + Scaffold.of(context).showSnackBar(SnackBar( + content: const Text('hello'), + duration: const Duration(seconds: 1), + onVisible: () { + called += 1; + }, + )); + }, + behavior: HitTestBehavior.opaque, + child: Container( + height: 100.0, + width: 100.0, + key: tapTarget, + ), + ); + }, + ), + ), + )); + + await tester.tap(find.byKey(tapTarget)); + await tester.pump(); // start animation + await tester.pumpAndSettle(); + + expect(find.text('hello'), findsOneWidget); + expect(called, 1); + }); + + testWidgets('Snackbar does not call onVisible when it is queued', (WidgetTester tester) async { + const Key tapTarget = Key('tap-target'); + int called = 0; + await tester.pumpWidget(MaterialApp( + home: Scaffold( + body: Builder( + builder: (BuildContext context) { + return GestureDetector( + onTap: () { + Scaffold.of(context).showSnackBar(SnackBar( + content: const Text('hello'), + duration: const Duration(seconds: 1), + onVisible: () { + called += 1; + }, + )); + Scaffold.of(context).showSnackBar(SnackBar( + content: const Text('hello 2'), + duration: const Duration(seconds: 1), + onVisible: () { + called += 1; + }, + )); + }, + behavior: HitTestBehavior.opaque, + child: Container( + height: 100.0, + width: 100.0, + key: tapTarget, + ), + ); + }, + ), + ), + )); + + await tester.tap(find.byKey(tapTarget)); + await tester.pump(); // start animation + await tester.pumpAndSettle(); + + expect(find.text('hello'), findsOneWidget); + expect(called, 1); + }); }