Skip to content

Instantly share code, notes, and snippets.

@Indy9000
Created May 31, 2021 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Indy9000/d2e15d431e746f41885200a79a0f620a to your computer and use it in GitHub Desktop.
Save Indy9000/d2e15d431e746f41885200a79a0f620a to your computer and use it in GitHub Desktop.
animated-donut-chart-05
TextPainter measureText(
String s, TextStyle style, double maxWidth, TextAlign align) {
final span = TextSpan(text: s, style: style);
final tp = TextPainter(
text: span, textAlign: align, textDirection: TextDirection.ltr);
// ellipsis: "...");
tp.layout(minWidth: 0, maxWidth: maxWidth);
return tp;
}
Size drawTextCentered(Canvas canvas, Offset position, String text,
TextStyle style, double maxWidth, Function(Size sz) bgCb) {
final tp = measureText(text, style, maxWidth, TextAlign.center);
final pos = position + Offset(-tp.width / 2.0, -tp.height / 2.0);
bgCb(tp.size);
tp.paint(canvas, pos);
return tp.size;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment