From be0c37097e58abdcee2e766c54a1ff68e3e90c30 Mon Sep 17 00:00:00 2001 From: Romain Rastel Date: Wed, 20 Jul 2022 01:14:55 +0200 Subject: [PATCH] Add transitionOnUserGestures to true on SnackBar for back swipe (#106638) --- .../flutter/lib/src/material/snack_bar.dart | 8 ++ .../flutter/test/material/snack_bar_test.dart | 76 +++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/packages/flutter/lib/src/material/snack_bar.dart b/packages/flutter/lib/src/material/snack_bar.dart index 90c0ad965c..9c9da4a4a4 100644 --- a/packages/flutter/lib/src/material/snack_bar.dart +++ b/packages/flutter/lib/src/material/snack_bar.dart @@ -158,6 +158,13 @@ class _SnackBarActionState extends State { /// 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]. @@ -620,6 +627,7 @@ class _SnackBarState extends State { return Hero( tag: '', + transitionOnUserGestures: true, child: ClipRect( clipBehavior: widget.clipBehavior, child: snackBarTransition, diff --git a/packages/flutter/test/material/snack_bar_test.dart b/packages/flutter/test/material/snack_bar_test.dart index 98faa2a9cd..71b0775259 100644 --- a/packages/flutter/test/material/snack_bar_test.dart +++ b/packages/flutter/test/material/snack_bar_test.dart @@ -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: { + for(final TargetPlatform platform in TargetPlatform.values) + platform: const CupertinoPageTransitionsBuilder(), + }, + ); + return MaterialApp( + theme: ThemeData(pageTransitionsTheme: pageTransitionTheme), + initialRoute: '/', + routes: { + '/': (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(