Skip to content

Commit

Permalink
Add transitionOnUserGestures to true on SnackBar for back swipe (#106…
Browse files Browse the repository at this point in the history
…638)
  • Loading branch information
letsar authored Jul 19, 2022
1 parent dbe38ee commit be0c370
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/flutter/lib/src/material/snack_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ class _SnackBarActionState extends State<SnackBarAction> {
/// A SnackBar with an action will not time out when TalkBack or VoiceOver are
/// enabled. This is controlled by [AccessibilityFeatures.accessibleNavigation].
///
/// During page transitions, the [SnackBar] will smoothly animate to its
/// location on the other page. For example if the [SnackBar.behavior] is set to
/// [SnackBarBehavior.floating] and the next page has a floating action button,
/// while the current one does not, the [SnackBar] will smoothly animate above
/// the floating action button. It also works in the case of a back gesture
/// transition.
///
/// {@tool dartpad}
/// Here is an example of a [SnackBar] with an [action] button implemented using
/// [SnackBarAction].
Expand Down Expand Up @@ -620,6 +627,7 @@ class _SnackBarState extends State<SnackBar> {

return Hero(
tag: '<SnackBar Hero tag - ${widget.content}>',
transitionOnUserGestures: true,
child: ClipRect(
clipBehavior: widget.clipBehavior,
child: snackBarTransition,
Expand Down
76 changes: 76 additions & 0 deletions packages/flutter/test/material/snack_bar_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1795,6 +1795,82 @@ void main() {
expect(find.text(secondHeader), findsOneWidget);
});

testWidgets('Should have only one SnackBar during back swipe navigation', (WidgetTester tester) async {
const String snackBarText = 'hello snackbar';
const Key snackTarget = Key('snack-target');
const Key transitionTarget = Key('transition-target');

Widget buildApp() {
final PageTransitionsTheme pageTransitionTheme = PageTransitionsTheme(
builders: <TargetPlatform, PageTransitionsBuilder>{
for(final TargetPlatform platform in TargetPlatform.values)
platform: const CupertinoPageTransitionsBuilder(),
},
);
return MaterialApp(
theme: ThemeData(pageTransitionsTheme: pageTransitionTheme),
initialRoute: '/',
routes: <String, WidgetBuilder> {
'/': (BuildContext context) {
return Scaffold(
body: Center(
child: ElevatedButton(
key: transitionTarget,
child: const Text('PUSH'),
onPressed: () {
Navigator.of(context).pushNamed('/second');
},
),
),

);
},
'/second': (BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
key: snackTarget,
onPressed: () async {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text(snackBarText),
),
);
},
child: const Text('X'),
),
);
},
},
);
}
await tester.pumpWidget(buildApp());

// Transition to second page.
await tester.tap(find.byKey(transitionTarget));
await tester.pumpAndSettle();

// Present SnackBar
await tester.tap(find.byKey(snackTarget));
await tester.pump(); // schedule animation
expect(find.text(snackBarText), findsOneWidget);
await tester.pump(); // begin animation
expect(find.text(snackBarText), findsOneWidget);
await tester.pump(const Duration(milliseconds: 750));
expect(find.text(snackBarText), findsOneWidget);

// Start the gesture at the edge of the screen.
final TestGesture gesture = await tester.startGesture(const Offset(5.0, 200.0));
// Trigger the swipe.
await gesture.moveBy(const Offset(100.0, 0.0));

// Back gestures should trigger and draw the hero transition in the very same
// frame (since the "from" route has already moved to reveal the "to" route).
await tester.pump();

// We should have only one SnackBar displayed on the screen.
expect(find.text(snackBarText), findsOneWidget);
});

testWidgets('SnackBars should be shown above the bottomSheet', (WidgetTester tester) async {
await tester.pumpWidget(const MaterialApp(
home: Scaffold(
Expand Down

0 comments on commit be0c370

Please sign in to comment.