[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:
@@ -0,0 +1,90 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// Flutter code sample for [SnackBar].
|
||||
|
||||
void main() => runApp(const SnackBarApp());
|
||||
|
||||
class SnackBarApp extends StatelessWidget {
|
||||
const SnackBarApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const MaterialApp(
|
||||
home: SnackBarExample(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
enum AnimationStyles { defaultStyle, custom, none }
|
||||
const List<(AnimationStyles, String)> animationStyleSegments = <(AnimationStyles, String)>[
|
||||
(AnimationStyles.defaultStyle, 'Default'),
|
||||
(AnimationStyles.custom, 'Custom'),
|
||||
(AnimationStyles.none, 'None'),
|
||||
];
|
||||
|
||||
class SnackBarExample extends StatefulWidget {
|
||||
const SnackBarExample({super.key});
|
||||
|
||||
@override
|
||||
State<SnackBarExample> createState() => _SnackBarExampleState();
|
||||
}
|
||||
|
||||
class _SnackBarExampleState extends State<SnackBarExample> {
|
||||
final Set<AnimationStyles> _animationStyleSelection = <AnimationStyles>{AnimationStyles.defaultStyle};
|
||||
AnimationStyle? _animationStyle;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('SnackBar Sample')),
|
||||
body: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
SegmentedButton<AnimationStyles>(
|
||||
selected: _animationStyleSelection,
|
||||
onSelectionChanged: (Set<AnimationStyles> styles) {
|
||||
setState(() {
|
||||
_animationStyle = switch (styles.first) {
|
||||
AnimationStyles.defaultStyle => null,
|
||||
AnimationStyles.custom => AnimationStyle(
|
||||
duration: const Duration(seconds: 3),
|
||||
reverseDuration: const Duration(seconds: 1),
|
||||
),
|
||||
AnimationStyles.none => AnimationStyle.noAnimation,
|
||||
};
|
||||
});
|
||||
},
|
||||
segments: animationStyleSegments
|
||||
.map<ButtonSegment<AnimationStyles>>(((AnimationStyles, String) shirt) {
|
||||
return ButtonSegment<AnimationStyles>(value: shirt.$1, label: Text(shirt.$2));
|
||||
})
|
||||
.toList(),
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
Builder(
|
||||
builder: (BuildContext context) {
|
||||
return ElevatedButton(
|
||||
onPressed: () {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('I am a snack bar.'),
|
||||
showCloseIcon: true,
|
||||
),
|
||||
snackBarAnimationStyle: _animationStyle,
|
||||
);
|
||||
},
|
||||
child: const Text('Show SnackBar'),
|
||||
);
|
||||
}
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_api_samples/material/scaffold/scaffold_messenger_state.show_snack_bar.2.dart' as example;
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('ScaffoldMessenger showSnackBar animation can be customized using AnimationStyle',
|
||||
(WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
const example.SnackBarApp(),
|
||||
);
|
||||
|
||||
// Tap the button to show the SnackBar with default animation style.
|
||||
await tester.tap(find.byType(ElevatedButton));
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(milliseconds: 125)); // Advance the animation by 125ms.
|
||||
|
||||
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.
|
||||
|
||||
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: 250)); // Advance the animation by 250ms.
|
||||
|
||||
expect(tester.getTopLeft(find.text('I am a snack bar.')).dy, closeTo(614, 0.1));
|
||||
|
||||
// Select custom animation style.
|
||||
await tester.tap(find.text('Custom'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// Tap the button to show the SnackBar with custom animation style.
|
||||
await tester.tap(find.byType(ElevatedButton));
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(milliseconds: 1500)); // Advance the animation by 125ms.
|
||||
|
||||
expect(tester.getTopLeft(find.text('I am a snack bar.')).dy, closeTo(576.7, 0.1));
|
||||
|
||||
await tester.pump(const Duration(milliseconds: 1500)); // Advance the animation by 125ms.
|
||||
|
||||
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(seconds: 1)); // Advance the animation by 1sec.
|
||||
|
||||
expect(tester.getTopLeft(find.text('I am a snack bar.')).dy, closeTo(614, 0.1));
|
||||
|
||||
// Select no animation style.
|
||||
await tester.tap(find.text('None'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// Tap the button to show the SnackBar with no animation style.
|
||||
await tester.tap(find.byType(ElevatedButton));
|
||||
await tester.pump();
|
||||
|
||||
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();
|
||||
|
||||
expect(find.text('I am a snack bar.'), findsNothing);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user