Пример #1
0
        public override void paint(PaintingContext context, Offset offset)
        {
            base.paint(context, offset);
            Offset      bottomRight = size.bottomRight(offset);
            Rect        outer       = Rect.fromLTRB(offset.dx, offset.dy, bottomRight.dx, bottomRight.dy);
            Rect        center      = outer.deflate(horizontalBorderSide.width / 2.0f);
            const float sweepAngle  = Mathf.PI / 2.0f;

            RRect rrect = RRect.fromRectAndCorners(
                center,
                topLeft: borderRadius.topLeft,
                topRight: borderRadius.topRight,
                bottomLeft: borderRadius.bottomLeft,
                bottomRight: borderRadius.bottomRight
                ).scaleRadii();

            Rect tlCorner = Rect.fromLTWH(
                rrect.left,
                rrect.top,
                rrect.tlRadiusX * 2.0f,
                rrect.tlRadiusY * 2.0f
                );
            Rect blCorner = Rect.fromLTWH(
                rrect.left,
                rrect.bottom - (rrect.blRadiusY * 2.0f),
                rrect.blRadiusX * 2.0f,
                rrect.blRadiusY * 2.0f
                );
            Rect trCorner = Rect.fromLTWH(
                rrect.right - (rrect.trRadiusX * 2.0f),
                rrect.top,
                rrect.trRadiusX * 2.0f,
                rrect.trRadiusY * 2.0f
                );
            Rect brCorner = Rect.fromLTWH(
                rrect.right - (rrect.brRadiusX * 2.0f),
                rrect.bottom - (rrect.brRadiusY * 2.0f),
                rrect.brRadiusX * 2.0f,
                rrect.brRadiusY * 2.0f
                );

            Paint leadingPaint = leadingBorderSide.toPaint();

            switch (textDirection)
            {
            case TextDirection.ltr: {
                if (isLastButton)
                {
                    Path leftPath = new Path();
                    leftPath.moveTo(rrect.left, rrect.bottom + leadingBorderSide.width / 2);
                    leftPath.lineTo(rrect.left, rrect.top - leadingBorderSide.width / 2);
                    context.canvas.drawPath(leftPath, leadingPaint);

                    Paint endingPaint = trailingBorderSide.toPaint();
                    Path  endingPath  = new Path();
                    endingPath.moveTo(rrect.left + horizontalBorderSide.width / 2.0f, rrect.top);
                    endingPath.lineTo(rrect.right - rrect.trRadiusX, rrect.top);
                    endingPath.addArc(trCorner, Mathf.PI * 3.0f / 2.0f, sweepAngle);
                    endingPath.lineTo(rrect.right, rrect.bottom - rrect.brRadiusY);
                    endingPath.addArc(brCorner, 0, sweepAngle);
                    endingPath.lineTo(rrect.left + horizontalBorderSide.width / 2.0f, rrect.bottom);
                    context.canvas.drawPath(endingPath, endingPaint);
                }
                else if (isFirstButton)
                {
                    Path leadingPath = new Path();
                    leadingPath.moveTo(outer.right, rrect.bottom);
                    leadingPath.lineTo(rrect.left + rrect.blRadiusX, rrect.bottom);
                    leadingPath.addArc(blCorner, Mathf.PI / 2.0f, sweepAngle);
                    leadingPath.lineTo(rrect.left, rrect.top + rrect.tlRadiusY);
                    leadingPath.addArc(tlCorner, Mathf.PI, sweepAngle);
                    leadingPath.lineTo(outer.right, rrect.top);
                    context.canvas.drawPath(leadingPath, leadingPaint);
                }
                else
                {
                    Path leadingPath = new Path();
                    leadingPath.moveTo(rrect.left, rrect.bottom + leadingBorderSide.width / 2);
                    leadingPath.lineTo(rrect.left, rrect.top - leadingBorderSide.width / 2);
                    context.canvas.drawPath(leadingPath, leadingPaint);

                    Paint horizontalPaint = horizontalBorderSide.toPaint();
                    Path  horizontalPaths = new Path();
                    horizontalPaths.moveTo(rrect.left + horizontalBorderSide.width / 2.0f, rrect.top);
                    horizontalPaths.lineTo(outer.right - rrect.trRadiusX, rrect.top);
                    horizontalPaths.moveTo(rrect.left + horizontalBorderSide.width / 2.0f + rrect.tlRadiusX,
                                           rrect.bottom);
                    horizontalPaths.lineTo(outer.right - rrect.trRadiusX, rrect.bottom);
                    context.canvas.drawPath(horizontalPaths, horizontalPaint);
                }

                break;
            }

            case TextDirection.rtl: {
                if (isLastButton)
                {
                    Path leadingPath = new Path();
                    leadingPath.moveTo(rrect.right, rrect.bottom + leadingBorderSide.width / 2);
                    leadingPath.lineTo(rrect.right, rrect.top - leadingBorderSide.width / 2);
                    context.canvas.drawPath(leadingPath, leadingPaint);

                    Paint endingPaint = trailingBorderSide.toPaint();
                    Path  endingPath  = new Path();
                    endingPath.moveTo(rrect.right - horizontalBorderSide.width / 2.0f, rrect.top);
                    endingPath.lineTo(rrect.left + rrect.tlRadiusX, rrect.top);
                    endingPath.addArc(tlCorner, Mathf.PI * 3.0f / 2.0f, -sweepAngle);
                    endingPath.lineTo(rrect.left, rrect.bottom - rrect.blRadiusY);
                    endingPath.addArc(blCorner, Mathf.PI, -sweepAngle);
                    endingPath.lineTo(rrect.right - horizontalBorderSide.width / 2.0f, rrect.bottom);
                    context.canvas.drawPath(endingPath, endingPaint);
                }
                else if (isFirstButton)
                {
                    Path leadingPath = new Path();
                    leadingPath.moveTo(outer.left, rrect.bottom);
                    leadingPath.lineTo(rrect.right - rrect.brRadiusX, rrect.bottom);
                    leadingPath.addArc(brCorner, Mathf.PI / 2.0f, -sweepAngle);
                    leadingPath.lineTo(rrect.right, rrect.top + rrect.trRadiusY);
                    leadingPath.addArc(trCorner, 0, -sweepAngle);
                    leadingPath.lineTo(outer.left, rrect.top);
                    context.canvas.drawPath(leadingPath, leadingPaint);
                }
                else
                {
                    Path leadingPath = new Path();
                    leadingPath.moveTo(rrect.right, rrect.bottom + leadingBorderSide.width / 2);
                    leadingPath.lineTo(rrect.right, rrect.top - leadingBorderSide.width / 2);
                    context.canvas.drawPath(leadingPath, leadingPaint);

                    Paint horizontalPaint = horizontalBorderSide.toPaint();
                    Path  horizontalPaths = new Path();
                    horizontalPaths.moveTo(rrect.right - horizontalBorderSide.width / 2.0f, rrect.top);
                    horizontalPaths.lineTo(outer.left - rrect.tlRadiusX, rrect.top);
                    horizontalPaths.moveTo(rrect.right - horizontalBorderSide.width / 2.0f + rrect.trRadiusX,
                                           rrect.bottom);
                    horizontalPaths.lineTo(outer.left - rrect.tlRadiusX, rrect.bottom);
                    context.canvas.drawPath(horizontalPaths, horizontalPaint);
                }

                break;
            }
            }
        }
