forked from firka/flutter
[reland] Add AnimationStyle to showSnackBar (#143052)
[Original was reverted due to uncaught analysis failure ](https://github.com/flutter/flutter/pull/142825#issuecomment-1930620085) --- fixes [`showSnackBar` is always replacing `animation` parameter of `SnackBar`](https://github.com/flutter/flutter/issues/141646) ### Code sample preview 
This commit is contained in:
@@ -290,13 +290,38 @@ class ScaffoldMessengerState extends State<ScaffoldMessenger> with TickerProvide
|
||||
/// ** See code in examples/api/lib/material/scaffold/scaffold_messenger_state.show_snack_bar.1.dart **
|
||||
/// {@end-tool}
|
||||
///
|
||||
ScaffoldFeatureController<SnackBar, SnackBarClosedReason> showSnackBar(SnackBar snackBar) {
|
||||
/// If [AnimationStyle.duration] is provided in the [snackBarAnimationStyle]
|
||||
/// parameter, it will be used to override the snackbar show animation duration.
|
||||
/// Otherwise, defaults to 250ms.
|
||||
///
|
||||
/// If [AnimationStyle.reverseDuration] is provided in the [snackBarAnimationStyle]
|
||||
/// parameter, it will be used to override the snackbar hide animation duration.
|
||||
/// Otherwise, defaults to 250ms.
|
||||
///
|
||||
/// To disable the snackbar animation, use [AnimationStyle.noAnimation].
|
||||
///
|
||||
/// {@tool dartpad}
|
||||
/// This sample showcases how to override [SnackBar] show and hide animation
|
||||
/// duration using [AnimationStyle] in [ScaffoldMessengerState.showSnackBar].
|
||||
///
|
||||
/// ** See code in examples/api/lib/material/scaffold/scaffold_messenger_state.show_snack_bar.2.dart **
|
||||
/// {@end-tool}
|
||||
///
|
||||
ScaffoldFeatureController<SnackBar, SnackBarClosedReason> showSnackBar(
|
||||
SnackBar snackBar,
|
||||
{ AnimationStyle? snackBarAnimationStyle }
|
||||
) {
|
||||
assert(
|
||||
_scaffolds.isNotEmpty,
|
||||
'ScaffoldMessenger.showSnackBar was called, but there are currently no '
|
||||
'descendant Scaffolds to present to.',
|
||||
);
|
||||
_snackBarController ??= SnackBar.createAnimationController(vsync: this)
|
||||
_didUpdateAnimationStyle(snackBarAnimationStyle);
|
||||
_snackBarController ??= SnackBar.createAnimationController(
|
||||
duration: snackBarAnimationStyle?.duration,
|
||||
reverseDuration: snackBarAnimationStyle?.reverseDuration,
|
||||
vsync: this,
|
||||
)
|
||||
..addStatusListener(_handleSnackBarStatusChanged);
|
||||
if (_snackBars.isEmpty) {
|
||||
assert(_snackBarController!.isDismissed);
|
||||
@@ -355,6 +380,16 @@ class ScaffoldMessengerState extends State<ScaffoldMessenger> with TickerProvide
|
||||
return controller;
|
||||
}
|
||||
|
||||
void _didUpdateAnimationStyle(AnimationStyle? snackBarAnimationStyle) {
|
||||
if (snackBarAnimationStyle != null) {
|
||||
if (_snackBarController?.duration != snackBarAnimationStyle.duration ||
|
||||
_snackBarController?.reverseDuration != snackBarAnimationStyle.reverseDuration) {
|
||||
_snackBarController?.dispose();
|
||||
_snackBarController = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _handleSnackBarStatusChanged(AnimationStatus status) {
|
||||
switch (status) {
|
||||
case AnimationStatus.dismissed:
|
||||
|
||||
@@ -476,9 +476,14 @@ class SnackBar extends StatefulWidget {
|
||||
// API for ScaffoldMessengerState.showSnackBar():
|
||||
|
||||
/// Creates an animation controller useful for driving a snack bar's entrance and exit animation.
|
||||
static AnimationController createAnimationController({ required TickerProvider vsync }) {
|
||||
static AnimationController createAnimationController({
|
||||
required TickerProvider vsync,
|
||||
Duration? duration,
|
||||
Duration? reverseDuration,
|
||||
}) {
|
||||
return AnimationController(
|
||||
duration: _snackBarTransitionDuration,
|
||||
duration: duration ?? _snackBarTransitionDuration,
|
||||
reverseDuration: reverseDuration,
|
||||
debugLabel: 'SnackBar',
|
||||
vsync: vsync,
|
||||
);
|
||||
|
||||
@@ -2868,6 +2868,241 @@ void main() {
|
||||
expect(tester.takeException(), isNull);
|
||||
});
|
||||
|
||||
testWidgets('ScaffoldMessenger showSnackBar default animatiom', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(MaterialApp(
|
||||
home: Scaffold(
|
||||
body: Builder(
|
||||
builder: (BuildContext context) {
|
||||
return ElevatedButton(
|
||||
onPressed: () {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('I am a snack bar.'),
|
||||
showCloseIcon: true,
|
||||
),
|
||||
);
|
||||
},
|
||||
child: const Text('Show SnackBar'),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
));
|
||||
|
||||
// Tap the button to show the SnackBar.
|
||||
await tester.tap(find.byType(ElevatedButton));
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(milliseconds: 125)); // Advance the animation by 125ms.
|
||||
|
||||
// The SnackBar is partially visible.
|
||||
expect(tester.getTopLeft(find.text('I am a snack bar.')).dy, closeTo(576.7, 0.1));
|
||||
|
||||
await tester.pump(const Duration(milliseconds: 125)); // Advance the animation by 125ms.
|
||||
|
||||
// The SnackBar is fully visible.
|
||||
expect(tester.getTopLeft(find.text('I am a snack bar.')).dy, closeTo(566, 0.1));
|
||||
|
||||
// Tap the close button to dismiss the SnackBar.
|
||||
await tester.tap(find.byType(IconButton));
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(milliseconds: 125)); // Advance the animation by 125ms.
|
||||
|
||||
// The SnackBar is partially visible.
|
||||
expect(tester.getTopLeft(find.text('I am a snack bar.')).dy, closeTo(576.7, 0.1));
|
||||
|
||||
await tester.pump(const Duration(milliseconds: 125)); // Advance the animation by 125ms.
|
||||
|
||||
// The SnackBar is dismissed.
|
||||
expect(tester.getTopLeft(find.text('I am a snack bar.')).dy, closeTo(614, 0.1));
|
||||
});
|
||||
|
||||
testWidgets('ScaffoldMessenger showSnackBar animation can be customized', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(MaterialApp(
|
||||
home: Scaffold(
|
||||
body: Builder(
|
||||
builder: (BuildContext context) {
|
||||
return ElevatedButton(
|
||||
onPressed: () {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('I am a snack bar.'),
|
||||
showCloseIcon: true,
|
||||
),
|
||||
snackBarAnimationStyle: AnimationStyle(
|
||||
duration: const Duration(milliseconds: 1200),
|
||||
reverseDuration: const Duration(milliseconds: 600),
|
||||
),
|
||||
);
|
||||
},
|
||||
child: const Text('Show SnackBar'),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
));
|
||||
|
||||
// Tap the button to show the SnackBar.
|
||||
await tester.tap(find.byType(ElevatedButton));
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(milliseconds: 300)); // Advance the animation by 300ms.
|
||||
|
||||
// The SnackBar is partially visible.
|
||||
expect(tester.getTopLeft(find.text('I am a snack bar.')).dy, closeTo(602.6, 0.1));
|
||||
|
||||
await tester.pump(const Duration(milliseconds: 300)); // Advance the animation by 300ms.
|
||||
|
||||
// The SnackBar is partially visible.
|
||||
expect(tester.getTopLeft(find.text('I am a snack bar.')).dy, closeTo(576.7, 0.1));
|
||||
|
||||
await tester.pump(const Duration(milliseconds: 600)); // Advance the animation by 600ms.
|
||||
|
||||
// The SnackBar is fully visible.
|
||||
expect(tester.getTopLeft(find.text('I am a snack bar.')).dy, closeTo(566, 0.1));
|
||||
|
||||
// Tap the close button to dismiss the SnackBar.
|
||||
await tester.tap(find.byType(IconButton));
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(milliseconds: 300)); // Advance the animation by 300ns.
|
||||
|
||||
// The SnackBar is partially visible.
|
||||
expect(tester.getTopLeft(find.text('I am a snack bar.')).dy, closeTo(576.7, 0.1));
|
||||
|
||||
await tester.pump(const Duration(milliseconds: 300)); // Advance the animation by 300ms.
|
||||
|
||||
// The SnackBar is dismissed.
|
||||
expect(tester.getTopLeft(find.text('I am a snack bar.')).dy, closeTo(614, 0.1));
|
||||
});
|
||||
|
||||
testWidgets('Updated snackBarAnimationStyle updates snack bar animation', (WidgetTester tester) async {
|
||||
Widget buildSnackBar(AnimationStyle snackBarAnimationStyle) {
|
||||
return MaterialApp(
|
||||
home: Scaffold(
|
||||
body: Builder(
|
||||
builder: (BuildContext context) {
|
||||
return ElevatedButton(
|
||||
onPressed: () {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('I am a snack bar.'),
|
||||
showCloseIcon: true,
|
||||
),
|
||||
snackBarAnimationStyle: snackBarAnimationStyle,
|
||||
);
|
||||
},
|
||||
child: const Text('Show SnackBar'),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// Test custom animation style.
|
||||
await tester.pumpWidget(buildSnackBar(AnimationStyle(
|
||||
duration: const Duration(milliseconds: 800),
|
||||
reverseDuration: const Duration(milliseconds: 400),
|
||||
)));
|
||||
|
||||
// Tap the button to show the SnackBar.
|
||||
await tester.tap(find.byType(ElevatedButton));
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(milliseconds: 400)); // Advance the animation by 400ms.
|
||||
|
||||
// The SnackBar is partially visible.
|
||||
expect(tester.getTopLeft(find.text('I am a snack bar.')).dy, closeTo(576.7, 0.1));
|
||||
|
||||
await tester.pump(const Duration(milliseconds: 400)); // Advance the animation by 400ms.
|
||||
|
||||
// The SnackBar is fully visible.
|
||||
expect(tester.getTopLeft(find.text('I am a snack bar.')).dy, closeTo(566, 0.1));
|
||||
|
||||
// Tap the close button to dismiss the SnackBar.
|
||||
await tester.tap(find.byType(IconButton));
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(milliseconds: 400)); // Advance the animation by 400ms.
|
||||
|
||||
// The SnackBar is dismissed.
|
||||
expect(tester.getTopLeft(find.text('I am a snack bar.')).dy, closeTo(614, 0.1));
|
||||
|
||||
// Test no animation style.
|
||||
await tester.pumpWidget(buildSnackBar(AnimationStyle.noAnimation));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// Tap the button to show the SnackBar.
|
||||
await tester.tap(find.byType(ElevatedButton));
|
||||
await tester.pump();
|
||||
|
||||
// The SnackBar is fully visible.
|
||||
expect(tester.getTopLeft(find.text('I am a snack bar.')).dy, closeTo(566, 0.1));
|
||||
|
||||
// Tap the close button to dismiss the SnackBar.
|
||||
await tester.tap(find.byType(IconButton));
|
||||
await tester.pump();
|
||||
|
||||
// The SnackBar is dismissed.
|
||||
expect(find.text('I am a snack bar.'), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('snackBarAnimationStyle with only reverseDuration uses default forward duration',
|
||||
(WidgetTester tester) async {
|
||||
Widget buildSnackBar(AnimationStyle snackBarAnimationStyle) {
|
||||
return MaterialApp(
|
||||
home: Scaffold(
|
||||
body: Builder(
|
||||
builder: (BuildContext context) {
|
||||
return ElevatedButton(
|
||||
onPressed: () {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('I am a snack bar.'),
|
||||
showCloseIcon: true,
|
||||
),
|
||||
snackBarAnimationStyle: snackBarAnimationStyle,
|
||||
);
|
||||
},
|
||||
child: const Text('Show SnackBar'),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// Test custom animation style with only reverseDuration.
|
||||
await tester.pumpWidget(buildSnackBar(AnimationStyle(
|
||||
reverseDuration: const Duration(milliseconds: 400),
|
||||
)));
|
||||
|
||||
// Tap the button to show the SnackBar.
|
||||
await tester.tap(find.byType(ElevatedButton));
|
||||
await tester.pump();
|
||||
// Advance the animation by 1/2 of the default forward duration.
|
||||
await tester.pump(const Duration(milliseconds: 125));
|
||||
|
||||
// The SnackBar is partially visible.
|
||||
expect(tester.getTopLeft(find.text('I am a snack bar.')).dy, closeTo(576.7, 0.1));
|
||||
|
||||
// Advance the animation by 1/2 of the default forward duration.
|
||||
await tester.pump(const Duration(milliseconds: 125)); // Advance the animation by 125ms.
|
||||
|
||||
// The SnackBar is fully visible.
|
||||
expect(tester.getTopLeft(find.text('I am a snack bar.')).dy, closeTo(566, 0.1));
|
||||
|
||||
// Tap the close button to dismiss the SnackBar.
|
||||
await tester.tap(find.byType(IconButton));
|
||||
await tester.pump();
|
||||
// Advance the animation by 1/2 of the reverse duration.
|
||||
await tester.pump(const Duration(milliseconds: 200));
|
||||
|
||||
// The SnackBar is partially visible.
|
||||
expect(tester.getTopLeft(find.text('I am a snack bar.')).dy, closeTo(576.7, 0.1));
|
||||
|
||||
// Advance the animation by 1/2 of the reverse duration.
|
||||
await tester.pump(const Duration(milliseconds: 200)); // Advance the animation by 200ms.
|
||||
|
||||
// The SnackBar is dismissed.
|
||||
expect(tester.getTopLeft(find.text('I am a snack bar.')).dy, closeTo(614, 0.1));
|
||||
});
|
||||
}
|
||||
|
||||
class _GeometryListener extends StatefulWidget {
|
||||
|
||||
Reference in New Issue
Block a user