Default colorScheme data in ButtonThemeData (Fix for #38655) (#39627)

* Set default colorScheme for ButtonThemeData
This commit is contained in:
Keshava Muraari 2019-10-14 21:10:02 +05:30 committed by Shi-Hao Hong
parent dee7583908
commit 19899db223
2 changed files with 92 additions and 12 deletions

View file

@ -83,7 +83,7 @@ class ButtonTheme extends InheritedTheme {
Color hoverColor,
Color highlightColor,
Color splashColor,
ColorScheme colorScheme,
ColorScheme colorScheme = const ColorScheme.light(),
MaterialTapTargetSize materialTapTargetSize,
Widget child,
}) : assert(textTheme != null),
@ -91,6 +91,7 @@ class ButtonTheme extends InheritedTheme {
assert(height != null && height >= 0.0),
assert(alignedDropdown != null),
assert(layoutBehavior != null),
assert(colorScheme != null),
data = ButtonThemeData(
textTheme: textTheme,
minWidth: minWidth,
@ -179,13 +180,14 @@ class ButtonTheme extends InheritedTheme {
Color hoverColor,
Color highlightColor,
Color splashColor,
ColorScheme colorScheme,
ColorScheme colorScheme = const ColorScheme.light(),
Widget child,
ButtonBarLayoutBehavior layoutBehavior = ButtonBarLayoutBehavior.padded,
}) : assert(textTheme != null),
assert(minWidth != null && minWidth >= 0.0),
assert(height != null && height >= 0.0),
assert(alignedDropdown != null),
assert(colorScheme != null),
data = ButtonThemeData(
textTheme: textTheme,
minWidth: minWidth,
@ -217,15 +219,13 @@ class ButtonTheme extends InheritedTheme {
static ButtonThemeData of(BuildContext context) {
final ButtonTheme inheritedButtonTheme = context.inheritFromWidgetOfExactType(ButtonTheme);
ButtonThemeData buttonTheme = inheritedButtonTheme?.data;
if (buttonTheme?.colorScheme == null) { // if buttonTheme or buttonTheme.colorScheme is null
if (buttonTheme == null) {
final ThemeData theme = Theme.of(context);
buttonTheme ??= theme.buttonTheme;
if (buttonTheme.colorScheme == null) {
buttonTheme = buttonTheme.copyWith(
colorScheme: theme.buttonTheme.colorScheme ?? theme.colorScheme,
);
assert(buttonTheme.colorScheme != null);
}
buttonTheme = buttonTheme.copyWith(
colorScheme: theme.buttonTheme.colorScheme ?? const ColorScheme.light(),
);
assert(buttonTheme.colorScheme != null);
}
return buttonTheme;
}
@ -270,13 +270,14 @@ class ButtonThemeData extends Diagnosticable {
Color hoverColor,
Color highlightColor,
Color splashColor,
this.colorScheme,
this.colorScheme = const ColorScheme.light(),
MaterialTapTargetSize materialTapTargetSize,
}) : assert(textTheme != null),
assert(minWidth != null && minWidth >= 0.0),
assert(height != null && height >= 0.0),
assert(alignedDropdown != null),
assert(layoutBehavior != null),
assert(colorScheme != null),
_buttonColor = buttonColor,
_disabledColor = disabledColor,
_focusColor = focusColor,

View file

@ -18,6 +18,7 @@ void main() {
));
expect(theme.alignedDropdown, false);
expect(theme.layoutBehavior, ButtonBarLayoutBehavior.padded);
expect(theme.colorScheme, const ColorScheme.light());
});
test('ButtonThemeData default overrides', () {
@ -28,12 +29,14 @@ void main() {
padding: EdgeInsets.zero,
shape: RoundedRectangleBorder(),
alignedDropdown: true,
colorScheme: ColorScheme.dark()
);
expect(theme.textTheme, ButtonTextTheme.primary);
expect(theme.constraints, const BoxConstraints(minWidth: 100.0, minHeight: 200.0));
expect(theme.padding, EdgeInsets.zero);
expect(theme.shape, const RoundedRectangleBorder());
expect(theme.alignedDropdown, true);
expect(theme.colorScheme, const ColorScheme.dark());
});
testWidgets('ButtonTheme defaults', (WidgetTester tester) async {
@ -80,7 +83,7 @@ void main() {
borderRadius: BorderRadius.all(Radius.circular(2.0)),
));
expect(alignedDropdown, false);
expect(colorScheme, ThemeData.light().colorScheme);
expect(colorScheme, const ColorScheme.light());
expect(tester.widget<Material>(find.byType(Material)).shape, shape);
expect(tester.getSize(find.byType(Material)), const Size(88.0, 36.0));
});
@ -95,7 +98,7 @@ void main() {
borderRadius: BorderRadius.all(Radius.circular(2.0)),
));
expect(theme.alignedDropdown, false);
expect(theme.colorScheme, null);
expect(theme.colorScheme, const ColorScheme.light());
theme = const ButtonThemeData().copyWith(
textTheme: ButtonTextTheme.primary,
@ -402,4 +405,80 @@ void main() {
},
semanticsEnabled: true,
);
testWidgets('Default RaisedButton text color when textTheme is set to ButtonTextTheme.accent', (WidgetTester tester) async {
// Test for https://github.com/flutter/flutter/issues/38655
const Color defaultEnabledAccentTextColor = Color(0xff2196f3);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Center(
child: RaisedButton(
child: const Text('RaisedButton'),
onPressed: () {},
textTheme: ButtonTextTheme.accent,
),
),
),
),
);
Color getRaisedButtonTextColor() {
return tester.renderObject<RenderParagraph>(find.text('RaisedButton')).text.style.color;
}
expect(getRaisedButtonTextColor(), equals(defaultEnabledAccentTextColor));
});
testWidgets('default button theme primary color for RaisedButton', (WidgetTester tester) async {
// Test for https://github.com/flutter/flutter/issues/38655
const Color defaultEnabledPrimaryTextColor = Color(0xff000000);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Center(
child: RaisedButton(
child: const Text('RaisedButton'),
onPressed: () {},
textTheme: ButtonTextTheme.primary,
),
),
),
),
);
Color getRaisedButtonTextColor() {
return tester.renderObject<RenderParagraph>(find.text('RaisedButton')).text.style.color;
}
expect(getRaisedButtonTextColor(), equals(defaultEnabledPrimaryTextColor));
});
testWidgets('default button theme normal color for RaisedButton', (WidgetTester tester) async {
// Test for https://github.com/flutter/flutter/issues/38655
const Color defaultEnabledNormalTextColor = Color(0xffffffff);
await tester.pumpWidget(
MaterialApp(
theme: ThemeData.dark(),
home: Scaffold(
body: Center(
child: RaisedButton(
child: const Text('RaisedButton'),
onPressed: () {},
textTheme: ButtonTextTheme.normal
),
),
),
),
);
Color getRaisedButtonTextColor() {
return tester.renderObject<RenderParagraph>(find.text('RaisedButton')).text.style.color;
}
expect(getRaisedButtonTextColor(), equals(defaultEnabledNormalTextColor));
});
}