【问题标题】:How to draw an arc with different start and end thickness in flutter如何在颤动中绘制具有不同开始和结束厚度的圆弧
【发布时间】:2020-02-01 09:17:17
【问题描述】:

我想绘制一条具有不同起点和终点厚度的弧线,如下图所示。

【问题讨论】:

  • 你可以使用两个不同半径和不同端点的圆
  • @meditat: 如何绘制两个不同半径和不同端点的圆?

标签: android ios flutter dart flutter-layout


【解决方案1】:

逻辑是使用两个圆圈一个接一个。顶部的圆圈向右移动。您可以通过 CustomPainter 来实现。

Paint backCirclePaint = Paint()
  ..style = PaintingStyle.fill
  ..color = Colors.blue;

Paint frontCirclePaint = Paint()
  ..style = PaintingStyle.fill
  ..color = Colors.white;

canvas.drawArc(
  Rect.fromCircle(center: Offset(size.width/2, size.height/2), radius: 100.0), 
  0.0, -(2 * pi * percentage) / 100, true, backCirclePaint);

// percentage is responsible for the amount(angle) of arc you want to build.

canvas.drawArc(
  Rect.fromCircle(center: Offset(size.width/2 + 10.0, size.height/2), radius: 90.0), 
  0.0, -(2 * pi * percentage) / 100, true, frontCirclePaint);

【讨论】:

    【解决方案2】:

    你可以用两个半圆来实现,我做了一个演示,请检查下面的代码是否适合你

    半圆类

    class CustomHalfCircleClipper extends CustomClipper<Path> {
      @override
      Path getClip(Size size) {
        final Path path = new Path();
        path.lineTo(0.0, size.height / 2);
        path.lineTo(size.width, size.height / 2);
        path.lineTo(size.width, 0);
        return path;
      }
    
      @override
      bool shouldReclip(CustomClipper<Path> oldClipper) {
        return true;
      }
    }
    

    构建方法

    @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('Demo'),
          ),
          body: Container(
            padding: EdgeInsets.all(10),
            child: Stack(
              children: <Widget>[
                ClipPath(
                  clipper: new CustomHalfCircleClipper(),
                  child: new Container(
                    height: MediaQuery.of(context).size.width,
                    width: MediaQuery.of(context).size.width,
                    decoration: new BoxDecoration(
                        color: Colors.blue,
                        borderRadius: BorderRadius.circular(
                            MediaQuery.of(context).size.width / 2)),
                  ),
                ),
                Positioned(
                  bottom: 0,
                  right: 1,
                  left: 40,
                  child: ClipPath(
                    clipper: new CustomHalfCircleClipper(),
                    child: new Container(
                      height: MediaQuery.of(context).size.width - 20,
                      width: MediaQuery.of(context).size.width - 20,
                      decoration: new BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(
                              (MediaQuery.of(context).size.width - 20) / 2)),
                    ),
                  ),
                )
              ],
            ),
          ),
        );
      }
    

    【讨论】:

    • @KailashChouhan:我需要计算这种路径的逻辑。不仅是半圆,圆弧也会根据不同的起点和终点角度而变化。此外,我不使用小部件来显示路径。我需要使用自定义画家在画布中绘制路径
    猜你喜欢
    • 2014-12-07
    • 1970-01-01
    • 1970-01-01
    • 2015-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-26
    相关资源
    最近更新 更多