Search code examples
flutterflutter-layout

TextSpan avoid line break on symbol String group


I have a RichText() widget with a TextSpan containing a String of symbols such as €€€€€€€

this group of symbol is not considered as one word even thought there is no spacing

-> I'd like to avoid a line break to happen between those symbols have to stay together

I can't set softWrap: false on RichText as I need a line break somewhere to avoid overflow

RichText(
   text: TextSpan(
      text: 'Random text hello there Random long text',
       children: <TextSpan> [
         const TextSpan(text: ' · '),
         TextSpan(text: '€€€€€€€'),
         ],
   ),
 ),

can't I set softWrap: false directly on TextSpan or something similar ?


Solution

  • Before:

    enter image description here

    After:

    enter image description here

    Use WidgetSpan with Text instead of TextSpan.

    RichText(
      text: TextSpan(
        text: 'Random text hello there Random',
        children: [
          const TextSpan(text: ' · '),
          WidgetSpan(
            child: Text('€€€€€€€'),
          ),
        ],
      ),
    )