diff --git a/dev/integration_tests/flutter_gallery/lib/demo/material/cards_demo.dart b/dev/integration_tests/flutter_gallery/lib/demo/material/cards_demo.dart index 268fe012f4..8746949cf9 100644 --- a/dev/integration_tests/flutter_gallery/lib/demo/material/cards_demo.dart +++ b/dev/integration_tests/flutter_gallery/lib/demo/material/cards_demo.dart @@ -247,7 +247,7 @@ class TravelDestinationContent extends StatelessWidget { final ThemeData theme = Theme.of(context); final TextStyle titleStyle = theme.textTheme.headline5!.copyWith(color: Colors.white); final TextStyle descriptionStyle = theme.textTheme.subtitle1!; - final ButtonStyle textButtonStyle = TextButton.styleFrom(primary: Colors.amber.shade500); + final ButtonStyle textButtonStyle = TextButton.styleFrom(foregroundColor: Colors.amber.shade500); return Column( crossAxisAlignment: CrossAxisAlignment.start, diff --git a/dev/integration_tests/flutter_gallery/lib/demo/shrine/shopping_cart.dart b/dev/integration_tests/flutter_gallery/lib/demo/shrine/shopping_cart.dart index 8547ddd55a..56aeb0a109 100644 --- a/dev/integration_tests/flutter_gallery/lib/demo/shrine/shopping_cart.dart +++ b/dev/integration_tests/flutter_gallery/lib/demo/shrine/shopping_cart.dart @@ -78,7 +78,7 @@ class _ShoppingCartPageState extends State { right: 16.0, child: ElevatedButton( style: ElevatedButton.styleFrom( - primary: kShrinePink100, + backgroundColor: kShrinePink100, shape: const BeveledRectangleBorder( borderRadius: BorderRadius.all(Radius.circular(7.0)), ), diff --git a/dev/integration_tests/flutter_gallery/lib/gallery/options.dart b/dev/integration_tests/flutter_gallery/lib/gallery/options.dart index 5cd81d17fc..5e1530083a 100644 --- a/dev/integration_tests/flutter_gallery/lib/gallery/options.dart +++ b/dev/integration_tests/flutter_gallery/lib/gallery/options.dart @@ -177,7 +177,7 @@ class _TextButton extends StatelessWidget { final ThemeData theme = Theme.of(context); return TextButton( style: TextButton.styleFrom( - primary: theme.colorScheme.onPrimary, + foregroundColor: theme.colorScheme.onPrimary, textStyle: theme.textTheme.subtitle1, padding: EdgeInsets.zero, ), diff --git a/dev/manual_tests/lib/density.dart b/dev/manual_tests/lib/density.dart index c4307b80e5..914c2d723d 100644 --- a/dev/manual_tests/lib/density.dart +++ b/dev/manual_tests/lib/density.dart @@ -534,7 +534,7 @@ class _MyHomePageState extends State { label: _model.rtl ? 'زر مسطح' : 'Text Button', child: TextButton( style: TextButton.styleFrom( - primary: Colors.white, + foregroundColor: Colors.white, backgroundColor: m2Swatch[200] ), onPressed: _model.enable ? () {} : null, diff --git a/dev/manual_tests/lib/text.dart b/dev/manual_tests/lib/text.dart index b6a00d89e0..8fe670829c 100644 --- a/dev/manual_tests/lib/text.dart +++ b/dev/manual_tests/lib/text.dart @@ -44,7 +44,7 @@ class _HomeState extends State { children: [ TextButton( style: TextButton.styleFrom( - primary: Colors.white, + foregroundColor: Colors.white, backgroundColor: Colors.red.shade800, ), onPressed: () { Navigator.pushNamed(context, 'underlines'); }, @@ -52,7 +52,7 @@ class _HomeState extends State { ), TextButton( style: TextButton.styleFrom( - primary: Colors.white, + foregroundColor: Colors.white, backgroundColor: Colors.orange.shade700, ), onPressed: () { Navigator.pushNamed(context, 'fallback'); }, @@ -60,7 +60,7 @@ class _HomeState extends State { ), TextButton( style: TextButton.styleFrom( - primary: Colors.black, + foregroundColor: Colors.black, backgroundColor: Colors.yellow.shade700, ), onPressed: () { Navigator.pushNamed(context, 'bidi'); }, @@ -68,7 +68,7 @@ class _HomeState extends State { ), TextButton( style: TextButton.styleFrom( - primary: Colors.black, + foregroundColor: Colors.black, backgroundColor: Colors.green.shade400, ), onPressed: () { Navigator.pushNamed(context, 'fuzzer'); }, @@ -76,7 +76,7 @@ class _HomeState extends State { ), TextButton( style: TextButton.styleFrom( - primary: Colors.white, + foregroundColor: Colors.white, backgroundColor: Colors.blue.shade400, ), onPressed: () { Navigator.pushNamed(context, 'zalgo'); }, @@ -84,7 +84,7 @@ class _HomeState extends State { ), TextButton( style: TextButton.styleFrom( - primary: Colors.black, + foregroundColor: Colors.black, backgroundColor: Colors.purple.shade200, ), onPressed: () { Navigator.pushNamed(context, 'painting'); }, @@ -618,7 +618,7 @@ class _UnderlinesState extends State { ), TextButton( style: TextButton.styleFrom( - primary: Colors.white, + foregroundColor: Colors.white, backgroundColor: Colors.red, ), onPressed: _text == '' ? null : () { diff --git a/examples/api/lib/material/app_bar/app_bar.2.dart b/examples/api/lib/material/app_bar/app_bar.2.dart index 58a4f236cf..01a7b63be8 100644 --- a/examples/api/lib/material/app_bar/app_bar.2.dart +++ b/examples/api/lib/material/app_bar/app_bar.2.dart @@ -25,7 +25,7 @@ class AppBarExample extends StatelessWidget { @override Widget build(BuildContext context) { final ButtonStyle style = TextButton.styleFrom( - primary: Theme.of(context).colorScheme.onPrimary, + foregroundColor: Theme.of(context).colorScheme.onPrimary, ); return Scaffold( appBar: AppBar( diff --git a/examples/api/lib/material/button_style/button_style.0.dart b/examples/api/lib/material/button_style/button_style.0.dart index 571b33952a..7f40de2cc0 100644 --- a/examples/api/lib/material/button_style/button_style.0.dart +++ b/examples/api/lib/material/button_style/button_style.0.dart @@ -63,10 +63,8 @@ class ButtonTypesGroup extends StatelessWidget { // 'Filled' type. ElevatedButton( style: ElevatedButton.styleFrom( - // Foreground color - onPrimary: Theme.of(context).colorScheme.onPrimary, - // Background color - primary: Theme.of(context).colorScheme.primary, + foregroundColor: Theme.of(context).colorScheme.onPrimary, + backgroundColor: Theme.of(context).colorScheme.primary, ).copyWith(elevation: ButtonStyleButton.allOrNull(0.0)), onPressed: onPressed, child: const Text('Filled'), @@ -76,10 +74,8 @@ class ButtonTypesGroup extends StatelessWidget { // 'Filled Tonal' type. ElevatedButton( style: ElevatedButton.styleFrom( - // Foreground color - onPrimary: Theme.of(context).colorScheme.onSecondaryContainer, - // Background color - primary: Theme.of(context).colorScheme.secondaryContainer, + foregroundColor: Theme.of(context).colorScheme.onSecondaryContainer, + backgroundColor: Theme.of(context).colorScheme.secondaryContainer, ).copyWith(elevation: ButtonStyleButton.allOrNull(0.0)), onPressed: onPressed, child: const Text('Filled Tonal'), diff --git a/examples/api/lib/material/navigation_bar/navigation_bar.0.dart b/examples/api/lib/material/navigation_bar/navigation_bar.0.dart index 12506fc1d4..865ce7984e 100644 --- a/examples/api/lib/material/navigation_bar/navigation_bar.0.dart +++ b/examples/api/lib/material/navigation_bar/navigation_bar.0.dart @@ -35,7 +35,7 @@ class RootPage extends StatelessWidget { Widget build(BuildContext context) { final TextStyle headline5 = Theme.of(context).textTheme.headline5!; final ButtonStyle buttonStyle = ElevatedButton.styleFrom( - primary: destination.color, + backgroundColor: destination.color, visualDensity: VisualDensity.comfortable, padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 16), textStyle: headline5, @@ -125,7 +125,7 @@ class ListPage extends StatelessWidget { Widget build(BuildContext context) { const int itemCount = 50; final ButtonStyle buttonStyle = OutlinedButton.styleFrom( - primary: destination.color, + foregroundColor: destination.color, fixedSize: const Size.fromHeight(128), textStyle: Theme.of(context).textTheme.headline5, ); diff --git a/examples/api/lib/material/text_button/text_button.0.dart b/examples/api/lib/material/text_button/text_button.0.dart index 82d9924d54..b59a1b0f17 100644 --- a/examples/api/lib/material/text_button/text_button.0.dart +++ b/examples/api/lib/material/text_button/text_button.0.dart @@ -69,8 +69,8 @@ class MyStatelessWidget extends StatelessWidget { ), TextButton( style: TextButton.styleFrom( + foregroundColor: Colors.white, padding: const EdgeInsets.all(16.0), - primary: Colors.white, textStyle: const TextStyle(fontSize: 20), ), onPressed: () {}, diff --git a/examples/api/lib/widgets/basic/absorb_pointer.0.dart b/examples/api/lib/widgets/basic/absorb_pointer.0.dart index 5e271808e5..03e6fb8553 100644 --- a/examples/api/lib/widgets/basic/absorb_pointer.0.dart +++ b/examples/api/lib/widgets/basic/absorb_pointer.0.dart @@ -49,7 +49,7 @@ class MyStatelessWidget extends StatelessWidget { child: AbsorbPointer( child: ElevatedButton( style: ElevatedButton.styleFrom( - primary: Colors.blue.shade200, + backgroundColor: Colors.blue.shade200, ), onPressed: () {}, child: null, diff --git a/packages/flutter/lib/fix_data.yaml b/packages/flutter/lib/fix_data.yaml index cee2607eb0..be4a44a03d 100644 --- a/packages/flutter/lib/fix_data.yaml +++ b/packages/flutter/lib/fix_data.yaml @@ -17,6 +17,167 @@ version: 1 transforms: + # Changes made in https://github.com/flutter/flutter/pull/105291 + - title: "Migrate 'ElevatedButton.styleFrom(primary)' to 'ElevatedButton.styleFrom(backgroundColor)'" + date: 2022-05-27 + element: + uris: [ 'material.dart' ] + method: 'styleFrom' + inClass: 'ElevatedButton' + changes: + - kind: 'addParameter' + index: 1 + name: 'backgroundColor' + style: optional_named + argumentValue: + expression: '{% primary %}' + requiredIf: "primary != ''" + - kind: 'removeParameter' + name: 'primary' + variables: + primary: + kind: 'fragment' + value: 'arguments[primary]' + + # Changes made in https://github.com/flutter/flutter/pull/105291 + - title: "Migrate 'ElevatedButton.styleFrom(onPrimary)' to 'ElevatedButton.styleFrom(foregroundColor)'" + date: 2022-05-27 + element: + uris: [ 'material.dart' ] + method: 'styleFrom' + inClass: 'ElevatedButton' + changes: + - kind: 'addParameter' + index: 0 + name: 'foregroundColor' + style: optional_named + argumentValue: + expression: '{% onPrimary %}' + requiredIf: "onPrimary != ''" + - kind: 'removeParameter' + name: 'onPrimary' + variables: + onPrimary: + kind: 'fragment' + value: 'arguments[onPrimary]' + + # Changes made in https://github.com/flutter/flutter/pull/105291 + - title: "Migrate 'ElevatedButton.styleFrom(onSurface)' to 'ElevatedButton.styleFrom(disabledForegroundColor)'" + date: 2022-05-27 + element: + uris: [ 'material.dart' ] + method: 'styleFrom' + inClass: 'ElevatedButton' + changes: + - kind: 'addParameter' + index: 2 + name: 'disabledForegroundColor' + style: optional_named + argumentValue: + expression: '{% onSurface %}.withOpacity(0.38)' + requiredIf: "onSurface != ''" + - kind: 'addParameter' + index: 3 + name: 'disabledBackgroundColor' + style: optional_named + argumentValue: + expression: '{% onSurface %}.withOpacity(0.12)' + requiredIf: "onSurface != ''" + - kind: 'removeParameter' + name: 'onSurface' + variables: + onSurface: + kind: 'fragment' + value: 'arguments[onSurface]' + + # Changes made in https://github.com/flutter/flutter/pull/105291 + - title: "Migrate 'OutlinedButton.styleFrom(primary)' to 'OutlinedButton.styleFrom(foregroundColor)'" + date: 2022-05-27 + element: + uris: [ 'material.dart' ] + method: 'styleFrom' + inClass: 'OutlinedButton' + changes: + - kind: 'addParameter' + index: 0 + name: 'foregroundColor' + style: optional_named + argumentValue: + expression: '{% primary %}' + requiredIf: "primary != ''" + - kind: 'removeParameter' + name: 'primary' + variables: + primary: + kind: 'fragment' + value: 'arguments[primary]' + + # Changes made in https://github.com/flutter/flutter/pull/105291 + - title: "Migrate 'OutlinedButton.styleFrom(onSurface)' to 'OutlinedButton.styleFrom(disabledForeground)'" + date: 2022-05-27 + element: + uris: [ 'material.dart' ] + method: 'styleFrom' + inClass: 'OutlinedButton' + changes: + - kind: 'addParameter' + index: 2 + name: 'disabledForegroundColor' + style: optional_named + argumentValue: + expression: '{% onSurface %}.withOpacity(0.38)' + requiredIf: "onSurface != ''" + - kind: 'removeParameter' + name: 'onSurface' + variables: + onSurface: + kind: 'fragment' + value: 'arguments[onSurface]' + + # Changes made in https://github.com/flutter/flutter/pull/105291 + - title: "Migrate 'TextButton.styleFrom(primary)' to 'TextButton.styleFrom(foregroundColor)'" + date: 2022-05-27 + element: + uris: [ 'material.dart' ] + method: 'styleFrom' + inClass: 'TextButton' + changes: + - kind: 'addParameter' + index: 0 + name: 'foregroundColor' + style: optional_named + argumentValue: + expression: '{% primary %}' + requiredIf: "primary != ''" + - kind: 'removeParameter' + name: 'primary' + variables: + primary: + kind: 'fragment' + value: 'arguments[primary]' + + # Changes made in https://github.com/flutter/flutter/pull/105291 + - title: "Migrate 'TextButton.styleFrom(onSurface)' to 'TextButton.styleFrom(disabledForeground)'" + date: 2022-05-27 + element: + uris: [ 'material.dart' ] + method: 'styleFrom' + inClass: 'TextButton' + changes: + - kind: 'addParameter' + index: 2 + name: 'disabledForegroundColor' + style: optional_named + argumentValue: + expression: '{% onSurface %}.withOpacity(0.38)' + requiredIf: "onSurface != ''" + - kind: 'removeParameter' + name: 'onSurface' + variables: + onSurface: + kind: 'fragment' + value: 'arguments[onSurface]' + # Changes made in https://github.com/flutter/flutter/pull/100381 - title: "Migrate 'TextSelectionOverlay.fadeDuration' to SelectionOverlay.fadeDuration" date: 2022-03-18 diff --git a/packages/flutter/lib/src/material/elevated_button.dart b/packages/flutter/lib/src/material/elevated_button.dart index 5541f391c3..bbd81b61aa 100644 --- a/packages/flutter/lib/src/material/elevated_button.dart +++ b/packages/flutter/lib/src/material/elevated_button.dart @@ -135,9 +135,10 @@ class ElevatedButton extends ButtonStyleButton { /// ) /// ``` static ButtonStyle styleFrom({ - Color? primary, - Color? onPrimary, - Color? onSurface, + Color? foregroundColor, + Color? backgroundColor, + Color? disabledForegroundColor, + Color? disabledBackgroundColor, Color? shadowColor, Color? surfaceTintColor, double? elevation, @@ -156,16 +157,35 @@ class ElevatedButton extends ButtonStyleButton { bool? enableFeedback, AlignmentGeometry? alignment, InteractiveInkFeatureFactory? splashFactory, + @Deprecated( + 'Use backgroundColor instead. ' + 'This feature was deprecated after v3.1.0.' + ) + Color? primary, + @Deprecated( + 'Use foregroundColor instead. ' + 'This feature was deprecated after v3.1.0.' + ) + Color? onPrimary, + @Deprecated( + 'Use disabledForegroundColor and disabledBackgroundColor instead. ' + 'This feature was deprecated after v3.1.0.' + ) + Color? onSurface, }) { - final MaterialStateProperty? backgroundColor = (onSurface == null && primary == null) + final Color? background = backgroundColor ?? primary; + final Color? disabledBackground = disabledBackgroundColor ?? onSurface?.withOpacity(0.12); + final MaterialStateProperty? backgroundColorProp = (background == null && disabledBackground == null) ? null - : _ElevatedButtonDefaultBackground(primary, onSurface); - final MaterialStateProperty? foregroundColor = (onSurface == null && onPrimary == null) + : _ElevatedButtonDefaultColor(background, disabledBackground); + final Color? foreground = foregroundColor ?? onPrimary; + final Color? disabledForeground = disabledForegroundColor ?? onSurface?.withOpacity(0.38); + final MaterialStateProperty? foregroundColorProp = (foreground == null && disabledForeground == null) ? null - : _ElevatedButtonDefaultForeground(onPrimary, onSurface); - final MaterialStateProperty? overlayColor = (onPrimary == null) + : _ElevatedButtonDefaultColor(foreground, disabledForeground); + final MaterialStateProperty? overlayColor = (foreground == null) ? null - : _ElevatedButtonDefaultOverlay(onPrimary); + : _ElevatedButtonDefaultOverlay(foreground); final MaterialStateProperty? elevationValue = (elevation == null) ? null : _ElevatedButtonDefaultElevation(elevation); @@ -175,8 +195,8 @@ class ElevatedButton extends ButtonStyleButton { return ButtonStyle( textStyle: MaterialStatePropertyAll(textStyle), - backgroundColor: backgroundColor, - foregroundColor: foregroundColor, + backgroundColor: backgroundColorProp, + foregroundColor: foregroundColorProp, overlayColor: overlayColor, shadowColor: ButtonStyleButton.allOrNull(shadowColor), surfaceTintColor: ButtonStyleButton.allOrNull(surfaceTintColor), @@ -321,9 +341,10 @@ class ElevatedButton extends ButtonStyleButton { return Theme.of(context).useMaterial3 ? _TokenDefaultsM3(context) : styleFrom( - primary: colorScheme.primary, - onPrimary: colorScheme.onPrimary, - onSurface: colorScheme.onSurface, + backgroundColor: colorScheme.primary, + foregroundColor: colorScheme.onPrimary, + disabledBackgroundColor: colorScheme.onSurface.withOpacity(0.12), + disabledForegroundColor: colorScheme.onSurface.withOpacity(0.38), shadowColor: theme.shadowColor, elevation: 2, textStyle: theme.textTheme.button, @@ -360,50 +381,34 @@ EdgeInsetsGeometry _scaledPadding(BuildContext context) { } @immutable -class _ElevatedButtonDefaultBackground extends MaterialStateProperty with Diagnosticable { - _ElevatedButtonDefaultBackground(this.primary, this.onSurface); +class _ElevatedButtonDefaultColor extends MaterialStateProperty with Diagnosticable { + _ElevatedButtonDefaultColor(this.color, this.disabled); - final Color? primary; - final Color? onSurface; + final Color? color; + final Color? disabled; @override Color? resolve(Set states) { if (states.contains(MaterialState.disabled)) { - return onSurface?.withOpacity(0.12); + return disabled; } - return primary; - } -} - -@immutable -class _ElevatedButtonDefaultForeground extends MaterialStateProperty with Diagnosticable { - _ElevatedButtonDefaultForeground(this.onPrimary, this.onSurface); - - final Color? onPrimary; - final Color? onSurface; - - @override - Color? resolve(Set states) { - if (states.contains(MaterialState.disabled)) { - return onSurface?.withOpacity(0.38); - } - return onPrimary; + return color; } } @immutable class _ElevatedButtonDefaultOverlay extends MaterialStateProperty with Diagnosticable { - _ElevatedButtonDefaultOverlay(this.onPrimary); + _ElevatedButtonDefaultOverlay(this.overlay); - final Color onPrimary; + final Color overlay; @override Color? resolve(Set states) { if (states.contains(MaterialState.hovered)) { - return onPrimary.withOpacity(0.08); + return overlay.withOpacity(0.08); } if (states.contains(MaterialState.focused) || states.contains(MaterialState.pressed)) { - return onPrimary.withOpacity(0.24); + return overlay.withOpacity(0.24); } return null; } diff --git a/packages/flutter/lib/src/material/outlined_button.dart b/packages/flutter/lib/src/material/outlined_button.dart index e74511fd9e..3bd32873e1 100644 --- a/packages/flutter/lib/src/material/outlined_button.dart +++ b/packages/flutter/lib/src/material/outlined_button.dart @@ -133,9 +133,10 @@ class OutlinedButton extends ButtonStyleButton { /// ) /// ``` static ButtonStyle styleFrom({ - Color? primary, - Color? onSurface, + Color? foregroundColor, Color? backgroundColor, + Color? disabledForegroundColor, + Color? disabledBackgroundColor, Color? shadowColor, Color? surfaceTintColor, double? elevation, @@ -154,21 +155,38 @@ class OutlinedButton extends ButtonStyleButton { bool? enableFeedback, AlignmentGeometry? alignment, InteractiveInkFeatureFactory? splashFactory, + @Deprecated( + 'Use foregroundColor instead. ' + 'This feature was deprecated after v3.1.0.' + ) + Color? primary, + @Deprecated( + 'Use disabledForegroundColor and disabledForegroundColor instead. ' + 'This feature was deprecated after v3.1.0.' + ) + Color? onSurface, }) { - final MaterialStateProperty? foregroundColor = (onSurface == null && primary == null) + final Color? foreground = foregroundColor ?? primary; + final Color? disabledForeground = disabledForegroundColor ?? onSurface?.withOpacity(0.38); + final MaterialStateProperty? foregroundColorProp = (foreground == null && disabledForeground == null) ? null - : _OutlinedButtonDefaultForeground(primary, onSurface); - final MaterialStateProperty? overlayColor = (primary == null) + : _OutlinedButtonDefaultColor(foreground, disabledForeground); + final MaterialStateProperty? backgroundColorProp = (backgroundColor == null && disabledBackgroundColor == null) ? null - : _OutlinedButtonDefaultOverlay(primary); + : disabledBackgroundColor == null + ? ButtonStyleButton.allOrNull(backgroundColor) + : _OutlinedButtonDefaultColor(backgroundColor, disabledBackgroundColor); + final MaterialStateProperty? overlayColor = (foreground == null) + ? null + : _OutlinedButtonDefaultOverlay(foreground); final MaterialStateProperty? mouseCursor = (enabledMouseCursor == null && disabledMouseCursor == null) ? null : _OutlinedButtonDefaultMouseCursor(enabledMouseCursor!, disabledMouseCursor!); return ButtonStyle( textStyle: ButtonStyleButton.allOrNull(textStyle), - foregroundColor: foregroundColor, - backgroundColor: ButtonStyleButton.allOrNull(backgroundColor), + foregroundColor: foregroundColorProp, + backgroundColor: backgroundColorProp, overlayColor: overlayColor, shadowColor: ButtonStyleButton.allOrNull(shadowColor), surfaceTintColor: ButtonStyleButton.allOrNull(surfaceTintColor), @@ -290,9 +308,10 @@ class OutlinedButton extends ButtonStyleButton { return Theme.of(context).useMaterial3 ? _TokenDefaultsM3(context) : styleFrom( - primary: colorScheme.primary, - onSurface: colorScheme.onSurface, + foregroundColor: colorScheme.primary, + disabledForegroundColor: colorScheme.onSurface.withOpacity(0.38), backgroundColor: Colors.transparent, + disabledBackgroundColor: Colors.transparent, shadowColor: theme.shadowColor, elevation: 0, textStyle: theme.textTheme.button, @@ -330,34 +349,34 @@ EdgeInsetsGeometry _scaledPadding(BuildContext context) { } @immutable -class _OutlinedButtonDefaultForeground extends MaterialStateProperty with Diagnosticable { - _OutlinedButtonDefaultForeground(this.primary, this.onSurface); +class _OutlinedButtonDefaultColor extends MaterialStateProperty with Diagnosticable { + _OutlinedButtonDefaultColor(this.color, this.disabled); - final Color? primary; - final Color? onSurface; + final Color? color; + final Color? disabled; @override Color? resolve(Set states) { if (states.contains(MaterialState.disabled)) { - return onSurface?.withOpacity(0.38); + return disabled; } - return primary; + return color; } } @immutable class _OutlinedButtonDefaultOverlay extends MaterialStateProperty with Diagnosticable { - _OutlinedButtonDefaultOverlay(this.primary); + _OutlinedButtonDefaultOverlay(this.foreground); - final Color primary; + final Color foreground; @override Color? resolve(Set states) { if (states.contains(MaterialState.hovered)) { - return primary.withOpacity(0.04); + return foreground.withOpacity(0.04); } if (states.contains(MaterialState.focused) || states.contains(MaterialState.pressed)) { - return primary.withOpacity(0.12); + return foreground.withOpacity(0.12); } return null; } diff --git a/packages/flutter/lib/src/material/text_button.dart b/packages/flutter/lib/src/material/text_button.dart index c2da28a867..a6d1bdaf73 100644 --- a/packages/flutter/lib/src/material/text_button.dart +++ b/packages/flutter/lib/src/material/text_button.dart @@ -141,9 +141,10 @@ class TextButton extends ButtonStyleButton { /// ) /// ``` static ButtonStyle styleFrom({ - Color? primary, - Color? onSurface, + Color? foregroundColor, Color? backgroundColor, + Color? disabledForegroundColor, + Color? disabledBackgroundColor, Color? shadowColor, Color? surfaceTintColor, double? elevation, @@ -162,21 +163,38 @@ class TextButton extends ButtonStyleButton { bool? enableFeedback, AlignmentGeometry? alignment, InteractiveInkFeatureFactory? splashFactory, + @Deprecated( + 'Use foregroundColor instead. ' + 'This feature was deprecated after v3.1.0.' + ) + Color? primary, + @Deprecated( + 'Use disabledForegroundColor and disabledForegroundColor instead. ' + 'This feature was deprecated after v3.1.0.' + ) + Color? onSurface, }) { - final MaterialStateProperty? foregroundColor = (onSurface == null && primary == null) + final Color? foreground = foregroundColor ?? primary; + final Color? disabledForeground = disabledForegroundColor ?? onSurface?.withOpacity(0.38); + final MaterialStateProperty? foregroundColorProp = (foreground == null && disabledForeground == null) ? null - : _TextButtonDefaultForeground(primary, onSurface); - final MaterialStateProperty? overlayColor = (primary == null) + : _TextButtonDefaultColor(foreground, disabledForeground); + final MaterialStateProperty? backgroundColorProp = (backgroundColor == null && disabledBackgroundColor == null) ? null - : _TextButtonDefaultOverlay(primary); + : disabledBackgroundColor == null + ? ButtonStyleButton.allOrNull(backgroundColor) + : _TextButtonDefaultColor(backgroundColor, disabledBackgroundColor); + final MaterialStateProperty? overlayColor = (foreground == null) + ? null + : _TextButtonDefaultOverlay(foreground); final MaterialStateProperty? mouseCursor = (enabledMouseCursor == null && disabledMouseCursor == null) ? null : _TextButtonDefaultMouseCursor(enabledMouseCursor!, disabledMouseCursor!); return ButtonStyle( textStyle: ButtonStyleButton.allOrNull(textStyle), - backgroundColor: ButtonStyleButton.allOrNull(backgroundColor), - foregroundColor: foregroundColor, + foregroundColor: foregroundColorProp, + backgroundColor: backgroundColorProp, overlayColor: overlayColor, shadowColor: ButtonStyleButton.allOrNull(shadowColor), surfaceTintColor: ButtonStyleButton.allOrNull(surfaceTintColor), @@ -311,9 +329,10 @@ class TextButton extends ButtonStyleButton { return Theme.of(context).useMaterial3 ? _TokenDefaultsM3(context) : styleFrom( - primary: colorScheme.primary, - onSurface: colorScheme.onSurface, + foregroundColor: colorScheme.primary, + disabledForegroundColor: colorScheme.onSurface.withOpacity(0.38), backgroundColor: Colors.transparent, + disabledBackgroundColor: Colors.transparent, shadowColor: theme.shadowColor, elevation: 0, textStyle: theme.textTheme.button, @@ -350,23 +369,23 @@ EdgeInsetsGeometry _scaledPadding(BuildContext context) { } @immutable -class _TextButtonDefaultForeground extends MaterialStateProperty { - _TextButtonDefaultForeground(this.primary, this.onSurface); +class _TextButtonDefaultColor extends MaterialStateProperty { + _TextButtonDefaultColor(this.color, this.disabled); - final Color? primary; - final Color? onSurface; + final Color? color; + final Color? disabled; @override Color? resolve(Set states) { if (states.contains(MaterialState.disabled)) { - return onSurface?.withOpacity(0.38); + return disabled; } - return primary; + return color; } @override String toString() { - return '{disabled: ${onSurface?.withOpacity(0.38)}, otherwise: $primary}'; + return '{disabled: $disabled, otherwise: $color}'; } } diff --git a/packages/flutter/test/material/elevated_button_test.dart b/packages/flutter/test/material/elevated_button_test.dart index 6a1d6ce022..0648bf4eb2 100644 --- a/packages/flutter/test/material/elevated_button_test.dart +++ b/packages/flutter/test/material/elevated_button_test.dart @@ -1600,6 +1600,30 @@ void main() { ); }); + testWidgets('ElevatedButton.styleFrom can be used to set foreground and background colors', (WidgetTester tester) async { + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: ElevatedButton( + style: ElevatedButton.styleFrom( + foregroundColor: Colors.white, + backgroundColor: Colors.purple, + ), + onPressed: () {}, + child: const Text('button'), + ), + ), + ), + ); + + final Material material = tester.widget(find.descendant( + of: find.byType(ElevatedButton), + matching: find.byType(Material), + )); + expect(material.color, Colors.purple); + expect(material.textStyle!.color, Colors.white); + }); + testWidgets('ElevatedButton statesController', (WidgetTester tester) async { int count = 0; void valueChanged() { diff --git a/packages/flutter/test/material/outlined_button_test.dart b/packages/flutter/test/material/outlined_button_test.dart index b1a78e0195..58063dc7d1 100644 --- a/packages/flutter/test/material/outlined_button_test.dart +++ b/packages/flutter/test/material/outlined_button_test.dart @@ -1718,6 +1718,30 @@ void main() { expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.basic); }); + testWidgets('OutlinedButton.styleFrom can be used to set foreground and background colors', (WidgetTester tester) async { + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: OutlinedButton( + style: OutlinedButton.styleFrom( + foregroundColor: Colors.white, + backgroundColor: Colors.purple, + ), + onPressed: () {}, + child: const Text('button'), + ), + ), + ), + ); + + final Material material = tester.widget(find.descendant( + of: find.byType(OutlinedButton), + matching: find.byType(Material), + )); + expect(material.color, Colors.purple); + expect(material.textStyle!.color, Colors.white); + }); + testWidgets('OutlinedButton statesController', (WidgetTester tester) async { int count = 0; void valueChanged() { diff --git a/packages/flutter/test/material/text_button_test.dart b/packages/flutter/test/material/text_button_test.dart index d950ad1733..cc4c67d85e 100644 --- a/packages/flutter/test/material/text_button_test.dart +++ b/packages/flutter/test/material/text_button_test.dart @@ -1525,6 +1525,30 @@ void main() { expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.basic); }); + testWidgets('TextButton.styleFrom can be used to set foreground and background colors', (WidgetTester tester) async { + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: TextButton( + style: TextButton.styleFrom( + foregroundColor: Colors.white, + backgroundColor: Colors.purple, + ), + onPressed: () {}, + child: const Text('button'), + ), + ), + ), + ); + + final Material material = tester.widget(find.descendant( + of: find.byType(TextButton), + matching: find.byType(Material), + )); + expect(material.color, Colors.purple); + expect(material.textStyle!.color, Colors.white); + }); + testWidgets('TextButton statesController', (WidgetTester tester) async { int count = 0; void valueChanged() { diff --git a/packages/flutter/test_fixes/material.dart b/packages/flutter/test_fixes/material.dart index 3a19e63961..f60f8b1693 100644 --- a/packages/flutter/test_fixes/material.dart +++ b/packages/flutter/test_fixes/material.dart @@ -554,4 +554,19 @@ void main() { // Change made in https://github.com/flutter/flutter/pull/100381 TextSelectionOverlay.fadeDuration; + + // Changes made in https://github.com/flutter/flutter/pull/105291 + ButtonStyle elevationButtonStyle = ElevatedButton.styleFrom( + primary: Colors.blue, + onPrimary: Colors.white, + onSurface: Colors.grey, + ); + ButtonStyle outlinedButtonStyle = OutlinedButton.styleFrom( + primary: Colors.blue, + onSurface: Colors.grey, + ); + ButtonStyle textButtonStyle = TextButton.styleFrom( + primary: Colors.blue, + onSurface: Colors.grey, + ); } diff --git a/packages/flutter/test_fixes/material.dart.expect b/packages/flutter/test_fixes/material.dart.expect index 9465999445..985c843b5d 100644 --- a/packages/flutter/test_fixes/material.dart.expect +++ b/packages/flutter/test_fixes/material.dart.expect @@ -527,4 +527,15 @@ void main() { // Change made in https://github.com/flutter/flutter/pull/100381 SelectionOverlay.fadeDuration; + + // Changes made in https://github.com/flutter/flutter/pull/105291 + ButtonStyle elevationButtonStyle = ElevatedButton.styleFrom( + foregroundColor: Colors.white, backgroundColor: Colors.blue, disabledForegroundColor: Colors.grey.withOpacity(0.38), disabledBackgroundColor: Colors.grey.withOpacity(0.12), + ); + ButtonStyle outlinedButtonStyle = OutlinedButton.styleFrom( + foregroundColor: Colors.blue, disabledForegroundColor: Colors.grey.withOpacity(0.38), + ); + ButtonStyle textButtonStyle = TextButton.styleFrom( + foregroundColor: Colors.blue, disabledForegroundColor: Colors.grey.withOpacity(0.38), + ); } diff --git a/packages/flutter_test/test/accessibility_test.dart b/packages/flutter_test/test/accessibility_test.dart index cadb26a526..c0dbb015cb 100644 --- a/packages/flutter_test/test/accessibility_test.dart +++ b/packages/flutter_test/test/accessibility_test.dart @@ -765,7 +765,7 @@ void main() { backgroundColor: Colors.white, body: ElevatedButton( style: ElevatedButton.styleFrom( - primary: const Color(0xFFFBBC04), + backgroundColor: const Color(0xFFFBBC04), elevation: 0, ), onPressed: () {}, diff --git a/packages/flutter_test/test/accessibility_window_test.dart b/packages/flutter_test/test/accessibility_window_test.dart index e84dd2ce46..57dac2122a 100644 --- a/packages/flutter_test/test/accessibility_window_test.dart +++ b/packages/flutter_test/test/accessibility_window_test.dart @@ -13,8 +13,8 @@ void main() { final Widget invalidButton = ElevatedButton( onPressed: () {}, style: ElevatedButton.styleFrom( - primary: Colors.orangeAccent, // background fill color - onPrimary: Colors.orange, // text foreground color + foregroundColor: Colors.orange, + backgroundColor: Colors.orangeAccent, ), child: const Text('Button'), ); @@ -31,8 +31,8 @@ void main() { final Widget invalidButton = ElevatedButton( onPressed: () {}, style: ElevatedButton.styleFrom( - primary: Colors.orangeAccent, // background fill color - onPrimary: Colors.orange, // text foreground color + foregroundColor: Colors.orange, + backgroundColor: Colors.orangeAccent, ), child: const Text('Button'), );