Home > Enterprise >  Flutter/Dart: RichText and TextField removes beginning spaces at display
Flutter/Dart: RichText and TextField removes beginning spaces at display

Time:01-17

I have a RichText Widget in Flutter where I want to print the attribute LUKETXT which is in a Modell. The problem is, that LUKETXT is containing whitespaces at the beginning, like for example:

'                                                                                          *end of invoice*                                          '  

But when I want to display it, the whitespaces at the beginning where removed.

My function is:

RichText BuildText(List<OrderReceipt> OrderReceipt) {
  int deflen = OrderReceipt.length;
  return RichText(
    text: TextSpan(
        style: const TextStyle(color: Colors.black,
            fontFamily: "PolarSys",fontSize: 10),
      spellOut: false,
      children: [
        for (int i =0; i<deflen; i  )
            TextSpan(
              spellOut: false,
              text: OrderReceipt[i].LUKETXT,
            )
      ]
    ),
  );
}

Just for information: PolarSys Font is a Monospace Font, that's the reason I gonna build it like that.

Does anyone know what is wrong? I already experimentet with spellOut true and false but there was no change.

Thanks..

CodePudding user response:

The problem is likely with the leading whitespaces being removed by the TextSpan widget. In Flutter, the TextSpan widget is designed to remove leading whitespaces by default. To preserve the leading whitespaces, you can use a Text widget instead of TextSpan. You can wrap the Text widget inside a parent widget like a Padding or a SizedBox to add the necessary whitespaces before the text.

  • Related