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?
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.
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.
You can set the font feature to use tabularFigures
.
// import 'dart:ui';
Text(
_getTimeString(),
style: TextStyle(
fontFeatures: [
FontFeature.tabularFigures()
],
),
),
Before:
After:
See also:
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With