如何在 flutter 中绘制或绘制全角矩形?

How to draw or paint a fullwidth rectangle in flutter?

我想画一个自动占据屏幕宽度的圆弧。 为此,我尝试了以下代码:

class ArcPainter extends CustomPainter   {


  @override
  void paint(Canvas canvas, Size size) {
    // create a bounding square, based on the centre and radius of the arc
    Rect rect = new Rect.fromPoints(new Offset(0.0, -45.0),new Offset(372.0, 45.0));


    // a fancy rainbow gradient
    final Gradient gradient = new RadialGradient(
      colors: <Color>[
        Colors.white.withOpacity(1.0),

      ],
      stops: [
        0.0,
      ],
    );

    // create the Shader from the gradient and the bounding square
    final Paint paint = new Paint()..shader = gradient.createShader(rect);

    // and draw an arc
    canvas.drawArc(rect, 0.0 , pi, true, paint);
  }

  @override
  bool shouldRepaint(ArcPainter oldDelegate) {
    return true;
  }
}

这是我的工作,但我想要 Rect rect = new Rect.fromPoints(new Offset(0.0, -45.0),new Offset(372.0, 45.0));占据整个屏幕宽度。

我已经试过了 Rect rect = new Rect.fromLTWH(0.0, -45.0, size.width, 45.0);但在这种情况下弧消失了。

所以我发现了错误:

调用此 class 时我没有给出任何尺寸,因此 size.width 和 size.height 变为 0。

我更改了主构建中的代码:

new Row(
children: <Widget>[
    new CustomPaint(
    painter: new ArcPainter(),
  ],
),

收件人:

new Row(
children: <Widget>[
  new Container(
  width: screen_width * 1,
  height: screen_height * 0.05,
  child: 
    new CustomPaint(
    painter: new ArcPainter(),
      )
    )
  ],
),

最后在 ArcPainter 中:

Rect rect = new Rect.fromPoints(new Offset(0.0, - size.height),new Offset(size.width, size.height));

注意: 双 screen_width = MediaQuery.of(上下文).size.width; 双 screen_height = MediaQuery.of(上下文).size.height;