html canvas drawrect 变形,flutter - Canvas.drawRect() with Rect.fromLTRB() not working - Stack Overflo...

在Flutter应用中尝试使用CustomPainter绘制四个不同颜色的小方块于一个大方块内,但结果四个方块只显示在左上角。问题出在绘制矩形的坐标上。示例代码展示了CustomPainter的实现和预期效果与实际效果的对比。
摘要由CSDN通过智能技术生成

In my flutter app, I tried to use CustomPainter, to draw custom shapes.

Here, I want to draw 4 small squares, in different colors, in a bigger square (here in yellow).

I expected to see the 4 squares, but for some reason, the 4 squares are on the top left of the CustomPainter.

Here is my sample app:

import 'package:flutter/material.dart';

void main() {

runApp(MyApp());

}

class MyApp extends StatelessWidget {

@override

Widget build(BuildContext context) {

return MaterialApp(

title: 'Flutter Demo',

home: MyHomePage(),

);

}

}

class MyHomePage extends StatefulWidget {

@override

_MyHomePageState createState() => _MyHomePageState();

}

class _MyHomePageState extends State {

@override

Widget build(BuildContext context) {

return Scaffold(

backgroundColor: Colors.black,

body: Center(

child: Container(

color: Colors.amber,

width: 180,

height: 180,

child: CustomPaint(

painter: TestCustomPainter(),

),

),

),

);

}

}

class TestCustomPainter extends CustomPainter {

Paint _paintWhite = Paint()

..color = Colors.white

..isAntiAlias = true;

Paint _paintBlue = Paint()

..color = Colors.blue

..isAntiAlias = true;

Paint _paintRed = Paint()

..color = Colors.red

..isAntiAlias = true;

Paint _paintGreen = Paint()

..color = Colors.green

..isAntiAlias = true;

@override

void paint(Canvas canvas, Size size) {

canvas.drawRect(Rect.fromLTRB(0, 0, 90, 90), _paintWhite);

canvas.drawRect(Rect.fromLTRB(90, 0, 0, 90), _paintBlue);

canvas.drawRect(Rect.fromLTRB(0, 90, 90, 0), _paintRed);

canvas.drawRect(Rect.fromLTRB(90, 90, 0, 0), _paintGreen);

}

@override

bool shouldRepaint(covariant CustomPainter oldDelegate) => true;

}

And here is what I get:

swZte.png

What did I do wrong?

Thanks.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值