@@ -239,6 +239,7 @@ class SnackBar extends StatefulWidget {
239239 this .shape,
240240 this .behavior,
241241 this .action,
242+ this .actionOverflowThreshold,
242243 this .showCloseIcon,
243244 this .closeIconColor,
244245 this .duration = _snackBarDisplayDuration,
@@ -247,10 +248,11 @@ class SnackBar extends StatefulWidget {
247248 this .dismissDirection = DismissDirection .down,
248249 this .clipBehavior = Clip .hardEdge,
249250 }) : assert (elevation == null || elevation >= 0.0 ),
250- assert (
251- width == null || margin == null ,
251+ assert (width == null || margin == null ,
252252 'Width and margin can not be used together' ,
253- );
253+ ),
254+ assert (actionOverflowThreshold == null || (actionOverflowThreshold >= 0 && actionOverflowThreshold <= 1 ),
255+ 'Action overflow threshold must be between 0 and 1 inclusive' );
254256
255257 /// The primary content of the snack bar.
256258 ///
@@ -358,6 +360,18 @@ class SnackBar extends StatefulWidget {
358360 /// The action should not be "dismiss" or "cancel".
359361 final SnackBarAction ? action;
360362
363+ /// (optional) The percentage threshold for action widget's width before it overflows
364+ /// to a new line.
365+ ///
366+ /// Must be between 0 and 1. If the width of the snackbar's [content] is greater
367+ /// than this percentage of the width of the snackbar less the width of its [action] ,
368+ /// then the [action] will appear below the [content] .
369+ ///
370+ /// At a value of 0, the action will not overflow to a new line.
371+ ///
372+ /// Defaults to 0.25.
373+ final double ? actionOverflowThreshold;
374+
361375 /// (optional) Whether to include a "close" icon widget.
362376 ///
363377 /// Tapping the icon will close the snack bar.
@@ -431,6 +445,7 @@ class SnackBar extends StatefulWidget {
431445 shape: shape,
432446 behavior: behavior,
433447 action: action,
448+ actionOverflowThreshold: actionOverflowThreshold,
434449 showCloseIcon: showCloseIcon,
435450 closeIconColor: closeIconColor,
436451 duration: duration,
@@ -601,10 +616,11 @@ class _SnackBarState extends State<SnackBar> {
601616 final EdgeInsets margin = widget.margin? .resolve (TextDirection .ltr) ?? snackBarTheme.insetPadding ?? defaults.insetPadding! ;
602617
603618 final double snackBarWidth = widget.width ?? MediaQuery .sizeOf (context).width - (margin.left + margin.right);
604- // Action and Icon will overflow to a new line if their width is greater
605- // than one quarter of the total Snack Bar width.
606- final bool actionLineOverflow =
607- actionAndIconWidth / snackBarWidth > 0.25 ;
619+ final double actionOverflowThreshold = widget.actionOverflowThreshold
620+ ?? snackBarTheme.actionOverflowThreshold
621+ ?? defaults.actionOverflowThreshold! ;
622+
623+ final bool willOverflowAction = actionAndIconWidth / snackBarWidth > actionOverflowThreshold;
608624
609625 final List <Widget > maybeActionAndIcon = < Widget > [
610626 if (widget.action != null )
@@ -645,18 +661,17 @@ class _SnackBarState extends State<SnackBar> {
645661 ),
646662 ),
647663 ),
648- if ( ! actionLineOverflow ) ...maybeActionAndIcon,
649- if (actionLineOverflow ) SizedBox (width: snackBarWidth* 0.4 ),
664+ if ( ! willOverflowAction ) ...maybeActionAndIcon,
665+ if (willOverflowAction ) SizedBox (width: snackBarWidth * 0.4 ),
650666 ],
651667 ),
652- if (actionLineOverflow) Padding (
653- padding: const EdgeInsets .only (bottom: _singleLineVerticalPadding),
654- child: Row (mainAxisAlignment: MainAxisAlignment .end,
655- children: maybeActionAndIcon),
656- ),
657- ],
658-
659- ),
668+ if (willOverflowAction)
669+ Padding (
670+ padding: const EdgeInsets .only (bottom: _singleLineVerticalPadding),
671+ child: Row (mainAxisAlignment: MainAxisAlignment .end, children: maybeActionAndIcon),
672+ ),
673+ ],
674+ ),
660675 );
661676
662677 if (! isFloatingSnackBar) {
@@ -820,6 +835,9 @@ class _SnackbarDefaultsM2 extends SnackBarThemeData {
820835
821836 @override
822837 Color get closeIconColor => _colors.onSurface;
838+
839+ @override
840+ double get actionOverflowThreshold => 0.25 ;
823841}
824842
825843// BEGIN GENERATED TOKEN PROPERTIES - Snackbar
@@ -884,6 +902,12 @@ class _SnackbarDefaultsM3 extends SnackBarThemeData {
884902
885903 @override
886904 bool get showCloseIcon => false ;
905+
906+ @override
907+ Color ? get closeIconColor => _colors.onInverseSurface;
908+
909+ @override
910+ double get actionOverflowThreshold => 0.25 ;
887911}
888912
889913// END GENERATED TOKEN PROPERTIES - Snackbar
0 commit comments