Skip to content

Commit d2e97da

Browse files
authored
[material_ui] Add Checkbox.markInsets (flutter#12690)
This PR ports my changes from flutter#188258 to flutter/packages, as part of flutter#188444 Adds `Checkbox.markInsets` and `CheckboxThemeData.markInsets`, which inset the check mark and the indeterminate dash within the checkbox without changing the size of the box itself. The mark and its stroke shrink proportionally, and the mark is not painted if the insets leave no room. Fixes flutter#188031 ## Tests Added to `packages/material_ui/test/checkbox_test.dart`: - `Checkbox.markInsets` defaults to null - Checkbox with no `markInsets` paints the check at full stroke width (backward-compat) - `Checkbox.markInsets` shrinks the check mark and its stroke - `Checkbox.markInsets` shrinks the indeterminate dash - `Checkbox.markInsets` larger than the box collapses the mark without crashing - `Checkbox.markInsets` falls back to `CheckboxThemeData.markInsets` - `Checkbox.markInsets` overrides `CheckboxThemeData.markInsets` ## Pre-Review Checklist submitting PRs. am not using AI tools. brackets, e.g. `[shared_preferences]` and the [repository CHANGELOG style], or I have commented below to indicate which documented exception this PR falls under[^1]. indicate which [test exemption] this PR falls under[^1]. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent e71b3fd commit d2e97da

5 files changed

Lines changed: 319 additions & 10 deletions

File tree

‎packages/material_ui/lib/src/checkbox.dart‎

Lines changed: 87 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
library;
1212

1313
import 'package:cupertino_ui/cupertino_ui.dart';
14+
import 'package:flutter/foundation.dart' show clampDouble;
1415

1516
import 'checkbox_theme.dart';
1617
import 'color_scheme.dart';
@@ -115,6 +116,7 @@ class Checkbox extends StatefulWidget {
115116
this.side,
116117
this.isError = false,
117118
this.semanticLabel,
119+
this.markInsets,
118120
}) : _checkboxType = _CheckboxType.material,
119121
assert(tristate || value != null);
120122

