Skip to content

Commit

Permalink
Fix Local CheckBoxTheme not being inherited by CheckBox Widget (#…
Browse files Browse the repository at this point in the history
…97715)
  • Loading branch information
TahaTesser authored Feb 3, 2022
1 parent ab89ce2 commit 2e10b46
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 13 deletions.
28 changes: 15 additions & 13 deletions packages/flutter/lib/src/material/checkbox.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'package:flutter/widgets.dart';

import 'checkbox_theme.dart';
import 'constants.dart';
import 'debug.dart';
import 'material_state.dart';
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
});

Expand All @@ -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(
Expand All @@ -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)
Expand All @@ -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),
),
);
}
Expand Down
35 changes: 35 additions & 0 deletions packages/flutter/test/material/checkbox_theme_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 2e10b46

Please sign in to comment.