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 @@
import 'package:flutter/widgets.dart';
import 'checkbox_theme.dart';
import 'constants.dart';
import 'debug.dart';
import 'material_state.dart';
......@@ -403,11 +404,12 @@ class _CheckboxState extends State<Checkbox> with TickerProviderStateMixin, Togg
Widget build(BuildContext context) {
assert(debugCheckHasMaterial(context));
final ThemeData themeData = Theme.of(context);
final CheckboxThemeData checkboxTheme = CheckboxTheme.of(context);
final MaterialTapTargetSize effectiveMaterialTapTargetSize = widget.materialTapTargetSize
?? themeData.checkboxTheme.materialTapTargetSize
?? checkboxTheme.materialTapTargetSize
?? themeData.materialTapTargetSize;
final VisualDensity effectiveVisualDensity = widget.visualDensity
?? themeData.checkboxTheme.visualDensity
?? checkboxTheme.visualDensity
?? themeData.visualDensity;
Size size;
switch (effectiveMaterialTapTargetSize) {
......@@ -422,7 +424,7 @@ class _CheckboxState extends State<Checkbox> with TickerProviderStateMixin, Togg
final MaterialStateProperty<MouseCursor> effectiveMouseCursor = MaterialStateProperty.resolveWith<MouseCursor>((Set<MaterialState> states) {
return MaterialStateProperty.resolveAs<MouseCursor?>(widget.mouseCursor, states)
?? themeData.checkboxTheme.mouseCursor?.resolve(states)
?? checkboxTheme.mouseCursor?.resolve(states)
?? MaterialStateMouseCursor.clickable.resolve(states);
});
......@@ -432,37 +434,37 @@ class _CheckboxState extends State<Checkbox> with TickerProviderStateMixin, Togg
final Set<MaterialState> inactiveStates = states..remove(MaterialState.selected);
final Color effectiveActiveColor = widget.fillColor?.resolve(activeStates)
?? _widgetFillColor.resolve(activeStates)
?? themeData.checkboxTheme.fillColor?.resolve(activeStates)
?? checkboxTheme.fillColor?.resolve(activeStates)
?? _defaultFillColor.resolve(activeStates);
final Color effectiveInactiveColor = widget.fillColor?.resolve(inactiveStates)
?? _widgetFillColor.resolve(inactiveStates)
?? themeData.checkboxTheme.fillColor?.resolve(inactiveStates)
?? checkboxTheme.fillColor?.resolve(inactiveStates)
?? _defaultFillColor.resolve(inactiveStates);
final Set<MaterialState> focusedStates = states..add(MaterialState.focused);
final Color effectiveFocusOverlayColor = widget.overlayColor?.resolve(focusedStates)
?? widget.focusColor
?? themeData.checkboxTheme.overlayColor?.resolve(focusedStates)
?? checkboxTheme.overlayColor?.resolve(focusedStates)
?? themeData.focusColor;
final Set<MaterialState> hoveredStates = states..add(MaterialState.hovered);
final Color effectiveHoverOverlayColor = widget.overlayColor?.resolve(hoveredStates)
?? widget.hoverColor
?? themeData.checkboxTheme.overlayColor?.resolve(hoveredStates)
?? checkboxTheme.overlayColor?.resolve(hoveredStates)
?? themeData.hoverColor;
final Set<MaterialState> activePressedStates = activeStates..add(MaterialState.pressed);
final Color effectiveActivePressedOverlayColor = widget.overlayColor?.resolve(activePressedStates)
?? themeData.checkboxTheme.overlayColor?.resolve(activePressedStates)
?? checkboxTheme.overlayColor?.resolve(activePressedStates)
?? effectiveActiveColor.withAlpha(kRadialReactionAlpha);
final Set<MaterialState> inactivePressedStates = inactiveStates..add(MaterialState.pressed);
final Color effectiveInactivePressedOverlayColor = widget.overlayColor?.resolve(inactivePressedStates)
?? themeData.checkboxTheme.overlayColor?.resolve(inactivePressedStates)
?? checkboxTheme.overlayColor?.resolve(inactivePressedStates)
?? effectiveActiveColor.withAlpha(kRadialReactionAlpha);
final Color effectiveCheckColor = widget.checkColor
?? themeData.checkboxTheme.checkColor?.resolve(states)
?? checkboxTheme.checkColor?.resolve(states)
?? const Color(0xFFFFFFFF);
return Semantics(
......@@ -481,7 +483,7 @@ class _CheckboxState extends State<Checkbox> with TickerProviderStateMixin, Togg
..reactionColor = effectiveActivePressedOverlayColor
..hoverColor = effectiveHoverOverlayColor
..focusColor = effectiveFocusOverlayColor
..splashRadius = widget.splashRadius ?? themeData.checkboxTheme.splashRadius ?? kRadialReactionRadius
..splashRadius = widget.splashRadius ?? checkboxTheme.splashRadius ?? kRadialReactionRadius
..downPosition = downPosition
..isFocused = states.contains(MaterialState.focused)
..isHovered = states.contains(MaterialState.hovered)
......@@ -490,10 +492,10 @@ class _CheckboxState extends State<Checkbox> with TickerProviderStateMixin, Togg
..checkColor = effectiveCheckColor
..value = value
..previousValue = _previousValue
..shape = widget.shape ?? themeData.checkboxTheme.shape ?? const RoundedRectangleBorder(
..shape = widget.shape ?? checkboxTheme.shape ?? const RoundedRectangleBorder(
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() {
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 {
......
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