@@ -129,9 +131,9 @@ class Checkbox extends StatefulWidget {
129131
///
130132
/// If a [CupertinoCheckbox] is created, the following parameters are ignored:
131133
/// [fillColor], [hoverColor], [overlayColor], [splashRadius],
132-
/// [materialTapTargetSize], [visualDensity], [isError]. However, [shape] and
133-
/// [side] will still affect the [CupertinoCheckbox] and should be handled if
134-
/// native fidelity is important.
134+
/// [materialTapTargetSize], [visualDensity], [isError], [markInsets]. However,
135+
/// [shape] and [side] will still affect the [CupertinoCheckbox] and should be
136+
/// handled if native fidelity is important.
135137
///
136138
/// The target platform is based on the current [Theme]: [ThemeData.platform].
137139
const Checkbox.adaptive({
@@ -155,6 +157,7 @@ class Checkbox extends StatefulWidget {
155157
this.side,
156158
this.isError = false,
157159
this.semanticLabel,
160+
this.markInsets,
158161
}) : _checkboxType = _CheckboxType.adaptive,
159162
assert(tristate || value != null);
160163

@@ -406,6 +409,31 @@ class Checkbox extends StatefulWidget {
406409
/// {@endtemplate}
407410
final String? semanticLabel;
408411

412+
/// {@template material_ui.checkbox.markInsets}
413+
/// The insets applied around the check mark when the checkbox is checked or
414+
/// in its indeterminate (tristate) state.
415+
///
416+
/// This insets the check mark inward; the size of the checkbox box itself
417+
/// is not affected. The check mark and its stroke shrink to fit the
418+
/// remaining space, so the mark stays proportional. Its size is the smaller
419+
/// of the remaining width and height, and it is centered in the remaining
420+
/// space, so an inset on one axis also shrinks it on the other. For example,
421+
/// `EdgeInsets.only(left: 9.0)` draws a half-size mark centered in the right
422+
/// half of the box.
423+
///
424+
/// On iOS and macOS, when using [Checkbox.adaptive], this property has no
425+
/// effect, because the checkbox delegates to [CupertinoCheckbox], which does
426+
/// not support insets.
427+
///
428+
/// Must be non-negative. If the horizontal or vertical insets are
429+
/// greater than or equal to the checkbox size, the check mark or indeterminate
430+
/// dash is not painted.
431+
///
432+
/// If null, [CheckboxThemeData.markInsets] is used. If that is also null,
433+
/// it defaults to [EdgeInsets.zero] and the check mark renders at full size.
434+
/// {@endtemplate}
435+
final EdgeInsets? markInsets;
436+
409437
/// The width of a checkbox widget.
410438
static const double width = 18.0;
411439

@@ -617,6 +645,8 @@ class _CheckboxState extends State<Checkbox> with TickerProviderStateMixin, Togg
617645
final double effectiveSplashRadius =
618646
widget.splashRadius ?? checkboxTheme.splashRadius ?? defaults.splashRadius!;
619647

648+
final EdgeInsets markInsets = widget.markInsets ?? checkboxTheme.markInsets ?? EdgeInsets.zero;
649+
620650
return Semantics(
621651
label: widget.semanticLabel,
622652
checked: widget.value ?? false,
@@ -646,6 +676,7 @@ class _CheckboxState extends State<Checkbox> with TickerProviderStateMixin, Togg
646676
..previousValue = _previousValue
647677
..shape = widget.shape ?? checkboxTheme.shape ?? defaults.shape!
648678
..activeSide = activeSide
679+
..markInsets = markInsets
649680
..inactiveSide = inactiveSide,
650681
),
651682
);
@@ -696,6 +727,17 @@ class _CheckboxPainter extends ToggleablePainter {
696727
notifyListeners();
697728
}
698729

730+
EdgeInsets get markInsets => _markInsets;
731+
EdgeInsets _markInsets = EdgeInsets.zero;
732+
set markInsets(EdgeInsets value) {
733+
assert(value.isNonNegative, 'markInsets must be non-negative.');
734+
if (_markInsets == value) {
735+
return;
736+
}
737+
_markInsets = value;
738+
notifyListeners(); // triggers repaint when it changes
739+
}
740+
699741
BorderSide get activeSide => _activeSide!;
700742
BorderSide? _activeSide;
701743
set activeSide(BorderSide value) {
@@ -741,6 +783,27 @@ class _CheckboxPainter extends ToggleablePainter {
741783
..strokeWidth = _kStrokeWidth;
742784
}
743785

786+
// The square that the check mark or the indeterminate dash is drawn in.
787+
//
788+
// The square is inset by [markInsets] and then scaled down uniformly to fit
789+
// whatever space is left, so that the mark keeps its aspect ratio no matter
790+
// how asymmetric the insets are. It is centered in the space the insets leave
791+
// behind. Returns null when the insets leave no room to draw in.
792+
Rect? _markSquare() {
793+
final double innerWidth = clampDouble(_kEdgeSize - _markInsets.horizontal, 0.0, _kEdgeSize);
794+
final double innerHeight = clampDouble(_kEdgeSize - _markInsets.vertical, 0.0, _kEdgeSize);
795+
if (innerWidth <= 0.0 || innerHeight <= 0.0) {
796+
return null;
797+
}
798+
final side = innerWidth < innerHeight ? innerWidth : innerHeight;
799+
return Rect.fromLTWH(
800+
_markInsets.left + (innerWidth - side) / 2.0,
801+
_markInsets.top + (innerHeight - side) / 2.0,
802+
side,
803+
side,
804+
);
805+
}
806+
744807
void _drawBox(Canvas canvas, Rect outer, Paint paint, BorderSide? side) {
745808
if (shape.preferPaintInterior) {
746809
shape.paintInterior(canvas, outer, paint);
@@ -756,10 +819,18 @@ class _CheckboxPainter extends ToggleablePainter {
756819
assert(t >= 0.0 && t <= 1.0);
757820
// As t goes from 0.0 to 1.0, animate the two check mark strokes from the
758821
// short side to the long side.
822+
final Rect? square = _markSquare();
823+
if (square == null) {
824+
return;
825+
}
826+
// Shrink the stroke by the same factor so a smaller mark doesn't look chunky.
827+
paint.strokeWidth = _kStrokeWidth * (square.width / _kEdgeSize);
828+
759829
final path = Path();
760-
const start = Offset(_kEdgeSize * 0.15, _kEdgeSize * 0.45);
761-
const mid = Offset(_kEdgeSize * 0.4, _kEdgeSize * 0.7);
762-
const end = Offset(_kEdgeSize * 0.85, _kEdgeSize * 0.25);
830+
final start = Offset(square.left + square.width * 0.15, square.top + square.height * 0.45);
831+
final mid = Offset(square.left + square.width * 0.4, square.top + square.height * 0.7);
832+
final end = Offset(square.left + square.width * 0.85, square.top + square.height * 0.25);
833+
763834
if (t < 0.5) {
764835
final double strokeT = t * 2.0;
765836
final Offset drawMid = Offset.lerp(start, mid, strokeT)!;
@@ -779,9 +850,16 @@ class _CheckboxPainter extends ToggleablePainter {
779850
assert(t >= 0.0 && t <= 1.0);
780851
// As t goes from 0.0 to 1.0, animate the horizontal line from the
781852
// mid point outwards.
782-
const start = Offset(_kEdgeSize * 0.2, _kEdgeSize * 0.5);
783-
const mid = Offset(_kEdgeSize * 0.5, _kEdgeSize * 0.5);
784-
const end = Offset(_kEdgeSize * 0.8, _kEdgeSize * 0.5);
853+
final Rect? square = _markSquare();
854+
if (square == null) {
855+
return;
856+
}
857+
paint.strokeWidth = _kStrokeWidth * (square.width / _kEdgeSize);
858+
859+
final start = Offset(square.left + square.width * 0.2, square.top + square.height * 0.5);
860+
final mid = Offset(square.left + square.width * 0.5, square.top + square.height * 0.5);
861+
final end = Offset(square.left + square.width * 0.8, square.top + square.height * 0.5);
862+
785863
final Offset drawStart = Offset.lerp(start, mid, 1.0 - t)!;
786864
final Offset drawEnd = Offset.lerp(mid, end, t)!;
787865
canvas.drawLine(origin + drawStart, origin + drawEnd, paint);

‎packages/material_ui/lib/src/checkbox_theme.dart‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class CheckboxThemeData with Diagnosticable {
4848
this.visualDensity,
4949
this.shape,
5050
this.side,
51+
this.markInsets,
5152
});
5253

5354
/// {@macro material_ui.checkbox.mouseCursor}
@@ -102,6 +103,9 @@ class CheckboxThemeData with Diagnosticable {
102103
/// If specified, overrides the default value of [Checkbox.side].
103104
final BorderSide? side;
104105

106+
/// {@macro material_ui.checkbox.markInsets}
107+
final EdgeInsets? markInsets;
108+
105109
/// Creates a copy of this object but with the given fields replaced with the
106110
/// new values.
107111
CheckboxThemeData copyWith({
@@ -114,6 +118,7 @@ class CheckboxThemeData with Diagnosticable {
114118
VisualDensity? visualDensity,
115119
OutlinedBorder? shape,
116120
BorderSide? side,
121+
EdgeInsets? markInsets,
117122
}) {
118123
return CheckboxThemeData(
119124
mouseCursor: mouseCursor ?? this.mouseCursor,
@@ -125,6 +130,7 @@ class CheckboxThemeData with Diagnosticable {
125130
visualDensity: visualDensity ?? this.visualDensity,
126131
shape: shape ?? this.shape,
127132
side: side ?? this.side,
133+
markInsets: markInsets ?? this.markInsets,
128134
);
129135
}
130136

@@ -150,6 +156,7 @@ class CheckboxThemeData with Diagnosticable {
150156
visualDensity: t < 0.5 ? a?.visualDensity : b?.visualDensity,
151157
shape: ShapeBorder.lerp(a?.shape, b?.shape, t) as OutlinedBorder?,
152158
side: _lerpSides(a?.side, b?.side, t),
159+
markInsets: EdgeInsets.lerp(a?.markInsets, b?.markInsets, t),
153160
);
154161
}
155162

@@ -164,6 +171,7 @@ class CheckboxThemeData with Diagnosticable {
164171
visualDensity,
165172
shape,
166173
side,
174+
markInsets,
167175
);
168176

169177
@override
@@ -183,7 +191,8 @@ class CheckboxThemeData with Diagnosticable {
183191
other.materialTapTargetSize == materialTapTargetSize &&
184192
other.visualDensity == visualDensity &&
185193
other.shape == shape &&
186-
other.side == side;
194+
other.side == side &&
195+
other.markInsets == markInsets;
187196
}
188197

189198
@override
@@ -226,6 +235,7 @@ class CheckboxThemeData with Diagnosticable {
226235
);
227236
properties.add(DiagnosticsProperty<OutlinedBorder>('shape', shape, defaultValue: null));
228237
properties.add(DiagnosticsProperty<BorderSide>('side', side, defaultValue: null));
238+
properties.add(DiagnosticsProperty<EdgeInsets>('markInsets', markInsets, defaultValue: null));
229239
}
230240

231241
// Special case because BorderSide.lerp() doesn't support null arguments
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
changelog: |
2+
- Adds `Checkbox.markInsets` and `CheckboxThemeData.markInsets`, which inset the
3+
check mark (and the indeterminate dash) within the checkbox without changing the
4+
size of the box itself. The mark and its stroke shrink proportionally.
5+
version: minor

0 commit comments

Comments
 (0)