簡體   English   中英

如何在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 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); 但是在這種情況下,電弧消失了。

所以我找出了錯誤:

調用此類時,我沒有給出任何大小,因此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));

注意:double screen_width = MediaQuery.of(context).size.width; double screen_height = MediaQuery.of(context).size.height;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM