Draw text on a canvas

Flutter tips Published on

Understanding Canvas in Flutter

A Canvas in Flutter is like a blank drawing board. It's an interface that lets you draw shapes, lines, images, and yes, even text, directly onto the screen.

Using a canvas gives you fine-grained control over exactly where and how things appear, which is great for custom widgets, charts, or drawing apps.

While you usually use standard widgets like Text to show words, sometimes you need to draw text as part of a custom drawing process on a canvas. This is where TextPainter comes in handy.

How to Draw Text on a Canvas

To draw text on a canvas, you'll typically use a combination of CustomPaint (which provides the canvas) and TextPainter (which helps you prepare and draw the text).

Here’s the basic process:

1. Define the Text Style

First, decide how your text should look. You create a TextStyle object, just like you would for a regular Text widget. You can set color, font size, weight, etc.

final textStyle = TextStyle(
  color: Colors.black,
  fontSize: 40,
);

2. Create a TextSpan and TextPainter

A TextSpan holds the text content and its style. Then, a TextPainter is created with this TextSpan. The TextPainter is the tool that measures and paints the text.

You also need to tell the TextPainter the text direction (like left-to-right).

final textSpan = TextSpan(
  text: 'Hello, Canvas!',
  style: textStyle,
);

final textPainter = TextPainter(
  text: textSpan,
  textDirection: ui.TextDirection.ltr,
);

3. Prepare for Layout

Before you can draw, you need to calculate the text's size based on the style and content. This is done with the layout() method.

textPainter.layout();

4. Define Position and Draw

Decide where on the canvas you want the top-left corner of your text to appear. You use an Offset for this.

Finally, call the paint() method on the TextPainter, passing the canvas and the offset position.

final offset = Offset(50, 100); // Position (x, y)
textPainter.paint(canvas, offset);

This process happens inside the paint method of a CustomPainter class, which is then used within a CustomPaint widget.

Using TextPainter on a canvas gives you great flexibility for drawing text as part of complex graphical elements in your Flutter app.

Save 3 months of work

Create your app using our 6 years of making Flutter apps and more than 50+ apps

kickstarter for flutter apps

Frequently Asked Questions

What is a Canvas in Flutter?

A Canvas is a surface in Flutter that allows you to draw directly onto the screen using painting commands.

Why use TextPainter instead of a Text widget?

You use TextPainter when you need to draw text as part of a custom drawing operation on a Canvas, often within a CustomPainter. A Text widget is a standard widget for displaying text in the regular widget tree.

How do I change the appearance of text drawn with TextPainter?

You define the appearance using a TextStyle object, which you pass to the TextSpan used by the TextPainter. This includes color, font size, weight, etc.

How do I set the position of the text on the Canvas?

You specify the top-left corner of where the text should be drawn using an Offset object when calling the textPainter.paint() method.

Read more
You may also be interested in
Listen keyboard visibility state  blog card image
Listen keyboard visibility state
Published on 2025-05-12T09:22:43.829Z
Compare app versions  blog card image
Compare app versions
Published on 2025-05-12T09:22:19.759Z
ApparenceKit is a flutter template generator tool by Apparence.io © 2025.
All rights reserved