render method

  1. @override
void render(
  1. Canvas canvas
)
override

渲染精灵

Implementation

@override
void render(Canvas canvas) {
  /// 画布暂存
  canvas.save();

  /// 将子精灵转换为相对坐标
  if (parent == null) {
    canvas.translate(position.x, position.y);
  } else {
    canvas.translate(position.x - parent!.size.width / 2, position.y - parent!.size.height / 2);
  }

  /// 文本内容
  ui.ParagraphBuilder pb = ui.ParagraphBuilder(ui.ParagraphStyle(
    textAlign: TextAlign.center,
    fontStyle: FontStyle.normal,
    fontSize: this.fontSize,
  ))
    ..pushStyle(ui.TextStyle(color: this.color))
    ..addText(this.text);

  ui.ParagraphConstraints pc = ui.ParagraphConstraints(width: size.width);
  ui.Paragraph paragraph = pb.build()..layout(pc);

  canvas.drawParagraph(paragraph, Offset(-paragraph.width / 2, -paragraph.height / 2));

  /// 精灵矩形边界
  var paint = new Paint()..color = background;
  canvas.drawRect(
      Rect.fromLTWH(-paragraph.width / 2, -paragraph.height / 2, paragraph.width, paragraph.height), paint);

  ///恢复画布
  canvas.restore();
}