我尝试在自定义画家上绘制许多TextSpan。一切正常,但是当我尝试缩放画家时,它变得非常慢。什么方式可以加快速度?
使用自定义绘画缩放小部件:
@override
Widget build(BuildContext context) {
return GestureTransformable(
size: sz,
initialScale: 0.3,
maxScale: 40.0,
minScale: 0.01,
boundaryRect: Rect.fromLTWH(
-sz.width * 10 / 2,
-sz.height * 10 / 2,
sz.width * 10,
sz.height * 10,
),
child: RepaintBoundary(
child: CustomPaint(
painter: CustomPaintSpans(),
),
)
);
}
和自定义画家:
@override
void paint(Canvas canvas, Size size) {
double delta = 10.0;
double pixelSize = 10.0;
for (int j = 0; j < 100; ++j) {
for (int i = 0; i < 100; ++i) {
double startX = delta + i * (pixelSize + 1);
double startY = delta + j * (pixelSize + 1);
////////
TextSpan textSpan = new TextSpan(
text: "$i",
style: TextStyle(fontSize: 1, color: Colors.grey, fontWeight: FontWeight.w800));
TextPainter tp = TextPainter(
textScaleFactor: 10,
text: textSpan,
textAlign: TextAlign.center,
textDirection: TextDirection.ltr);
tp.layout();
Offset offset;
offset = Offset(startX - pixelSize * 0.5, startY - pixelSize * 0.5);
tp.paint(canvas, offset);
///////////
}
}