Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to make characters in Flutter to have the same width?

As you can see in the picture below, the texts have the same amount of characters, but since the number "1" is slimmer than the "5" and "2", both texts get a different width.

How can I adjust that in Flutter?

's

like image 939
Daniel Oliveira Avatar asked Feb 28 '19 22:02

Daniel Oliveira


People also ask

How do you change text width in flutter?

Setting the width of a TextField You can set the width of a TextField exactly as you want by wrapping it inside a Container, a SizedBox, or a ContrainedBox widget. Note that the height of the parent widget will not affect the height of the text field inside it.

How do you add big text in flutter?

Wrap the text within a FittedBox widget, to force the text to be enclosed by a box. The FittedBox's size will depend on it's parent's widget. Within the FittedBox, the Text widget, can simply 'cover' the box, so the text doesn't stretch to fill the available space within the FittedBox. The enum BoxFit.


3 Answers

You can set the font feature to use tabularFigures.

// import 'dart:ui';

Text(
  _getTimeString(),
  style: TextStyle(
    fontFeatures: [
      FontFeature.tabularFigures()
    ],
  ),
),

Before:

enter image description here

After:

enter image description here

See also:

  • Font Features in Flutter
like image 98
Suragch Avatar answered Oct 19 '22 01:10

Suragch


Use a monospaced font, also called a fixed-pitch, fixed-width, or non-proportional font, is a font whose letters and characters each occupy the same amount of horizontal space.

Wikipedia explains it well. https://en.wikipedia.org/wiki/Monospaced_font

like image 21
hiepav Avatar answered Oct 19 '22 01:10

hiepav


Hiepav suggestion, seems a good approach because you are not doing nothing wrong but each character in the font have different widths so it will have to adjust to give enough room.

However, as a workaround you can actually wrap your texts in a fixed width sized box that gives enough space for both widgets regarding its character widths variations , such as SizedBox, ConstrainedBox or even Container with width constraints and center align child. This way, with that font, you should at least have the : vertically aligned.

like image 2
Miguel Ruivo Avatar answered Oct 19 '22 00:10

Miguel Ruivo