How to create a Spacer style Widget that prints dots?

274

You can combine CustomPainter with TextPainter to fill the available width with a .

enter image description here

Example:

class PointPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    final point = TextPainter(
        text: TextSpan(text: ".", style: TextStyle(color: Colors.red)),
        textDirection: TextDirection.ltr);
    point.layout(maxWidth: size.width);

    for (double i = 0; i < size.width; i += point.width) {
      point.paint(canvas, Offset(i, .0));
    }
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }
}

You'd normally pass the text theme as parameter, but for example purposes let's skip it.

It can then be used within a Row using an Expanded:

Expanded(
  child: CustomPaint(
    painter: PointPainter(),
  ),
),
Share:
274
Brett
Author by

Brett

Updated on December 07, 2022

Comments

  • Brett
    Brett over 1 year

    I'm looking at the Material.io Basil design study, and I'm trying to figure out how to make a Flutter Widget acts like a Spacer, but with content.

    Basil's ingredients listing

  • Abdol Hussain Mozaffari
    Abdol Hussain Mozaffari almost 3 years
    How to make it center vertically?