diff --git a/packages/flutter/lib/src/material/button_style.dart b/packages/flutter/lib/src/material/button_style.dart index 413e19949a..414a65a783 100644 --- a/packages/flutter/lib/src/material/button_style.dart +++ b/packages/flutter/lib/src/material/button_style.dart @@ -107,6 +107,7 @@ class ButtonStyle with Diagnosticable { this.elevation, this.padding, this.minimumSize, + this.fixedSize, this.side, this.shape, this.mouseCursor, @@ -158,6 +159,16 @@ class ButtonStyle with Diagnosticable { /// per [tapTargetSize]. final MaterialStateProperty? minimumSize; + /// The button's size. + /// + /// This size is still constrained by the style's [minimumSize]. Fixed + /// size dimensions whose value is [double.infinity] are ignored. + /// + /// To specify buttons with a fixed width and the default height use + /// `fixedSize: Size.fromWidth(320)`. Similarly, to specify a fixed + /// height and the default width use `fixedSize: Size.fromHeight(100)`. + final MaterialStateProperty? fixedSize; + /// The color and weight of the button's outline. /// /// This value is combined with [shape] to create a shape decorated @@ -230,6 +241,7 @@ class ButtonStyle with Diagnosticable { MaterialStateProperty? elevation, MaterialStateProperty? padding, MaterialStateProperty? minimumSize, + MaterialStateProperty? fixedSize, MaterialStateProperty? side, MaterialStateProperty? shape, MaterialStateProperty? mouseCursor, @@ -248,6 +260,7 @@ class ButtonStyle with Diagnosticable { elevation: elevation ?? this.elevation, padding: padding ?? this.padding, minimumSize: minimumSize ?? this.minimumSize, + fixedSize: fixedSize ?? this.fixedSize, side: side ?? this.side, shape: shape ?? this.shape, mouseCursor: mouseCursor ?? this.mouseCursor, @@ -276,6 +289,7 @@ class ButtonStyle with Diagnosticable { elevation: elevation ?? style.elevation, padding: padding ?? style.padding, minimumSize: minimumSize ?? style.minimumSize, + fixedSize: fixedSize ?? style.fixedSize, side: side ?? style.side, shape: shape ?? style.shape, mouseCursor: mouseCursor ?? style.mouseCursor, @@ -298,6 +312,7 @@ class ButtonStyle with Diagnosticable { elevation, padding, minimumSize, + fixedSize, side, shape, mouseCursor, @@ -324,6 +339,7 @@ class ButtonStyle with Diagnosticable { && other.elevation == elevation && other.padding == padding && other.minimumSize == minimumSize + && other.fixedSize == fixedSize && other.side == side && other.shape == shape && other.mouseCursor == mouseCursor @@ -345,6 +361,7 @@ class ButtonStyle with Diagnosticable { properties.add(DiagnosticsProperty>('elevation', elevation, defaultValue: null)); properties.add(DiagnosticsProperty>('padding', padding, defaultValue: null)); properties.add(DiagnosticsProperty>('minimumSize', minimumSize, defaultValue: null)); + properties.add(DiagnosticsProperty>('fixedSize', fixedSize, defaultValue: null)); properties.add(DiagnosticsProperty>('side', side, defaultValue: null)); properties.add(DiagnosticsProperty>('shape', shape, defaultValue: null)); properties.add(DiagnosticsProperty>('mouseCursor', mouseCursor, defaultValue: null)); @@ -369,6 +386,7 @@ class ButtonStyle with Diagnosticable { elevation: _lerpProperties(a?.elevation, b?.elevation, t, lerpDouble), padding: _lerpProperties(a?.padding, b?.padding, t, EdgeInsetsGeometry.lerp), minimumSize: _lerpProperties(a?.minimumSize, b?.minimumSize, t, Size.lerp), + fixedSize: _lerpProperties(a?.fixedSize, b?.fixedSize, t, Size.lerp), side: _lerpSides(a?.side, b?.side, t), shape: _lerpShapes(a?.shape, b?.shape, t), mouseCursor: t < 0.5 ? a?.mouseCursor : b?.mouseCursor, diff --git a/packages/flutter/lib/src/material/button_style_button.dart b/packages/flutter/lib/src/material/button_style_button.dart index e24dcdac6e..2e3fcdc9ee 100644 --- a/packages/flutter/lib/src/material/button_style_button.dart +++ b/packages/flutter/lib/src/material/button_style_button.dart @@ -268,6 +268,7 @@ class _ButtonStyleState extends State with TickerProviderStat final Color? resolvedShadowColor = resolve((ButtonStyle? style) => style?.shadowColor); final EdgeInsetsGeometry? resolvedPadding = resolve((ButtonStyle? style) => style?.padding); final Size? resolvedMinimumSize = resolve((ButtonStyle? style) => style?.minimumSize); + final Size? resolvedFixedSize = resolve((ButtonStyle? style) => style?.fixedSize); final BorderSide? resolvedSide = resolve((ButtonStyle? style) => style?.side); final OutlinedBorder? resolvedShape = resolve((ButtonStyle? style) => style?.shape); @@ -285,12 +286,29 @@ class _ButtonStyleState extends State with TickerProviderStat final bool? resolvedEnableFeedback = effectiveValue((ButtonStyle? style) => style?.enableFeedback); final AlignmentGeometry? resolvedAlignment = effectiveValue((ButtonStyle? style) => style?.alignment); final Offset densityAdjustment = resolvedVisualDensity!.baseSizeAdjustment; - final BoxConstraints effectiveConstraints = resolvedVisualDensity.effectiveConstraints( + + BoxConstraints effectiveConstraints = resolvedVisualDensity.effectiveConstraints( BoxConstraints( minWidth: resolvedMinimumSize!.width, minHeight: resolvedMinimumSize.height, ), ); + if (resolvedFixedSize != null) { + final Size size = effectiveConstraints.constrain(resolvedFixedSize); + if (size.width.isFinite) { + effectiveConstraints = effectiveConstraints.copyWith( + minWidth: size.width, + maxWidth: size.width, + ); + } + if (size.height.isFinite) { + effectiveConstraints = effectiveConstraints.copyWith( + minHeight: size.height, + maxHeight: size.height + ); + } + } + final EdgeInsetsGeometry padding = resolvedPadding!.add( EdgeInsets.only( left: densityAdjustment.dx, diff --git a/packages/flutter/lib/src/material/elevated_button.dart b/packages/flutter/lib/src/material/elevated_button.dart index 22fadc9b19..cb4a9a2bc1 100644 --- a/packages/flutter/lib/src/material/elevated_button.dart +++ b/packages/flutter/lib/src/material/elevated_button.dart @@ -139,6 +139,7 @@ class ElevatedButton extends ButtonStyleButton { TextStyle? textStyle, EdgeInsetsGeometry? padding, Size? minimumSize, + Size? fixedSize, BorderSide? side, OutlinedBorder? shape, MouseCursor? enabledMouseCursor, @@ -174,6 +175,7 @@ class ElevatedButton extends ButtonStyleButton { elevation: elevationValue, padding: ButtonStyleButton.allOrNull(padding), minimumSize: ButtonStyleButton.allOrNull(minimumSize), + fixedSize: ButtonStyleButton.allOrNull(fixedSize), side: ButtonStyleButton.allOrNull(side), shape: ButtonStyleButton.allOrNull(shape), mouseCursor: mouseCursor, @@ -231,6 +233,7 @@ class ElevatedButton extends ButtonStyleButton { /// * `2 < textScaleFactor <= 3` - lerp(horizontal(8), horizontal(4)) /// * `3 < textScaleFactor` - horizontal(4) /// * `minimumSize` - Size(64, 36) + /// * `fixedSize` - null /// * `side` - null /// * `shape` - RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)) /// * `mouseCursor` diff --git a/packages/flutter/lib/src/material/outlined_button.dart b/packages/flutter/lib/src/material/outlined_button.dart index 9c83f4db52..7c5851149c 100644 --- a/packages/flutter/lib/src/material/outlined_button.dart +++ b/packages/flutter/lib/src/material/outlined_button.dart @@ -131,6 +131,7 @@ class OutlinedButton extends ButtonStyleButton { TextStyle? textStyle, EdgeInsetsGeometry? padding, Size? minimumSize, + Size? fixedSize, BorderSide? side, OutlinedBorder? shape, MouseCursor? enabledMouseCursor, @@ -160,6 +161,7 @@ class OutlinedButton extends ButtonStyleButton { elevation: ButtonStyleButton.allOrNull(elevation), padding: ButtonStyleButton.allOrNull(padding), minimumSize: ButtonStyleButton.allOrNull(minimumSize), + fixedSize: ButtonStyleButton.allOrNull(fixedSize), side: ButtonStyleButton.allOrNull(side), shape: ButtonStyleButton.allOrNull(shape), mouseCursor: mouseCursor, @@ -210,6 +212,7 @@ class OutlinedButton extends ButtonStyleButton { /// * `2 < textScaleFactor <= 3` - lerp(horizontal(8), horizontal(4)) /// * `3 < textScaleFactor` - horizontal(4) /// * `minimumSize` - Size(64, 36) + /// * `fixedSize` - null /// * `side` - BorderSide(width: 1, color: Theme.colorScheme.onSurface(0.12)) /// * `shape` - RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)) /// * `mouseCursor` diff --git a/packages/flutter/lib/src/material/text_button.dart b/packages/flutter/lib/src/material/text_button.dart index 32c7bd0aac..a864639b47 100644 --- a/packages/flutter/lib/src/material/text_button.dart +++ b/packages/flutter/lib/src/material/text_button.dart @@ -137,6 +137,7 @@ class TextButton extends ButtonStyleButton { TextStyle? textStyle, EdgeInsetsGeometry? padding, Size? minimumSize, + Size? fixedSize, BorderSide? side, OutlinedBorder? shape, MouseCursor? enabledMouseCursor, @@ -166,6 +167,7 @@ class TextButton extends ButtonStyleButton { elevation: ButtonStyleButton.allOrNull(elevation), padding: ButtonStyleButton.allOrNull(padding), minimumSize: ButtonStyleButton.allOrNull(minimumSize), + fixedSize: ButtonStyleButton.allOrNull(fixedSize), side: ButtonStyleButton.allOrNull(side), shape: ButtonStyleButton.allOrNull(shape), mouseCursor: mouseCursor, @@ -219,6 +221,7 @@ class TextButton extends ButtonStyleButton { /// * `2 < textScaleFactor <= 3` - lerp(horizontal(8), horizontal(4)) /// * `3 < textScaleFactor` - horizontal(4) /// * `minimumSize` - Size(64, 36) + /// * `fixedSize` - null /// * `side` - null /// * `shape` - RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)) /// * `mouseCursor` diff --git a/packages/flutter/test/material/button_style_test.dart b/packages/flutter/test/material/button_style_test.dart index 57ccf4ef20..568ff4dfbf 100644 --- a/packages/flutter/test/material/button_style_test.dart +++ b/packages/flutter/test/material/button_style_test.dart @@ -24,6 +24,7 @@ void main() { expect(style.elevation, null); expect(style.padding, null); expect(style.minimumSize, null); + expect(style.fixedSize, null); expect(style.side, null); expect(style.shape, null); expect(style.mouseCursor, null); @@ -93,6 +94,7 @@ void main() { final MaterialStateProperty elevation = MaterialStateProperty.all(1); final MaterialStateProperty padding = MaterialStateProperty.all(const EdgeInsets.all(1)); final MaterialStateProperty minimumSize = MaterialStateProperty.all(const Size(1, 2)); + final MaterialStateProperty fixedSize = MaterialStateProperty.all(const Size(3, 4)); final MaterialStateProperty side = MaterialStateProperty.all(const BorderSide()); final MaterialStateProperty shape = MaterialStateProperty.all(const StadiumBorder()); final MaterialStateProperty mouseCursor = MaterialStateProperty.all(SystemMouseCursors.forbidden); @@ -109,6 +111,7 @@ void main() { elevation: elevation, padding: padding, minimumSize: minimumSize, + fixedSize: fixedSize, side: side, shape: shape, mouseCursor: mouseCursor, @@ -128,6 +131,7 @@ void main() { elevation: elevation, padding: padding, minimumSize: minimumSize, + fixedSize: fixedSize, side: side, shape: shape, mouseCursor: mouseCursor, diff --git a/packages/flutter/test/material/elevated_button_test.dart b/packages/flutter/test/material/elevated_button_test.dart index 3e094c738c..fb463d93bd 100644 --- a/packages/flutter/test/material/elevated_button_test.dart +++ b/packages/flutter/test/material/elevated_button_test.dart @@ -1039,6 +1039,39 @@ void main() { expect(find.byType(ElevatedButton), paints ..path(strokeWidth: 4) ..drrect(color: borderColor)); }); + + testWidgets('Fixed size ElevatedButtons', (WidgetTester tester) async { + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: Column( + mainAxisSize: MainAxisSize.min, + children: [ + ElevatedButton( + style: ElevatedButton.styleFrom(fixedSize: const Size(100, 100)), + onPressed: () {}, + child: const Text('100x100'), + ), + ElevatedButton( + style: ElevatedButton.styleFrom(fixedSize: const Size.fromWidth(200)), + onPressed: () {}, + child: const Text('200xh'), + ), + ElevatedButton( + style: ElevatedButton.styleFrom(fixedSize: const Size.fromHeight(200)), + onPressed: () {}, + child: const Text('wx200'), + ), + ], + ), + ), + ), + ); + + expect(tester.getSize(find.widgetWithText(ElevatedButton, '100x100')), const Size(100, 100)); + expect(tester.getSize(find.widgetWithText(ElevatedButton, '200xh')).width, 200); + expect(tester.getSize(find.widgetWithText(ElevatedButton, 'wx200')).height, 200); + }); } TextStyle _iconStyle(WidgetTester tester, IconData icon) { diff --git a/packages/flutter/test/material/outlined_button_test.dart b/packages/flutter/test/material/outlined_button_test.dart index ee68765aeb..3ca0282386 100644 --- a/packages/flutter/test/material/outlined_button_test.dart +++ b/packages/flutter/test/material/outlined_button_test.dart @@ -1215,6 +1215,39 @@ void main() { ); expect(paddingWidget.padding, const EdgeInsets.all(22)); }); + + testWidgets('Fixed size OutlinedButtons', (WidgetTester tester) async { + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: Column( + mainAxisSize: MainAxisSize.min, + children: [ + OutlinedButton( + style: OutlinedButton.styleFrom(fixedSize: const Size(100, 100)), + onPressed: () {}, + child: const Text('100x100'), + ), + OutlinedButton( + style: OutlinedButton.styleFrom(fixedSize: const Size.fromWidth(200)), + onPressed: () {}, + child: const Text('200xh'), + ), + OutlinedButton( + style: OutlinedButton.styleFrom(fixedSize: const Size.fromHeight(200)), + onPressed: () {}, + child: const Text('wx200'), + ), + ], + ), + ), + ), + ); + + expect(tester.getSize(find.widgetWithText(OutlinedButton, '100x100')), const Size(100, 100)); + expect(tester.getSize(find.widgetWithText(OutlinedButton, '200xh')).width, 200); + expect(tester.getSize(find.widgetWithText(OutlinedButton, 'wx200')).height, 200); + }); } PhysicalModelLayer _findPhysicalLayer(Element element) { diff --git a/packages/flutter/test/material/text_button_test.dart b/packages/flutter/test/material/text_button_test.dart index ab709f1db8..e81e873d2e 100644 --- a/packages/flutter/test/material/text_button_test.dart +++ b/packages/flutter/test/material/text_button_test.dart @@ -1012,8 +1012,39 @@ void main() { expect(paddingWidget.padding, const EdgeInsets.all(22)); }); -} + testWidgets('Fixed size TextButtons', (WidgetTester tester) async { + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: Column( + mainAxisSize: MainAxisSize.min, + children: [ + TextButton( + style: TextButton.styleFrom(fixedSize: const Size(100, 100)), + onPressed: () {}, + child: const Text('100x100'), + ), + TextButton( + style: TextButton.styleFrom(fixedSize: const Size.fromWidth(200)), + onPressed: () {}, + child: const Text('200xh'), + ), + TextButton( + style: TextButton.styleFrom(fixedSize: const Size.fromHeight(200)), + onPressed: () {}, + child: const Text('wx200'), + ), + ], + ), + ), + ), + ); + expect(tester.getSize(find.widgetWithText(TextButton, '100x100')), const Size(100, 100)); + expect(tester.getSize(find.widgetWithText(TextButton, '200xh')).width, 200); + expect(tester.getSize(find.widgetWithText(TextButton, 'wx200')).height, 200); + }); +} TextStyle? _iconStyle(WidgetTester tester, IconData icon) { final RichText iconRichText = tester.widget(