四分之一圆形状与扑在一个容器

时间:2018-08-05 00:30:57

标签: containers flutter shape geometry

我想有一个四分之一圆形状的容器,想想整个披萨的四分之一片。

我该如何实现?基本上,我想使用堆栈小部件将其放置在右下角位置的另一个容器的顶部,使圆形部分朝内,并且当然与底部容器的右下角所在的角度匹配。

谢谢。

1 个答案:

答案 0 :(得分:1)

您可以结合使用CustomPainterClipRect画一个圆并对其进行裁剪。

enter image description here

enum CircleAlignment {
  topLeft,
  topRight,
  bottomLeft,
  bottomRight,
}

class QuarterCircle extends StatelessWidget {
  final CircleAlignment circleAlignment;
  final Color color;

  const QuarterCircle({
    this.color = Colors.grey,
    this.circleAlignment = CircleAlignment.topLeft,
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SizedBox.expand(
      child: ClipRect(
        child: CustomPaint(
          painter: QuarterCirclePainter(
            circleAlignment: circleAlignment,
            color: color,
          ),
        ),
      ),
    );
  }
}

class QuarterCirclePainter extends CustomPainter {
  final CircleAlignment circleAlignment;
  final Color color;

  const QuarterCirclePainter({this.circleAlignment, this.color});

  @override
  void paint(Canvas canvas, Size size) {
    final radius = math.min(size.height, size.width);
    final offset = circleAlignment == CircleAlignment.topLeft
        ? Offset(.0, .0)
        : circleAlignment == CircleAlignment.topRight
            ? Offset(size.width, .0)
            : circleAlignment == CircleAlignment.bottomLeft
                ? Offset(.0, size.height)
                : Offset(size.width, size.height);
    canvas.drawCircle(offset, radius, Paint()..color = color);
  }

  @override
  bool shouldRepaint(QuarterCirclePainter oldDelegate) {
    return color == oldDelegate.color &&
        circleAlignment == oldDelegate.circleAlignment;
  }
}

您可以通过

使用
QuarterCircle(
  circleAlignment: CircleAlignment.bottomLeft,
),