Пример #2
0
        public override void paint(PaintingContext context, Offset offset)
        {
            Canvas canvas = context.canvas;

            float currentValue         = this._position.value;
            float currentReactionValue = this._reaction.value;

            float visualPosition = 0f;

            switch (this.textDirection)
            {
            case TextDirection.rtl:
                visualPosition = 1.0f - currentValue;
                break;

            case TextDirection.ltr:
                visualPosition = currentValue;
                break;
            }

            Color trackColor      = this._value ? this.activeColor : CupertinoSwitchUtils._kTrackColor;
            float borderThickness =
                1.5f + (CupertinoSwitchUtils._kTrackRadius - 1.5f) * Mathf.Max(currentReactionValue, currentValue);

            Paint paint = new Paint();

            paint.color = trackColor;

            Rect trackRect = Rect.fromLTWH(
                offset.dx + (this.size.width - CupertinoSwitchUtils._kTrackWidth) / 2.0f,
                offset.dy + (this.size.height - CupertinoSwitchUtils._kTrackHeight) / 2.0f,
                CupertinoSwitchUtils._kTrackWidth,
                CupertinoSwitchUtils._kTrackHeight
                );
            RRect outerRRect = RRect.fromRectAndRadius(trackRect, Radius.circular(CupertinoSwitchUtils
                                                                                  ._kTrackRadius));
            RRect innerRRect = RRect.fromRectAndRadius(trackRect.deflate(borderThickness), Radius.circular
                                                           (CupertinoSwitchUtils._kTrackRadius));

            canvas.drawDRRect(outerRRect, innerRRect, paint);

            float currentThumbExtension = CupertinoThumbPainter.extension * currentReactionValue;
            float thumbLeft             = MathUtils.lerpFloat(
                trackRect.left + CupertinoSwitchUtils._kTrackInnerStart - CupertinoThumbPainter.radius,
                trackRect.left + CupertinoSwitchUtils._kTrackInnerEnd - CupertinoThumbPainter.radius -
                currentThumbExtension,
                visualPosition
                );
            float thumbRight = MathUtils.lerpFloat(
                trackRect.left + CupertinoSwitchUtils._kTrackInnerStart + CupertinoThumbPainter.radius +
                currentThumbExtension,
                trackRect.left + CupertinoSwitchUtils._kTrackInnerEnd + CupertinoThumbPainter.radius,
                visualPosition
                );
            float thumbCenterY = offset.dy + this.size.height / 2.0f;

            this._thumbPainter.paint(canvas, Rect.fromLTRB(
                                         thumbLeft,
                                         thumbCenterY - CupertinoThumbPainter.radius,
                                         thumbRight,
                                         thumbCenterY + CupertinoThumbPainter.radius
                                         ));
        }