drawText method
Implementation
void drawText(Canvas canvas, Size size) {
// 第一步
final paragraphStyle = ui.ParagraphStyle(
// 字体方向,有些国家语言是从右往左排版的
textDirection: TextDirection.ltr,
// 字体对齐方式
textAlign: TextAlign.justify,
fontSize: 10,
maxLines: 1,
// 字体超出大小时显示的提示
ellipsis: '...',
fontStyle: FontStyle.italic,
// 当我们设置[TextStyle.height]时 这个高度是否应用到字体顶部和底部
textHeightBehavior: const TextHeightBehavior(
applyHeightToFirstAscent: true, applyHeightToLastDescent: true));
// 第二步 与第三步
final paragraphBuilder = ui.ParagraphBuilder(paragraphStyle)
..addText('ParagraphBuilder');
// 第四步
var paragraph = paragraphBuilder.build();
// 第五步
paragraph.layout(const ui.ParagraphConstraints(width: 100));
// 画一个辅助矩形(可以通过paragraph.width和paragraph.height来获取绘制文字的宽高)
// canvas.drawRect(
// Rect.fromLTRB(50, 50, 50 + paragraph.width, 50 + paragraph.height),
// _bgRectPaint);
// 第六步
canvas.drawParagraph(paragraph, Offset(0, size.height - 15));
}