Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter rich text international

Tags:

flutter

I use intl package to internationalize my app, but a problem comes to me.

There is a string for English and Chinese:

  • English version: "has $num"
  • Chinese version: "有$num个"

Now I want to add some style to the $num part, e.g., set the number to red color.

I thought I need to get the string by locale, process and split the string then convert it to TextSpans by myself.

Is there any easier/cleverer way to do this?

like image 449
Loong_T Avatar asked Nov 16 '22 14:11

Loong_T


1 Answers

Well actually you can use inline html view from webview_flutter (ref https://github.com/flutter/flutter/issues/19030#issuecomment-437534853 )

  • English version: "has <span style='text-color:red'>$num</span>"
  • Chinese version: "有<span style='text-color:red'>$num</span>个"

Just have to be careful with string interpolation.

like image 182
TruongSinh Avatar answered Feb 27 '23 19:02

TruongSinh