Unverified Commit 2532584b authored by Darren Austin's avatar Darren Austin Committed by GitHub

Update parameters to the `styleFrom` button methods. (#105291)

* Update parameters to the `styleFrom` button methods.

* Updated the Flutter fix data to point to this PR.

* Updated handling of background color to better maintain backwards compatibility with previous API.
parent ce10e6ff
...@@ -247,7 +247,7 @@ class TravelDestinationContent extends StatelessWidget { ...@@ -247,7 +247,7 @@ class TravelDestinationContent extends StatelessWidget {
final ThemeData theme = Theme.of(context); final ThemeData theme = Theme.of(context);
final TextStyle titleStyle = theme.textTheme.headline5!.copyWith(color: Colors.white); final TextStyle titleStyle = theme.textTheme.headline5!.copyWith(color: Colors.white);
final TextStyle descriptionStyle = theme.textTheme.subtitle1!; 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( return Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
......
...@@ -78,7 +78,7 @@ class _ShoppingCartPageState extends State<ShoppingCartPage> { ...@@ -78,7 +78,7 @@ class _ShoppingCartPageState extends State<ShoppingCartPage> {
right: 16.0, right: 16.0,
child: ElevatedButton( child: ElevatedButton(
style: ElevatedButton.styleFrom( style: ElevatedButton.styleFrom(
primary: kShrinePink100, backgroundColor: kShrinePink100,
shape: const BeveledRectangleBorder( shape: const BeveledRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(7.0)), borderRadius: BorderRadius.all(Radius.circular(7.0)),
), ),
......
...@@ -177,7 +177,7 @@ class _TextButton extends StatelessWidget { ...@@ -177,7 +177,7 @@ class _TextButton extends StatelessWidget {
final ThemeData theme = Theme.of(context); final ThemeData theme = Theme.of(context);
return TextButton( return TextButton(
style: TextButton.styleFrom( style: TextButton.styleFrom(
primary: theme.colorScheme.onPrimary, foregroundColor: theme.colorScheme.onPrimary,
textStyle: theme.textTheme.subtitle1, textStyle: theme.textTheme.subtitle1,
padding: EdgeInsets.zero, padding: EdgeInsets.zero,
), ),
......
...@@ -534,7 +534,7 @@ class _MyHomePageState extends State<MyHomePage> { ...@@ -534,7 +534,7 @@ class _MyHomePageState extends State<MyHomePage> {
label: _model.rtl ? 'زر مسطح' : 'Text Button', label: _model.rtl ? 'زر مسطح' : 'Text Button',
child: TextButton( child: TextButton(
style: TextButton.styleFrom( style: TextButton.styleFrom(
primary: Colors.white, foregroundColor: Colors.white,
backgroundColor: m2Swatch[200] backgroundColor: m2Swatch[200]
), ),
onPressed: _model.enable ? () {} : null, onPressed: _model.enable ? () {} : null,
......
...@@ -44,7 +44,7 @@ class _HomeState extends State<Home> { ...@@ -44,7 +44,7 @@ class _HomeState extends State<Home> {
children: <Widget>[ children: <Widget>[
TextButton( TextButton(
style: TextButton.styleFrom( style: TextButton.styleFrom(
primary: Colors.white, foregroundColor: Colors.white,
backgroundColor: Colors.red.shade800, backgroundColor: Colors.red.shade800,
), ),
onPressed: () { Navigator.pushNamed(context, 'underlines'); }, onPressed: () { Navigator.pushNamed(context, 'underlines'); },
...@@ -52,7 +52,7 @@ class _HomeState extends State<Home> { ...@@ -52,7 +52,7 @@ class _HomeState extends State<Home> {
), ),
TextButton( TextButton(
style: TextButton.styleFrom( style: TextButton.styleFrom(
primary: Colors.white, foregroundColor: Colors.white,
backgroundColor: Colors.orange.shade700, backgroundColor: Colors.orange.shade700,
), ),
onPressed: () { Navigator.pushNamed(context, 'fallback'); }, onPressed: () { Navigator.pushNamed(context, 'fallback'); },
...@@ -60,7 +60,7 @@ class _HomeState extends State<Home> { ...@@ -60,7 +60,7 @@ class _HomeState extends State<Home> {
), ),
TextButton( TextButton(
style: TextButton.styleFrom( style: TextButton.styleFrom(
primary: Colors.black, foregroundColor: Colors.black,
backgroundColor: Colors.yellow.shade700, backgroundColor: Colors.yellow.shade700,
), ),
onPressed: () { Navigator.pushNamed(context, 'bidi'); }, onPressed: () { Navigator.pushNamed(context, 'bidi'); },
...@@ -68,7 +68,7 @@ class _HomeState extends State<Home> { ...@@ -68,7 +68,7 @@ class _HomeState extends State<Home> {
), ),
TextButton( TextButton(
style: TextButton.styleFrom( style: TextButton.styleFrom(
primary: Colors.black, foregroundColor: Colors.black,
backgroundColor: Colors.green.shade400, backgroundColor: Colors.green.shade400,
), ),
onPressed: () { Navigator.pushNamed(context, 'fuzzer'); }, onPressed: () { Navigator.pushNamed(context, 'fuzzer'); },
...@@ -76,7 +76,7 @@ class _HomeState extends State<Home> { ...@@ -76,7 +76,7 @@ class _HomeState extends State<Home> {
), ),
TextButton( TextButton(
style: TextButton.styleFrom( style: TextButton.styleFrom(
primary: Colors.white, foregroundColor: Colors.white,
backgroundColor: Colors.blue.shade400, backgroundColor: Colors.blue.shade400,
), ),
onPressed: () { Navigator.pushNamed(context, 'zalgo'); }, onPressed: () { Navigator.pushNamed(context, 'zalgo'); },
...@@ -84,7 +84,7 @@ class _HomeState extends State<Home> { ...@@ -84,7 +84,7 @@ class _HomeState extends State<Home> {
), ),
TextButton( TextButton(
style: TextButton.styleFrom( style: TextButton.styleFrom(
primary: Colors.black, foregroundColor: Colors.black,
backgroundColor: Colors.purple.shade200, backgroundColor: Colors.purple.shade200,
), ),
onPressed: () { Navigator.pushNamed(context, 'painting'); }, onPressed: () { Navigator.pushNamed(context, 'painting'); },
...@@ -618,7 +618,7 @@ class _UnderlinesState extends State<Underlines> { ...@@ -618,7 +618,7 @@ class _UnderlinesState extends State<Underlines> {
), ),
TextButton( TextButton(
style: TextButton.styleFrom( style: TextButton.styleFrom(
primary: Colors.white, foregroundColor: Colors.white,
backgroundColor: Colors.red, backgroundColor: Colors.red,
), ),
onPressed: _text == '' ? null : () { onPressed: _text == '' ? null : () {
......
...@@ -25,7 +25,7 @@ class AppBarExample extends StatelessWidget { ...@@ -25,7 +25,7 @@ class AppBarExample extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final ButtonStyle style = TextButton.styleFrom( final ButtonStyle style = TextButton.styleFrom(
primary: Theme.of(context).colorScheme.onPrimary, foregroundColor: Theme.of(context).colorScheme.onPrimary,
); );
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
......
...@@ -63,10 +63,8 @@ class ButtonTypesGroup extends StatelessWidget { ...@@ -63,10 +63,8 @@ class ButtonTypesGroup extends StatelessWidget {
// 'Filled' type. // 'Filled' type.
ElevatedButton( ElevatedButton(
style: ElevatedButton.styleFrom( style: ElevatedButton.styleFrom(
// Foreground color foregroundColor: Theme.of(context).colorScheme.onPrimary,
onPrimary: Theme.of(context).colorScheme.onPrimary, backgroundColor: Theme.of(context).colorScheme.primary,
// Background color
primary: Theme.of(context).colorScheme.primary,
).copyWith(elevation: ButtonStyleButton.allOrNull(0.0)), ).copyWith(elevation: ButtonStyleButton.allOrNull(0.0)),
onPressed: onPressed, onPressed: onPressed,
child: const Text('Filled'), child: const Text('Filled'),
...@@ -76,10 +74,8 @@ class ButtonTypesGroup extends StatelessWidget { ...@@ -76,10 +74,8 @@ class ButtonTypesGroup extends StatelessWidget {
// 'Filled Tonal' type. // 'Filled Tonal' type.
ElevatedButton( ElevatedButton(
style: ElevatedButton.styleFrom( style: ElevatedButton.styleFrom(
// Foreground color foregroundColor: Theme.of(context).colorScheme.onSecondaryContainer,
onPrimary: Theme.of(context).colorScheme.onSecondaryContainer, backgroundColor: Theme.of(context).colorScheme.secondaryContainer,
// Background color
primary: Theme.of(context).colorScheme.secondaryContainer,
).copyWith(elevation: ButtonStyleButton.allOrNull(0.0)), ).copyWith(elevation: ButtonStyleButton.allOrNull(0.0)),
onPressed: onPressed, onPressed: onPressed,
child: const Text('Filled Tonal'), child: const Text('Filled Tonal'),
......
...@@ -35,7 +35,7 @@ class RootPage extends StatelessWidget { ...@@ -35,7 +35,7 @@ class RootPage extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
final TextStyle headline5 = Theme.of(context).textTheme.headline5!; final TextStyle headline5 = Theme.of(context).textTheme.headline5!;
final ButtonStyle buttonStyle = ElevatedButton.styleFrom( final ButtonStyle buttonStyle = ElevatedButton.styleFrom(
primary: destination.color, backgroundColor: destination.color,
visualDensity: VisualDensity.comfortable, visualDensity: VisualDensity.comfortable,
padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 16), padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 16),
textStyle: headline5, textStyle: headline5,
...@@ -125,7 +125,7 @@ class ListPage extends StatelessWidget { ...@@ -125,7 +125,7 @@ class ListPage extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
const int itemCount = 50; const int itemCount = 50;
final ButtonStyle buttonStyle = OutlinedButton.styleFrom( final ButtonStyle buttonStyle = OutlinedButton.styleFrom(
primary: destination.color, foregroundColor: destination.color,
fixedSize: const Size.fromHeight(128), fixedSize: const Size.fromHeight(128),
textStyle: Theme.of(context).textTheme.headline5, textStyle: Theme.of(context).textTheme.headline5,
); );
......
...@@ -69,8 +69,8 @@ class MyStatelessWidget extends StatelessWidget { ...@@ -69,8 +69,8 @@ class MyStatelessWidget extends StatelessWidget {
), ),
TextButton( TextButton(
style: TextButton.styleFrom( style: TextButton.styleFrom(
foregroundColor: Colors.white,
padding: const EdgeInsets.all(16.0), padding: const EdgeInsets.all(16.0),
primary: Colors.white,
textStyle: const TextStyle(fontSize: 20), textStyle: const TextStyle(fontSize: 20),
), ),
onPressed: () {}, onPressed: () {},
......
...@@ -49,7 +49,7 @@ class MyStatelessWidget extends StatelessWidget { ...@@ -49,7 +49,7 @@ class MyStatelessWidget extends StatelessWidget {
child: AbsorbPointer( child: AbsorbPointer(
child: ElevatedButton( child: ElevatedButton(
style: ElevatedButton.styleFrom( style: ElevatedButton.styleFrom(
primary: Colors.blue.shade200, backgroundColor: Colors.blue.shade200,
), ),
onPressed: () {}, onPressed: () {},
child: null, child: null,
......
...@@ -17,6 +17,167 @@ ...@@ -17,6 +17,167 @@
version: 1 version: 1
transforms: 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 # Changes made in https://github.com/flutter/flutter/pull/100381
- title: "Migrate 'TextSelectionOverlay.fadeDuration' to SelectionOverlay.fadeDuration" - title: "Migrate 'TextSelectionOverlay.fadeDuration' to SelectionOverlay.fadeDuration"
date: 2022-03-18 date: 2022-03-18
......
...@@ -135,9 +135,10 @@ class ElevatedButton extends ButtonStyleButton { ...@@ -135,9 +135,10 @@ class ElevatedButton extends ButtonStyleButton {
/// ) /// )
/// ``` /// ```
static ButtonStyle styleFrom({ static ButtonStyle styleFrom({
Color? primary, Color? foregroundColor,
Color? onPrimary, Color? backgroundColor,
Color? onSurface, Color? disabledForegroundColor,
Color? disabledBackgroundColor,
Color? shadowColor, Color? shadowColor,
Color? surfaceTintColor, Color? surfaceTintColor,
double? elevation, double? elevation,
...@@ -156,16 +157,35 @@ class ElevatedButton extends ButtonStyleButton { ...@@ -156,16 +157,35 @@ class ElevatedButton extends ButtonStyleButton {
bool? enableFeedback, bool? enableFeedback,
AlignmentGeometry? alignment, AlignmentGeometry? alignment,
InteractiveInkFeatureFactory? splashFactory, 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<Color?>? backgroundColor = (onSurface == null && primary == null) final Color? background = backgroundColor ?? primary;
final Color? disabledBackground = disabledBackgroundColor ?? onSurface?.withOpacity(0.12);
final MaterialStateProperty<Color?>? backgroundColorProp = (background == null && disabledBackground == null)
? null ? null
: _ElevatedButtonDefaultBackground(primary, onSurface); : _ElevatedButtonDefaultColor(background, disabledBackground);
final MaterialStateProperty<Color?>? foregroundColor = (onSurface == null && onPrimary == null) final Color? foreground = foregroundColor ?? onPrimary;
final Color? disabledForeground = disabledForegroundColor ?? onSurface?.withOpacity(0.38);
final MaterialStateProperty<Color?>? foregroundColorProp = (foreground == null && disabledForeground == null)
? null ? null
: _ElevatedButtonDefaultForeground(onPrimary, onSurface); : _ElevatedButtonDefaultColor(foreground, disabledForeground);
final MaterialStateProperty<Color?>? overlayColor = (onPrimary == null) final MaterialStateProperty<Color?>? overlayColor = (foreground == null)
? null ? null
: _ElevatedButtonDefaultOverlay(onPrimary); : _ElevatedButtonDefaultOverlay(foreground);
final MaterialStateProperty<double>? elevationValue = (elevation == null) final MaterialStateProperty<double>? elevationValue = (elevation == null)
? null ? null
: _ElevatedButtonDefaultElevation(elevation); : _ElevatedButtonDefaultElevation(elevation);
...@@ -175,8 +195,8 @@ class ElevatedButton extends ButtonStyleButton { ...@@ -175,8 +195,8 @@ class ElevatedButton extends ButtonStyleButton {
return ButtonStyle( return ButtonStyle(
textStyle: MaterialStatePropertyAll<TextStyle?>(textStyle), textStyle: MaterialStatePropertyAll<TextStyle?>(textStyle),
backgroundColor: backgroundColor, backgroundColor: backgroundColorProp,
foregroundColor: foregroundColor, foregroundColor: foregroundColorProp,
overlayColor: overlayColor, overlayColor: overlayColor,
shadowColor: ButtonStyleButton.allOrNull<Color>(shadowColor), shadowColor: ButtonStyleButton.allOrNull<Color>(shadowColor),
surfaceTintColor: ButtonStyleButton.allOrNull<Color>(surfaceTintColor), surfaceTintColor: ButtonStyleButton.allOrNull<Color>(surfaceTintColor),
...@@ -321,9 +341,10 @@ class ElevatedButton extends ButtonStyleButton { ...@@ -321,9 +341,10 @@ class ElevatedButton extends ButtonStyleButton {
return Theme.of(context).useMaterial3 return Theme.of(context).useMaterial3
? _TokenDefaultsM3(context) ? _TokenDefaultsM3(context)
: styleFrom( : styleFrom(
primary: colorScheme.primary, backgroundColor: colorScheme.primary,
onPrimary: colorScheme.onPrimary, foregroundColor: colorScheme.onPrimary,
onSurface: colorScheme.onSurface, disabledBackgroundColor: colorScheme.onSurface.withOpacity(0.12),
disabledForegroundColor: colorScheme.onSurface.withOpacity(0.38),
shadowColor: theme.shadowColor, shadowColor: theme.shadowColor,
elevation: 2, elevation: 2,
textStyle: theme.textTheme.button, textStyle: theme.textTheme.button,
...@@ -360,50 +381,34 @@ EdgeInsetsGeometry _scaledPadding(BuildContext context) { ...@@ -360,50 +381,34 @@ EdgeInsetsGeometry _scaledPadding(BuildContext context) {
} }
@immutable @immutable
class _ElevatedButtonDefaultBackground extends MaterialStateProperty<Color?> with Diagnosticable { class _ElevatedButtonDefaultColor extends MaterialStateProperty<Color?> with Diagnosticable {
_ElevatedButtonDefaultBackground(this.primary, this.onSurface); _ElevatedButtonDefaultColor(this.color, this.disabled);
final Color? primary;
final Color? onSurface;
@override
Color? resolve(Set<MaterialState> states) {
if (states.contains(MaterialState.disabled)) {
return onSurface?.withOpacity(0.12);
}
return primary;
}
}
@immutable
class _ElevatedButtonDefaultForeground extends MaterialStateProperty<Color?> with Diagnosticable {
_ElevatedButtonDefaultForeground(this.onPrimary, this.onSurface);
final Color? onPrimary; final Color? color;
final Color? onSurface; final Color? disabled;
@override @override
Color? resolve(Set<MaterialState> states) { Color? resolve(Set<MaterialState> states) {
if (states.contains(MaterialState.disabled)) { if (states.contains(MaterialState.disabled)) {
return onSurface?.withOpacity(0.38); return disabled;
} }
return onPrimary; return color;
} }
} }
@immutable @immutable
class _ElevatedButtonDefaultOverlay extends MaterialStateProperty<Color?> with Diagnosticable { class _ElevatedButtonDefaultOverlay extends MaterialStateProperty<Color?> with Diagnosticable {
_ElevatedButtonDefaultOverlay(this.onPrimary); _ElevatedButtonDefaultOverlay(this.overlay);
final Color onPrimary; final Color overlay;
@override @override
Color? resolve(Set<MaterialState> states) { Color? resolve(Set<MaterialState> states) {
if (states.contains(MaterialState.hovered)) { if (states.contains(MaterialState.hovered)) {
return onPrimary.withOpacity(0.08); return overlay.withOpacity(0.08);
} }
if (states.contains(MaterialState.focused) || states.contains(MaterialState.pressed)) { if (states.contains(MaterialState.focused) || states.contains(MaterialState.pressed)) {
return onPrimary.withOpacity(0.24); return overlay.withOpacity(0.24);
} }
return null; return null;
} }
......
...@@ -133,9 +133,10 @@ class OutlinedButton extends ButtonStyleButton { ...@@ -133,9 +133,10 @@ class OutlinedButton extends ButtonStyleButton {
/// ) /// )
/// ``` /// ```
static ButtonStyle styleFrom({ static ButtonStyle styleFrom({
Color? primary, Color? foregroundColor,
Color? onSurface,
Color? backgroundColor, Color? backgroundColor,
Color? disabledForegroundColor,
Color? disabledBackgroundColor,
Color? shadowColor, Color? shadowColor,
Color? surfaceTintColor, Color? surfaceTintColor,
double? elevation, double? elevation,
...@@ -154,21 +155,38 @@ class OutlinedButton extends ButtonStyleButton { ...@@ -154,21 +155,38 @@ class OutlinedButton extends ButtonStyleButton {
bool? enableFeedback, bool? enableFeedback,
AlignmentGeometry? alignment, AlignmentGeometry? alignment,
InteractiveInkFeatureFactory? splashFactory, 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<Color?>? foregroundColor = (onSurface == null && primary == null) final Color? foreground = foregroundColor ?? primary;
final Color? disabledForeground = disabledForegroundColor ?? onSurface?.withOpacity(0.38);
final MaterialStateProperty<Color?>? foregroundColorProp = (foreground == null && disabledForeground == null)
? null
: _OutlinedButtonDefaultColor(foreground, disabledForeground);
final MaterialStateProperty<Color?>? backgroundColorProp = (backgroundColor == null && disabledBackgroundColor == null)
? null ? null
: _OutlinedButtonDefaultForeground(primary, onSurface); : disabledBackgroundColor == null
final MaterialStateProperty<Color?>? overlayColor = (primary == null) ? ButtonStyleButton.allOrNull<Color?>(backgroundColor)
: _OutlinedButtonDefaultColor(backgroundColor, disabledBackgroundColor);
final MaterialStateProperty<Color?>? overlayColor = (foreground == null)
? null ? null
: _OutlinedButtonDefaultOverlay(primary); : _OutlinedButtonDefaultOverlay(foreground);
final MaterialStateProperty<MouseCursor>? mouseCursor = (enabledMouseCursor == null && disabledMouseCursor == null) final MaterialStateProperty<MouseCursor>? mouseCursor = (enabledMouseCursor == null && disabledMouseCursor == null)
? null ? null
: _OutlinedButtonDefaultMouseCursor(enabledMouseCursor!, disabledMouseCursor!); : _OutlinedButtonDefaultMouseCursor(enabledMouseCursor!, disabledMouseCursor!);
return ButtonStyle( return ButtonStyle(
textStyle: ButtonStyleButton.allOrNull<TextStyle>(textStyle), textStyle: ButtonStyleButton.allOrNull<TextStyle>(textStyle),
foregroundColor: foregroundColor, foregroundColor: foregroundColorProp,
backgroundColor: ButtonStyleButton.allOrNull<Color>(backgroundColor), backgroundColor: backgroundColorProp,
overlayColor: overlayColor, overlayColor: overlayColor,
shadowColor: ButtonStyleButton.allOrNull<Color>(shadowColor), shadowColor: ButtonStyleButton.allOrNull<Color>(shadowColor),
surfaceTintColor: ButtonStyleButton.allOrNull<Color>(surfaceTintColor), surfaceTintColor: ButtonStyleButton.allOrNull<Color>(surfaceTintColor),
...@@ -290,9 +308,10 @@ class OutlinedButton extends ButtonStyleButton { ...@@ -290,9 +308,10 @@ class OutlinedButton extends ButtonStyleButton {
return Theme.of(context).useMaterial3 return Theme.of(context).useMaterial3
? _TokenDefaultsM3(context) ? _TokenDefaultsM3(context)
: styleFrom( : styleFrom(
primary: colorScheme.primary, foregroundColor: colorScheme.primary,
onSurface: colorScheme.onSurface, disabledForegroundColor: colorScheme.onSurface.withOpacity(0.38),
backgroundColor: Colors.transparent, backgroundColor: Colors.transparent,
disabledBackgroundColor: Colors.transparent,
shadowColor: theme.shadowColor, shadowColor: theme.shadowColor,
elevation: 0, elevation: 0,
textStyle: theme.textTheme.button, textStyle: theme.textTheme.button,
...@@ -330,34 +349,34 @@ EdgeInsetsGeometry _scaledPadding(BuildContext context) { ...@@ -330,34 +349,34 @@ EdgeInsetsGeometry _scaledPadding(BuildContext context) {
} }
@immutable @immutable
class _OutlinedButtonDefaultForeground extends MaterialStateProperty<Color?> with Diagnosticable { class _OutlinedButtonDefaultColor extends MaterialStateProperty<Color?> with Diagnosticable {
_OutlinedButtonDefaultForeground(this.primary, this.onSurface); _OutlinedButtonDefaultColor(this.color, this.disabled);
final Color? primary; final Color? color;
final Color? onSurface; final Color? disabled;
@override @override
Color? resolve(Set<MaterialState> states) { Color? resolve(Set<MaterialState> states) {
if (states.contains(MaterialState.disabled)) { if (states.contains(MaterialState.disabled)) {
return onSurface?.withOpacity(0.38); return disabled;
} }
return primary; return color;
} }
} }
@immutable @immutable
class _OutlinedButtonDefaultOverlay extends MaterialStateProperty<Color?> with Diagnosticable { class _OutlinedButtonDefaultOverlay extends MaterialStateProperty<Color?> with Diagnosticable {
_OutlinedButtonDefaultOverlay(this.primary); _OutlinedButtonDefaultOverlay(this.foreground);
final Color primary; final Color foreground;
@override @override
Color? resolve(Set<MaterialState> states) { Color? resolve(Set<MaterialState> states) {
if (states.contains(MaterialState.hovered)) { if (states.contains(MaterialState.hovered)) {
return primary.withOpacity(0.04); return foreground.withOpacity(0.04);
} }
if (states.contains(MaterialState.focused) || states.contains(MaterialState.pressed)) { if (states.contains(MaterialState.focused) || states.contains(MaterialState.pressed)) {
return primary.withOpacity(0.12); return foreground.withOpacity(0.12);
} }
return null; return null;
} }
......
...@@ -141,9 +141,10 @@ class TextButton extends ButtonStyleButton { ...@@ -141,9 +141,10 @@ class TextButton extends ButtonStyleButton {
/// ) /// )
/// ``` /// ```
static ButtonStyle styleFrom({ static ButtonStyle styleFrom({
Color? primary, Color? foregroundColor,
Color? onSurface,
Color? backgroundColor, Color? backgroundColor,
Color? disabledForegroundColor,
Color? disabledBackgroundColor,
Color? shadowColor, Color? shadowColor,
Color? surfaceTintColor, Color? surfaceTintColor,
double? elevation, double? elevation,
...@@ -162,21 +163,38 @@ class TextButton extends ButtonStyleButton { ...@@ -162,21 +163,38 @@ class TextButton extends ButtonStyleButton {
bool? enableFeedback, bool? enableFeedback,
AlignmentGeometry? alignment, AlignmentGeometry? alignment,
InteractiveInkFeatureFactory? splashFactory, 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<Color?>? foregroundColor = (onSurface == null && primary == null) final Color? foreground = foregroundColor ?? primary;
final Color? disabledForeground = disabledForegroundColor ?? onSurface?.withOpacity(0.38);
final MaterialStateProperty<Color?>? foregroundColorProp = (foreground == null && disabledForeground == null)
? null
: _TextButtonDefaultColor(foreground, disabledForeground);
final MaterialStateProperty<Color?>? backgroundColorProp = (backgroundColor == null && disabledBackgroundColor == null)
? null ? null
: _TextButtonDefaultForeground(primary, onSurface); : disabledBackgroundColor == null
final MaterialStateProperty<Color?>? overlayColor = (primary == null) ? ButtonStyleButton.allOrNull<Color?>(backgroundColor)
: _TextButtonDefaultColor(backgroundColor, disabledBackgroundColor);
final MaterialStateProperty<Color?>? overlayColor = (foreground == null)
? null ? null
: _TextButtonDefaultOverlay(primary); : _TextButtonDefaultOverlay(foreground);
final MaterialStateProperty<MouseCursor>? mouseCursor = (enabledMouseCursor == null && disabledMouseCursor == null) final MaterialStateProperty<MouseCursor>? mouseCursor = (enabledMouseCursor == null && disabledMouseCursor == null)
? null ? null
: _TextButtonDefaultMouseCursor(enabledMouseCursor!, disabledMouseCursor!); : _TextButtonDefaultMouseCursor(enabledMouseCursor!, disabledMouseCursor!);
return ButtonStyle( return ButtonStyle(
textStyle: ButtonStyleButton.allOrNull<TextStyle>(textStyle), textStyle: ButtonStyleButton.allOrNull<TextStyle>(textStyle),
backgroundColor: ButtonStyleButton.allOrNull<Color>(backgroundColor), foregroundColor: foregroundColorProp,
foregroundColor: foregroundColor, backgroundColor: backgroundColorProp,
overlayColor: overlayColor, overlayColor: overlayColor,
shadowColor: ButtonStyleButton.allOrNull<Color>(shadowColor), shadowColor: ButtonStyleButton.allOrNull<Color>(shadowColor),
surfaceTintColor: ButtonStyleButton.allOrNull<Color>(surfaceTintColor), surfaceTintColor: ButtonStyleButton.allOrNull<Color>(surfaceTintColor),
...@@ -311,9 +329,10 @@ class TextButton extends ButtonStyleButton { ...@@ -311,9 +329,10 @@ class TextButton extends ButtonStyleButton {
return Theme.of(context).useMaterial3 return Theme.of(context).useMaterial3
? _TokenDefaultsM3(context) ? _TokenDefaultsM3(context)
: styleFrom( : styleFrom(
primary: colorScheme.primary, foregroundColor: colorScheme.primary,
onSurface: colorScheme.onSurface, disabledForegroundColor: colorScheme.onSurface.withOpacity(0.38),
backgroundColor: Colors.transparent, backgroundColor: Colors.transparent,
disabledBackgroundColor: Colors.transparent,
shadowColor: theme.shadowColor, shadowColor: theme.shadowColor,
elevation: 0, elevation: 0,
textStyle: theme.textTheme.button, textStyle: theme.textTheme.button,
...@@ -350,23 +369,23 @@ EdgeInsetsGeometry _scaledPadding(BuildContext context) { ...@@ -350,23 +369,23 @@ EdgeInsetsGeometry _scaledPadding(BuildContext context) {
} }
@immutable @immutable
class _TextButtonDefaultForeground extends MaterialStateProperty<Color?> { class _TextButtonDefaultColor extends MaterialStateProperty<Color?> {
_TextButtonDefaultForeground(this.primary, this.onSurface); _TextButtonDefaultColor(this.color, this.disabled);
final Color? primary; final Color? color;
final Color? onSurface; final Color? disabled;
@override @override
Color? resolve(Set<MaterialState> states) { Color? resolve(Set<MaterialState> states) {
if (states.contains(MaterialState.disabled)) { if (states.contains(MaterialState.disabled)) {
return onSurface?.withOpacity(0.38); return disabled;
} }
return primary; return color;
} }
@override @override
String toString() { String toString() {
return '{disabled: ${onSurface?.withOpacity(0.38)}, otherwise: $primary}'; return '{disabled: $disabled, otherwise: $color}';
} }
} }
......
...@@ -1600,6 +1600,30 @@ void main() { ...@@ -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<Material>(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 { testWidgets('ElevatedButton statesController', (WidgetTester tester) async {
int count = 0; int count = 0;
void valueChanged() { void valueChanged() {
......
...@@ -1718,6 +1718,30 @@ void main() { ...@@ -1718,6 +1718,30 @@ void main() {
expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.basic); 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<Material>(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 { testWidgets('OutlinedButton statesController', (WidgetTester tester) async {
int count = 0; int count = 0;
void valueChanged() { void valueChanged() {
......
...@@ -1525,6 +1525,30 @@ void main() { ...@@ -1525,6 +1525,30 @@ void main() {
expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.basic); 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<Material>(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 { testWidgets('TextButton statesController', (WidgetTester tester) async {
int count = 0; int count = 0;
void valueChanged() { void valueChanged() {
......
...@@ -554,4 +554,19 @@ void main() { ...@@ -554,4 +554,19 @@ void main() {
// Change made in https://github.com/flutter/flutter/pull/100381 // Change made in https://github.com/flutter/flutter/pull/100381
TextSelectionOverlay.fadeDuration; 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,
);
} }
...@@ -527,4 +527,15 @@ void main() { ...@@ -527,4 +527,15 @@ void main() {
// Change made in https://github.com/flutter/flutter/pull/100381 // Change made in https://github.com/flutter/flutter/pull/100381
SelectionOverlay.fadeDuration; 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),
);
} }
...@@ -765,7 +765,7 @@ void main() { ...@@ -765,7 +765,7 @@ void main() {
backgroundColor: Colors.white, backgroundColor: Colors.white,
body: ElevatedButton( body: ElevatedButton(
style: ElevatedButton.styleFrom( style: ElevatedButton.styleFrom(
primary: const Color(0xFFFBBC04), backgroundColor: const Color(0xFFFBBC04),
elevation: 0, elevation: 0,
), ),
onPressed: () {}, onPressed: () {},
......
...@@ -13,8 +13,8 @@ void main() { ...@@ -13,8 +13,8 @@ void main() {
final Widget invalidButton = ElevatedButton( final Widget invalidButton = ElevatedButton(
onPressed: () {}, onPressed: () {},
style: ElevatedButton.styleFrom( style: ElevatedButton.styleFrom(
primary: Colors.orangeAccent, // background fill color foregroundColor: Colors.orange,
onPrimary: Colors.orange, // text foreground color backgroundColor: Colors.orangeAccent,
), ),
child: const Text('Button'), child: const Text('Button'),
); );
...@@ -31,8 +31,8 @@ void main() { ...@@ -31,8 +31,8 @@ void main() {
final Widget invalidButton = ElevatedButton( final Widget invalidButton = ElevatedButton(
onPressed: () {}, onPressed: () {},
style: ElevatedButton.styleFrom( style: ElevatedButton.styleFrom(
primary: Colors.orangeAccent, // background fill color foregroundColor: Colors.orange,
onPrimary: Colors.orange, // text foreground color backgroundColor: Colors.orangeAccent,
), ),
child: const Text('Button'), child: const Text('Button'),
); );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment