how to add text on pie chart slice in CustomPaint(Canvas) widget in flutter

298

To paint text in flutter using CustomPainter you need to use TextSpan and TextPainter like so:

TextSpan span = new TextSpan(style: new TextStyle(color: Colors.blue[800]), text: yourText);
TextPainter tp = new TextPainter(text: span, textAlign: TextAlign.left, textDirection: TextDirection.ltr);
tp.layout();
tp.paint(canvas, new Offset(5.0, 5.0));
Share:
298
Hardik K
Author by

Hardik K

Updated on January 04, 2023

Comments

  • Hardik K
    Hardik K over 1 year

    enter image description here

    I have to add text on pie chart slices in canvas circle drawing and i have 3 slices in pie chart (cash/card/bill) and i want to set that text with their percentage on pie chart particular slice

    note :- i completed drawing this chart now i only have to add text on slices

    • Yeasin Sheikh
      Yeasin Sheikh about 2 years
      Can you include your chart-code?
    • Hardik K
      Hardik K about 2 years
      chart image is included
    • Yeasin Sheikh
      Yeasin Sheikh about 2 years
      are you using any package like pie_chart?
    • Hardik K
      Hardik K about 2 years
      no, i drawing it on canvas
    • Yeasin Sheikh
      Yeasin Sheikh about 2 years
      Can you include that widget? also you can try those package.
  • Hardik K
    Hardik K about 2 years
    i have to add text on particular slice of pie chart
  • TheUltimateOptimist
    TheUltimateOptimist about 2 years
    Well, then you just have to come up with some formula to calculate the right offset and plug it in the above code snippet.