From af61c6c73fb1057633c84bde5b183f8fc3b0673a Mon Sep 17 00:00:00 2001 From: Shanmugam <81793777+shanmugam28@users.noreply.github.com> Date: Wed, 6 Dec 2023 01:00:15 +0530 Subject: [PATCH] Support for dismissDirection property in SnackBarTheme (#139068) This PR introduces a new feature that allows users to configure the 'dismissDirection' in SnackBarTheme. This enhancement provides users with the flexibility to set the 'dismissDirection' property in the ThemeData, rather than having to apply it each time when initializing a snack bar. This streamlines the process and makes it more convenient for users to manage and customize the behavior of snack bars within their applications. Fixes #139012 --- .../flutter/lib/src/material/snack_bar.dart | 11 ++- .../lib/src/material/snack_bar_theme.dart | 18 +++- .../flutter/test/material/snack_bar_test.dart | 95 +++++++++++++++++++ .../test/material/snack_bar_theme_test.dart | 8 +- 4 files changed, 123 insertions(+), 9 deletions(-) diff --git a/packages/flutter/lib/src/material/snack_bar.dart b/packages/flutter/lib/src/material/snack_bar.dart index 520abc225a..51f8ce1cf9 100644 --- a/packages/flutter/lib/src/material/snack_bar.dart +++ b/packages/flutter/lib/src/material/snack_bar.dart @@ -289,7 +289,7 @@ class SnackBar extends StatefulWidget { this.duration = _snackBarDisplayDuration, this.animation, this.onVisible, - this.dismissDirection = DismissDirection.down, + this.dismissDirection, this.clipBehavior = Clip.hardEdge, }) : assert(elevation == null || elevation >= 0.0), assert(width == null || margin == null, @@ -463,8 +463,10 @@ class SnackBar extends StatefulWidget { /// The direction in which the SnackBar can be dismissed. /// - /// Defaults to [DismissDirection.down]. - final DismissDirection dismissDirection; + /// If this property is null, then [SnackBarThemeData.dismissDirection] of + /// [ThemeData.snackBarTheme] is used. If that is null, then the default is + /// [DismissDirection.down]. + final DismissDirection? dismissDirection; /// {@macro flutter.material.Material.clipBehavior} /// @@ -737,6 +739,7 @@ class _SnackBarState extends State { final double elevation = widget.elevation ?? snackBarTheme.elevation ?? defaults.elevation!; final Color backgroundColor = widget.backgroundColor ?? snackBarTheme.backgroundColor ?? defaults.backgroundColor!; final ShapeBorder? shape = widget.shape ?? snackBarTheme.shape ?? (isFloatingSnackBar ? defaults.shape : null); + final DismissDirection dismissDirection = widget.dismissDirection ?? snackBarTheme.dismissDirection ?? DismissDirection.down; snackBar = Material( shape: shape, @@ -783,7 +786,7 @@ class _SnackBarState extends State { }, child: Dismissible( key: const Key('dismissible'), - direction: widget.dismissDirection, + direction: dismissDirection, resizeDuration: null, behavior: widget.hitTestBehavior ?? (widget.margin != null ? HitTestBehavior.deferToChild : HitTestBehavior.opaque), onDismissed: (DismissDirection direction) { diff --git a/packages/flutter/lib/src/material/snack_bar_theme.dart b/packages/flutter/lib/src/material/snack_bar_theme.dart index 938c0b9e83..d0819ac69a 100644 --- a/packages/flutter/lib/src/material/snack_bar_theme.dart +++ b/packages/flutter/lib/src/material/snack_bar_theme.dart @@ -70,7 +70,8 @@ class SnackBarThemeData with Diagnosticable { this.closeIconColor, this.actionOverflowThreshold, this.actionBackgroundColor, - this.disabledActionBackgroundColor + this.disabledActionBackgroundColor, + this.dismissDirection, }) : assert(elevation == null || elevation >= 0.0), assert(width == null || identical(behavior, SnackBarBehavior.floating), 'Width can only be set if behaviour is SnackBarBehavior.floating'), @@ -158,6 +159,11 @@ class SnackBarThemeData with Diagnosticable { /// If null, [SnackBarAction] falls back to [Colors.transparent]. final Color? disabledActionBackgroundColor; + /// Overrides the default value for [SnackBar.dismissDirection]. + /// + /// If null, [SnackBar] will default to [DismissDirection.down]. + final DismissDirection? dismissDirection; + /// Creates a copy of this object with the given fields replaced with the /// new values. SnackBarThemeData copyWith({ @@ -175,6 +181,7 @@ class SnackBarThemeData with Diagnosticable { double? actionOverflowThreshold, Color? actionBackgroundColor, Color? disabledActionBackgroundColor, + DismissDirection? dismissDirection, }) { return SnackBarThemeData( backgroundColor: backgroundColor ?? this.backgroundColor, @@ -191,6 +198,7 @@ class SnackBarThemeData with Diagnosticable { actionOverflowThreshold: actionOverflowThreshold ?? this.actionOverflowThreshold, actionBackgroundColor: actionBackgroundColor ?? this.actionBackgroundColor, disabledActionBackgroundColor: disabledActionBackgroundColor ?? this.disabledActionBackgroundColor, + dismissDirection: dismissDirection ?? this.dismissDirection, ); } @@ -215,6 +223,7 @@ class SnackBarThemeData with Diagnosticable { actionOverflowThreshold: lerpDouble(a?.actionOverflowThreshold, b?.actionOverflowThreshold, t), actionBackgroundColor: Color.lerp(a?.actionBackgroundColor, b?.actionBackgroundColor, t), disabledActionBackgroundColor: Color.lerp(a?.disabledActionBackgroundColor, b?.disabledActionBackgroundColor, t), + dismissDirection: t < 0.5 ? a?.dismissDirection : b?.dismissDirection, ); } @@ -233,7 +242,8 @@ class SnackBarThemeData with Diagnosticable { closeIconColor, actionOverflowThreshold, actionBackgroundColor, - disabledActionBackgroundColor + disabledActionBackgroundColor, + dismissDirection, ); @override @@ -258,7 +268,8 @@ class SnackBarThemeData with Diagnosticable { && other.closeIconColor == closeIconColor && other.actionOverflowThreshold == actionOverflowThreshold && other.actionBackgroundColor == actionBackgroundColor - && other.disabledActionBackgroundColor == disabledActionBackgroundColor; + && other.disabledActionBackgroundColor == disabledActionBackgroundColor + && other.dismissDirection == dismissDirection; } @override @@ -278,5 +289,6 @@ class SnackBarThemeData with Diagnosticable { properties.add(DoubleProperty('actionOverflowThreshold', actionOverflowThreshold, defaultValue: null)); properties.add(ColorProperty('actionBackgroundColor', actionBackgroundColor, defaultValue: null)); properties.add(ColorProperty('disabledActionBackgroundColor', disabledActionBackgroundColor, defaultValue: null)); + properties.add(DiagnosticsProperty('dismissDirection', dismissDirection, defaultValue: null)); } } diff --git a/packages/flutter/test/material/snack_bar_test.dart b/packages/flutter/test/material/snack_bar_test.dart index db2fd6118a..c0f59b557a 100644 --- a/packages/flutter/test/material/snack_bar_test.dart +++ b/packages/flutter/test/material/snack_bar_test.dart @@ -265,6 +265,101 @@ void main() { ); }); + testWidgetsWithLeakTracking('SnackBar dismissDirection can be customised from SnackBarThemeData', (WidgetTester tester) async { + const Key tapTarget = Key('tap-target'); + late double width; + + await tester.pumpWidget(MaterialApp( + theme: ThemeData( + snackBarTheme: const SnackBarThemeData( + dismissDirection: DismissDirection.startToEnd, + ) + ), + home: Scaffold( + body: Builder( + builder: (BuildContext context) { + width = MediaQuery.sizeOf(context).width; + + return GestureDetector( + key: tapTarget, + onTap: () { + ScaffoldMessenger.of(context).showSnackBar(const SnackBar( + content: Text('swipe ltr'), + duration: Duration(seconds: 2), + )); + }, + behavior: HitTestBehavior.opaque, + child: const SizedBox( + height: 100.0, + width: 100.0, + ), + ); + }, + ), + ), + )); + + expect(find.text('swipe ltr'), findsNothing); + await tester.tap(find.byKey(tapTarget)); + expect(find.text('swipe ltr'), findsNothing); + await tester.pump(); // schedule animation for snack bar + expect(find.text('swipe ltr'), findsOneWidget); + await tester.pump(); // begin animation + expect(find.text('swipe ltr'), findsOneWidget); + await tester.pump(const Duration(milliseconds: 750)); + await tester.drag(find.text('swipe ltr'), Offset(width, 0.0)); + await tester.pump(); // snack bar dismissed + expect(find.text('swipe ltr'), findsNothing); + }); + + testWidgetsWithLeakTracking('dismissDirection from SnackBar should be preferred over SnackBarThemeData', (WidgetTester tester) async { + const Key tapTarget = Key('tap-target'); + late double width; + + await tester.pumpWidget(MaterialApp( + theme: ThemeData( + snackBarTheme: const SnackBarThemeData( + dismissDirection: DismissDirection.startToEnd, + ) + ), + home: Scaffold( + body: Builder( + builder: (BuildContext context) { + width = MediaQuery.sizeOf(context).width; + + return GestureDetector( + key: tapTarget, + onTap: () { + ScaffoldMessenger.of(context).showSnackBar(const SnackBar( + content: Text('swipe rtl'), + duration: Duration(seconds: 2), + dismissDirection: DismissDirection.endToStart, + )); + }, + behavior: HitTestBehavior.opaque, + child: const SizedBox( + height: 100.0, + width: 100.0, + ), + ); + }, + ), + ), + )); + + expect(find.text('swipe rtl'), findsNothing); + await tester.tap(find.byKey(tapTarget)); + expect(find.text('swipe rtl'), findsNothing); + await tester.pump(); // schedule animation for snack bar + expect(find.text('swipe rtl'), findsOneWidget); + await tester.pump(); // begin animation + expect(find.text('swipe rtl'), findsOneWidget); + await tester.pump(const Duration(milliseconds: 750)); + await tester.drag(find.text('swipe rtl'), Offset(-width, 0.0)); + await tester.pump(); // snack bar dismissed + expect(find.text('swipe rtl'), findsNothing); + }); + testWidgetsWithLeakTracking('SnackBar cannot be tapped twice', (WidgetTester tester) async { int tapCount = 0; await tester.pumpWidget(MaterialApp( diff --git a/packages/flutter/test/material/snack_bar_theme_test.dart b/packages/flutter/test/material/snack_bar_theme_test.dart index d39bdf418c..88dd74e8af 100644 --- a/packages/flutter/test/material/snack_bar_theme_test.dart +++ b/packages/flutter/test/material/snack_bar_theme_test.dart @@ -74,6 +74,7 @@ void main() { showCloseIcon: false, closeIconColor: Color(0xFF0000AA), actionOverflowThreshold: 0.5, + dismissDirection: DismissDirection.down, ).debugFillProperties(builder); final List description = builder.properties @@ -94,6 +95,7 @@ void main() { 'showCloseIcon: false', 'closeIconColor: Color(0xff0000aa)', 'actionOverflowThreshold: 0.5', + 'dismissDirection: DismissDirection.down', ]); }); @@ -618,7 +620,8 @@ SnackBarThemeData _createSnackBarTheme({ ShapeBorder? shape, SnackBarBehavior? behavior, Color? actionBackgroundColor, - Color? disabledActionBackgroundColor + Color? disabledActionBackgroundColor, + DismissDirection? dismissDirection }) { return SnackBarThemeData( backgroundColor: backgroundColor, @@ -629,7 +632,8 @@ SnackBarThemeData _createSnackBarTheme({ shape: shape, behavior: behavior, actionBackgroundColor: actionBackgroundColor, - disabledActionBackgroundColor: disabledActionBackgroundColor + disabledActionBackgroundColor: disabledActionBackgroundColor, + dismissDirection: dismissDirection ); }