A tester using Hadeed, my gym log, in Arabic reported that length and weight were reversed. A height of 175 showed as 571, and an age of 28 showed as 82.
The stored values were right. The animated number was a Row with one widget per digit, and a Row lays its children out in the ambient text direction. The fix is one line on that row:
Row(
textDirection: TextDirection.ltr,
children: [
for (final c in value.characters) DigitWheel(c),
],
)Why a plain Text is fine#
Numbers are read left to right in Arabic too. Inside one Text, Flutter applies the Unicode bidirectional algorithm, and that algorithm keeps a run of digits in order.
It can only do that inside one paragraph. When every digit is its own Text, there are three paragraphs with one character each, and nothing connects them. Their order on screen is a layout decision, and Row takes it from Directionality: right to left in Arabic.
So only the animated numbers were wrong. Every ordinary Text in the app showed the same value correctly, which is why it looked like some of the data was corrupted.
I looked in the wrong place first#
571 for 175 looks like a swapped field or a byte order mistake, so I spent the first ten minutes on the data. Then I started to sketch a number formatter that knew about locales. Neither was needed.
The quick way to tell the two apart is to draw the same value with a second widget:
Column(children: [
RollingText(value, style: style),
Text(value),
])If the two disagree, the value is fine and the layout is the problem.
Where else it happens#
Anything that positions characters or digits as siblings has the same risk:
- counters that roll or flip each digit
- one-time code fields, with one box per digit
- clocks built from separate hour, minute and second widgets
- a
WraporFlex, becauseRowis aFlex - a custom layout that reads
Directionality.of(context)
A Column is not affected unless you use its cross axis.
Do not force the whole screen to LTR to fix it. Labels, icons and spacing should still mirror. Put the direction on the row that holds the digits.
Alignment is a separate question#
With the row fixed to LTR, its position in the parent should still follow the language. Mine sits in an AnimatedSize, which needed the directional values:
alignment: switch (widget.textAlign) {
TextAlign.right => Alignment.centerRight,
TextAlign.end => AlignmentDirectional.centerEnd,
TextAlign.start => AlignmentDirectional.centerStart,
TextAlign.center => Alignment.center,
_ => Alignment.centerLeft,
},AlignmentDirectional.centerStart is the right side in Arabic. Alignment.centerLeft is the left side everywhere.
Keep it readable to a screen reader#
One widget per digit also means a screen reader can find "1", "7" and "5" instead of a number. Give the row one label and hide the pieces:
Semantics(
label: widget.text,
excludeSemantics: true,
child: row,
)Test the painted order#
find.text('175') cannot catch this, because there is no such text. Checking the list of children does not work either, since they are in the right order in the tree and only painted in the wrong one. Read the positions:
List<String> painted(WidgetTester tester) {
final seen = <(double, String)>[];
for (final t in tester.widgetList<Text>(find.byType(Text))) {
final s = t.data;
if (s == null || s.isEmpty) continue;
seen.add((tester.getTopLeft(find.byWidget(t)).dx, s));
}
seen.sort((a, b) => a.$1.compareTo(b.$1));
return [for (final e in seen) e.$2];
}
for (final dir in TextDirection.values) {
testWidgets('175 is painted as 175 in ${dir.name}', (tester) async {
await tester.pumpWidget(MaterialApp(
home: Directionality(
textDirection: dir,
child: const Scaffold(body: Center(child: RollingText('175', style: style))),
),
));
await tester.pumpAndSettle();
expect(painted(tester), ['1', '7', '5']);
});
}That is my test with the sorting shortened. Run it once with the fix removed. Mine then reported ['5', '7', '1'], the number the tester had photographed. Include a decimal such as 82.5 so the point is checked as well.
When one Text is not enough either#
A single Text protects plain digits. It does not protect a number next to neutral characters, such as 3 × 12, −15 or a unit after a space. Those can be reordered inside an Arabic paragraph. For them I wrap the string in Unicode isolates:
final String _lri = String.fromCharCode(0x2066); // LEFT-TO-RIGHT ISOLATE
final String _pdi = String.fromCharCode(0x2069); // POP DIRECTIONAL ISOLATE
String ltrRun(String s) => s.isEmpty ? s : '$_lri$s$_pdi';The two problems look alike and have different fixes. Digits in separate widgets need a direction on the row. Symbols around digits in one string need an isolate in the string. The web version of the second one is in CSS RTL: the minus sign on the wrong side of the number.