123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- part of ct_assets_picker;
-
- class RoundedCheckbox extends StatefulWidget {
- const RoundedCheckbox({
- Key key,
- @required this.value,
- this.tristate = false,
- @required this.onChanged,
- this.activeColor,
- this.inactiveColor,
- this.checkColor,
- this.materialTapTargetSize,
- }) : assert(tristate != null),
- assert(tristate || value != null),
- super(key: key);
-
- final bool value;
-
- final ValueChanged<bool> onChanged;
-
- final Color activeColor;
-
- final Color inactiveColor;
-
- final Color checkColor;
-
- final bool tristate;
-
- final MaterialTapTargetSize materialTapTargetSize;
-
- static const double width = 18.0;
-
- @override
- _RoundedCheckboxState createState() => _RoundedCheckboxState();
- }
-
- class _RoundedCheckboxState extends State<RoundedCheckbox>
- with TickerProviderStateMixin {
- @override
- Widget build(BuildContext context) {
- assert(debugCheckHasMaterial(context));
- final ThemeData themeData = Theme.of(context);
- Size size;
- switch (widget.materialTapTargetSize ?? themeData.materialTapTargetSize) {
- case MaterialTapTargetSize.padded:
- size = const Size(
- 2 * kRadialReactionRadius + 8.0, 2 * kRadialReactionRadius + 8.0);
- break;
- case MaterialTapTargetSize.shrinkWrap:
- size = const Size(2 * kRadialReactionRadius, 2 * kRadialReactionRadius);
- break;
- }
- final BoxConstraints additionalConstraints = BoxConstraints.tight(size);
- return _CheckboxRenderObjectWidget(
- value: widget.value,
- tristate: widget.tristate,
- activeColor: widget.activeColor ?? themeData.toggleableActiveColor,
- checkColor: widget.checkColor ?? const Color(0xFFFFFFFF),
- inactiveColor: widget.inactiveColor ??
- (widget.onChanged != null
- ? themeData.unselectedWidgetColor
- : themeData.disabledColor),
- onChanged: widget.onChanged,
- additionalConstraints: additionalConstraints,
- vsync: this,
- );
- }
- }
-
- class _CheckboxRenderObjectWidget extends LeafRenderObjectWidget {
- const _CheckboxRenderObjectWidget({
- Key key,
- @required this.value,
- @required this.tristate,
- @required this.activeColor,
- @required this.checkColor,
- @required this.inactiveColor,
- @required this.onChanged,
- @required this.vsync,
- @required this.additionalConstraints,
- }) : assert(tristate != null),
- assert(tristate || value != null),
- assert(activeColor != null),
- assert(inactiveColor != null),
- assert(vsync != null),
- super(key: key);
-
- final bool value;
- final bool tristate;
- final Color activeColor;
- final Color checkColor;
- final Color inactiveColor;
- final ValueChanged<bool> onChanged;
- final TickerProvider vsync;
- final BoxConstraints additionalConstraints;
-
- @override
- _RenderCheckbox createRenderObject(BuildContext context) => _RenderCheckbox(
- value: value,
- tristate: tristate,
- activeColor: activeColor,
- checkColor: checkColor,
- inactiveColor: inactiveColor,
- onChanged: onChanged,
- vsync: vsync,
- additionalConstraints: additionalConstraints,
- );
-
- @override
- void updateRenderObject(BuildContext context, _RenderCheckbox renderObject) {
- renderObject
- ..value = value
- ..tristate = tristate
- ..activeColor = activeColor
- ..checkColor = checkColor
- ..inactiveColor = inactiveColor
- ..onChanged = onChanged
- ..additionalConstraints = additionalConstraints
- ..vsync = vsync;
- }
- }
-
- const double _kEdgeSize = RoundedCheckbox.width;
- const Radius _kEdgeRadius = Radius.circular(123546);
- const double _kStrokeWidth = 2.0;
-
- class _RenderCheckbox extends RenderToggleable {
- _RenderCheckbox({
- bool value,
- bool tristate,
- Color activeColor,
- this.checkColor,
- Color inactiveColor,
- BoxConstraints additionalConstraints,
- ValueChanged<bool> onChanged,
- @required TickerProvider vsync,
- }) : _oldValue = value,
- super(
- value: value,
- tristate: tristate,
- activeColor: activeColor,
- inactiveColor: inactiveColor,
- onChanged: onChanged,
- additionalConstraints: additionalConstraints,
- vsync: vsync,
- );
-
- bool _oldValue;
- Color checkColor;
-
- @override
- set value(bool newValue) {
- if (newValue == value) {
- return;
- }
- _oldValue = value;
- super.value = newValue;
- }
-
- @override
- void describeSemanticsConfiguration(SemanticsConfiguration config) {
- super.describeSemanticsConfiguration(config);
- config.isChecked = value == true;
- }
-
- RRect _outerRectAt(Offset origin, double t) {
- final double inset = 1.0 - (t - 0.5).abs() * 2.0;
- final double size = _kEdgeSize - inset * _kStrokeWidth;
- final Rect rect =
- Rect.fromLTWH(origin.dx + inset, origin.dy + inset, size, size);
- return RRect.fromRectAndRadius(rect, _kEdgeRadius);
- }
-
- Color _colorAt(double t) {
- return onChanged == null
- ? inactiveColor
- : (t >= 0.25
- ? activeColor
- : Color.lerp(inactiveColor, activeColor, t * 4.0));
- }
-
- void _initStrokePaint(Paint paint) {
- paint
- ..color = checkColor
- ..style = PaintingStyle.stroke
- ..strokeWidth = _kStrokeWidth;
- }
-
- void _drawBorder(Canvas canvas, RRect outer, double t, Paint paint) {
- assert(t >= 0.0 && t <= 0.5);
- final double size = outer.width;
- final RRect inner =
- outer.deflate(math.min(size / 2.0, _kStrokeWidth + size * t));
- canvas.drawDRRect(outer, inner, paint);
- }
-
- void _drawCheck(Canvas canvas, Offset origin, double t, Paint paint) {
- assert(t >= 0.0 && t <= 1.0);
- final Path path = Path();
- const double size = _kEdgeSize - 2;
- const Offset start = Offset(size * 0.2, size * 0.55);
- const Offset mid = Offset(size * 0.45, size * 0.8);
- const Offset end = Offset(size * 0.9, size * 0.35);
- if (t < 0.5) {
- final double strokeT = t * 2.0;
- final Offset drawMid = Offset.lerp(start, mid, strokeT);
- path.moveTo(origin.dx + start.dx, origin.dy + start.dy);
- path.lineTo(origin.dx + drawMid.dx, origin.dy + drawMid.dy);
- } else {
- final double strokeT = (t - 0.5) * 2.0;
- final Offset drawEnd = Offset.lerp(mid, end, strokeT);
- path.moveTo(origin.dx + start.dx, origin.dy + start.dy);
- path.lineTo(origin.dx + mid.dx, origin.dy + mid.dy);
- path.lineTo(origin.dx + drawEnd.dx, origin.dy + drawEnd.dy);
- }
- canvas.drawPath(path, paint);
- }
-
- void _drawDash(Canvas canvas, Offset origin, double t, Paint paint) {
- assert(t >= 0.0 && t <= 1.0);
- const Offset start = Offset(_kEdgeSize * 0.2, _kEdgeSize * 0.5);
- const Offset mid = Offset(_kEdgeSize * 0.5, _kEdgeSize * 0.5);
- const Offset end = Offset(_kEdgeSize * 0.8, _kEdgeSize * 0.5);
- final Offset drawStart = Offset.lerp(start, mid, 1.0 - t);
- final Offset drawEnd = Offset.lerp(mid, end, t);
- canvas.drawLine(origin + drawStart, origin + drawEnd, paint);
- }
-
- @override
- void paint(PaintingContext context, Offset offset) {
- final Canvas canvas = context.canvas;
- paintRadialReaction(canvas, offset, size.center(Offset.zero));
-
- final Offset origin =
- offset + (size / 2.0 - const Size.square(_kEdgeSize) / 2.0 as Offset);
- final AnimationStatus status = position.status;
- final double tNormalized =
- status == AnimationStatus.forward || status == AnimationStatus.completed
- ? position.value
- : 1.0 - position.value;
-
- if (_oldValue == false || value == false) {
- final double t = value == false ? 1.0 - tNormalized : tNormalized;
- final RRect outer = _outerRectAt(origin, t);
- final Paint paint = Paint()..color = _colorAt(t);
-
- if (t <= 0.5) {
- _drawBorder(canvas, outer, t, paint);
- } else {
- canvas.drawRRect(outer, paint);
-
- _initStrokePaint(paint);
- final double tShrink = (t - 0.5) * 2.0;
- if (_oldValue == null || value == null) {
- _drawDash(canvas, origin, tShrink, paint);
- } else {
- _drawCheck(canvas, origin, tShrink, paint);
- }
- }
- } else {
- final RRect outer = _outerRectAt(origin, 1.0);
- final Paint paint = Paint()..color = _colorAt(1.0);
- canvas.drawRRect(outer, paint);
-
- _initStrokePaint(paint);
- if (tNormalized <= 0.5) {
- final double tShrink = 1.0 - tNormalized * 2.0;
- if (_oldValue == true) {
- _drawCheck(canvas, origin, tShrink, paint);
- } else {
- _drawDash(canvas, origin, tShrink, paint);
- }
- } else {
- final double tExpand = (tNormalized - 0.5) * 2.0;
- if (value == true) {
- _drawCheck(canvas, origin, tExpand, paint);
- } else {
- _drawDash(canvas, origin, tExpand, paint);
- }
- }
- }
- }
- }
|