Unverified Commit 2e10b46b authored by Taha Tesser's avatar Taha Tesser Committed by GitHub

Fix Local `CheckBoxTheme` not being inherited by `CheckBox` Widget (#97715)

parent ab89ce28
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'checkbox_theme.dart';
import 'constants.dart'; import 'constants.dart';
import 'debug.dart'; import 'debug.dart';
import 'material_state.dart'; import 'material_state.dart';
...@@ -403,11 +404,12 @@ class _CheckboxState extends State<Checkbox> with TickerProviderStateMixin, Togg ...@@ -403,11 +404,12 @@ class _CheckboxState extends State<Checkbox> with TickerProviderStateMixin, Togg
Widget build(BuildContext context) { Widget build(BuildContext context) {
assert(debugCheckHasMaterial(context)); assert(debugCheckHasMaterial(context));
final ThemeData themeData = Theme.of(context); final ThemeData themeData = Theme.of(context);
final CheckboxThemeData checkboxTheme = CheckboxTheme.of(context);
final MaterialTapTargetSize effectiveMaterialTapTargetSize = widget.materialTapTargetSize final MaterialTapTargetSize effectiveMaterialTapTargetSize = widget.materialTapTargetSize
?? themeData.checkboxTheme.materialTapTargetSize ?? checkboxTheme.materialTapTargetSize
?? themeData.materialTapTargetSize; ?? themeData.materialTapTargetSize;
final VisualDensity effectiveVisualDensity = widget.visualDensity final VisualDensity effectiveVisualDensity = widget.visualDensity
?? themeData.checkboxTheme.visualDensity ?? checkboxTheme.visualDensity
?? themeData.visualDensity; ?? themeData.visualDensity;
Size size; Size size;
switch (effectiveMaterialTapTargetSize) { switch (effectiveMaterialTapTargetSize) {
...@@ -422,7 +424,7 @@ class _CheckboxState extends State<Checkbox> with TickerProviderStateMixin, Togg ...@@ -422,7 +424,7 @@ class _CheckboxState extends State<Checkbox> with TickerProviderStateMixin, Togg
final MaterialStateProperty<MouseCursor> effectiveMouseCursor = MaterialStateProperty.resolveWith<MouseCursor>((Set<MaterialState> states) { final MaterialStateProperty<MouseCursor> effectiveMouseCursor = MaterialStateProperty.resolveWith<MouseCursor>((Set<MaterialState> states) {
return MaterialStateProperty.resolveAs<MouseCursor?>(widget.mouseCursor, states) return MaterialStateProperty.resolveAs<MouseCursor?>(widget.mouseCursor, states)
?? themeData.checkboxTheme.mouseCursor?.resolve(states) ?? checkboxTheme.mouseCursor?.resolve(states)
?? MaterialStateMouseCursor.clickable.resolve(states); ?? MaterialStateMouseCursor.clickable.resolve(states);
}); });
...@@ -432,37 +434,37 @@ class _CheckboxState extends State<Checkbox> with TickerProviderStateMixin, Togg ...@@ -432,37 +434,37 @@ class _CheckboxState extends State<Checkbox> with TickerProviderStateMixin, Togg
final Set<MaterialState> inactiveStates = states..remove(MaterialState.selected); final Set<MaterialState> inactiveStates = states..remove(MaterialState.selected);
final Color effectiveActiveColor = widget.fillColor?.resolve(activeStates) final Color effectiveActiveColor = widget.fillColor?.resolve(activeStates)
?? _widgetFillColor.resolve(activeStates) ?? _widgetFillColor.resolve(activeStates)
?? themeData.checkboxTheme.fillColor?.resolve(activeStates) ?? checkboxTheme.fillColor?.resolve(activeStates)
?? _defaultFillColor.resolve(activeStates); ?? _defaultFillColor.resolve(activeStates);
final Color effectiveInactiveColor = widget.fillColor?.resolve(inactiveStates) final Color effectiveInactiveColor = widget.fillColor?.resolve(inactiveStates)
?? _widgetFillColor.resolve(inactiveStates) ?? _widgetFillColor.resolve(inactiveStates)
?? themeData.checkboxTheme.fillColor?.resolve(inactiveStates) ?? checkboxTheme.fillColor?.resolve(inactiveStates)
?? _defaultFillColor.resolve(inactiveStates); ?? _defaultFillColor.resolve(inactiveStates);
final Set<MaterialState> focusedStates = states..add(MaterialState.focused); final Set<MaterialState> focusedStates = states..add(MaterialState.focused);
final Color effectiveFocusOverlayColor = widget.overlayColor?.resolve(focusedStates) final Color effectiveFocusOverlayColor = widget.overlayColor?.resolve(focusedStates)
?? widget.focusColor ?? widget.focusColor
?? themeData.checkboxTheme.overlayColor?.resolve(focusedStates) ?? checkboxTheme.overlayColor?.resolve(focusedStates)
?? themeData.focusColor; ?? themeData.focusColor;
final Set<MaterialState> hoveredStates = states..add(MaterialState.hovered); final Set<MaterialState> hoveredStates = states..add(MaterialState.hovered);
final Color effectiveHoverOverlayColor = widget.overlayColor?.resolve(hoveredStates) final Color effectiveHoverOverlayColor = widget.overlayColor?.resolve(hoveredStates)
?? widget.hoverColor ?? widget.hoverColor
?? themeData.checkboxTheme.overlayColor?.resolve(hoveredStates) ?? checkboxTheme.overlayColor?.resolve(hoveredStates)
?? themeData.hoverColor; ?? themeData.hoverColor;
final Set<MaterialState> activePressedStates = activeStates..add(MaterialState.pressed); final Set<MaterialState> activePressedStates = activeStates..add(MaterialState.pressed);
final Color effectiveActivePressedOverlayColor = widget.overlayColor?.resolve(activePressedStates) final Color effectiveActivePressedOverlayColor = widget.overlayColor?.resolve(activePressedStates)
?? themeData.checkboxTheme.overlayColor?.resolve(activePressedStates) ?? checkboxTheme.overlayColor?.resolve(activePressedStates)
?? effectiveActiveColor.withAlpha(kRadialReactionAlpha); ?? effectiveActiveColor.withAlpha(kRadialReactionAlpha);
final Set<MaterialState> inactivePressedStates = inactiveStates..add(MaterialState.pressed); final Set<MaterialState> inactivePressedStates = inactiveStates..add(MaterialState.pressed);
final Color effectiveInactivePressedOverlayColor = widget.overlayColor?.resolve(inactivePressedStates) final Color effectiveInactivePressedOverlayColor = widget.overlayColor?.resolve(inactivePressedStates)
?? themeData.checkboxTheme.overlayColor?.resolve(inactivePressedStates) ?? checkboxTheme.overlayColor?.resolve(inactivePressedStates)
?? effectiveActiveColor.withAlpha(kRadialReactionAlpha); ?? effectiveActiveColor.withAlpha(kRadialReactionAlpha);
final Color effectiveCheckColor = widget.checkColor final Color effectiveCheckColor = widget.checkColor
?? themeData.checkboxTheme.checkColor?.resolve(states) ?? checkboxTheme.checkColor?.resolve(states)
?? const Color(0xFFFFFFFF); ?? const Color(0xFFFFFFFF);
return Semantics( return Semantics(
...@@ -481,7 +483,7 @@ class _CheckboxState extends State<Checkbox> with TickerProviderStateMixin, Togg ...@@ -481,7 +483,7 @@ class _CheckboxState extends State<Checkbox> with TickerProviderStateMixin, Togg
..reactionColor = effectiveActivePressedOverlayColor ..reactionColor = effectiveActivePressedOverlayColor
..hoverColor = effectiveHoverOverlayColor ..hoverColor = effectiveHoverOverlayColor
..focusColor = effectiveFocusOverlayColor ..focusColor = effectiveFocusOverlayColor
..splashRadius = widget.splashRadius ?? themeData.checkboxTheme.splashRadius ?? kRadialReactionRadius ..splashRadius = widget.splashRadius ?? checkboxTheme.splashRadius ?? kRadialReactionRadius
..downPosition = downPosition ..downPosition = downPosition
..isFocused = states.contains(MaterialState.focused) ..isFocused = states.contains(MaterialState.focused)
..isHovered = states.contains(MaterialState.hovered) ..isHovered = states.contains(MaterialState.hovered)
...@@ -490,10 +492,10 @@ class _CheckboxState extends State<Checkbox> with TickerProviderStateMixin, Togg ...@@ -490,10 +492,10 @@ class _CheckboxState extends State<Checkbox> with TickerProviderStateMixin, Togg
..checkColor = effectiveCheckColor ..checkColor = effectiveCheckColor
..value = value ..value = value
..previousValue = _previousValue ..previousValue = _previousValue
..shape = widget.shape ?? themeData.checkboxTheme.shape ?? const RoundedRectangleBorder( ..shape = widget.shape ?? checkboxTheme.shape ?? const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(1.0)), borderRadius: BorderRadius.all(Radius.circular(1.0)),
) )
..side = _resolveSide(widget.side) ?? _resolveSide(themeData.checkboxTheme.side), ..side = _resolveSide(widget.side) ?? _resolveSide(checkboxTheme.side),
), ),
); );
} }
......
...@@ -351,6 +351,41 @@ void main() { ...@@ -351,6 +351,41 @@ void main() {
reason: 'Active pressed Checkbox should have overlay color: $activePressedOverlayColor', reason: 'Active pressed Checkbox should have overlay color: $activePressedOverlayColor',
); );
}); });
testWidgets('Local CheckboxTheme can override global CheckboxTheme', (WidgetTester tester) async {
const Color globalThemeFillColor = Color(0xfffffff1);
const Color globalThemeCheckColor = Color(0xff000000);
const Color localThemeFillColor = Color(0xffff0000);
const Color localThemeCheckColor = Color(0xffffffff);
Widget buildCheckbox({required bool active}) {
return MaterialApp(
theme: ThemeData(
checkboxTheme: CheckboxThemeData(
checkColor: MaterialStateProperty.all<Color>(globalThemeCheckColor),
fillColor: MaterialStateProperty.all<Color>(globalThemeFillColor),
),
),
home: Scaffold(
body: CheckboxTheme(
data: CheckboxThemeData(
fillColor: MaterialStateProperty.all<Color>(localThemeFillColor),
checkColor: MaterialStateProperty.all<Color>(localThemeCheckColor),
),
child: Checkbox(
value: active,
onChanged: (_) { },
),
),
),
);
}
await tester.pumpWidget(buildCheckbox(active: true));
await tester.pumpAndSettle();
expect(_getCheckboxMaterial(tester), paints..path(color: localThemeFillColor));
expect(_getCheckboxMaterial(tester), paints..path(color: localThemeFillColor)..path(color: localThemeCheckColor));
});
} }
Future<void> _pointGestureToCheckbox(WidgetTester tester) async { Future<void> _pointGestureToCheckbox(WidgetTester tester) async {
......
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