Flutter TextField set textAlign for each line separately

208 Views Asked by At

In flutter, Is there any way to set textAlign for each line separately? I want something like telegram app text input:

enter image description here

In telegram, if the line starts with a rtl language, text align is right otherwise it is left.

I try these ways so far:

1- auto_direction package

2- Checking text with intl.Bidi.detectRtlDirectionality and set textAlign dynamically.

But all of these ways sets the textAlign for all lines, I want to set it separately for each line.

2

There are 2 best solutions below

4
Linisha On

Wrap each line with align widget separately and give alignment property as per your requirement

2
zex_rectooor On

Detect language and change textDirection and textAlign properly. You can detect languages with RegExp class.

  var persian = RegExp(r'[\u0600-\u06FF]');
  var english = RegExp(r'[a-zA-Z]');
  var arabic = RegExp(r'[\u0750-\u077F]');
  var chinese = RegExp(r'[\u4E00-\u9FFF]');
  var japanese = RegExp(r'[\u3040-\u309F]');
  var korean = RegExp(r'[\uAC00-\uD7AF]');

Use onChanged call back property of TextField to detect language.