你好,我创建了一个扩展 CustomPainter 的小部件。问题是当调用paint方法时,Size参数宽度和高度字段总是0,0我不知道如何解决这个问题。任何想法将不胜感激。这是小部件。谢谢!!
class Box extends CustomPainter {
double _top = 0.0;
double _left = 0.0;
double _width = 0.0;
double _height = 0.0;
String _text;
Box(this._top, this._left, this._width, this._height, this._text);
@override
void paint(Canvas canvas, Size size) {
canvas.drawRect(
new Rect.fromLTWH(_left, _top, _width, _height),
new Paint()
..style = PaintingStyle.stroke
..strokeWidth = 2.0
..color = Colors.blue // new Color(0xFF0099FF),
);
}
@override
bool shouldRepaint(Box oldDelegate) {
return false;
}
}
并这样使用它:
new Positioned(
left: 0.0,
top: 0.0,
child: new Container(
child:new CustomPaint(
painter: new Box(block.boundingBox.top.toDouble(), block.boundingBox.left.toDouble(),
block.boundingBox.width.toDouble(), block.boundingBox.height.toDouble(),
block.text)
))
)
再次感谢您的想法!