RTL is not properly working in string and number combined text format

Viewed 111

I have been trying to add RTL support in my app. But I am facing the issue that the RTL is not applied in some specific cases.Here I have shared the code sample regarding this issue

void main() {
  return runApp(MyApp());
}

bool? rtlEnabled;
late TextDirection _textDirection;

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyStatefulWidget(),
    );
  }
}

/// RTL main file
class MyStatefulWidget extends StatefulWidget {
  const MyStatefulWidget({Key? key}) : super(key: key);
  @override
  _MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  @override
  void initState() {
    super.initState();
    _textDirection = TextDirection.ltr;
    rtlEnabled = false;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(),
        backgroundColor: Colors.grey,
        endDrawer: Container(
          color: Colors.white,
          width: 200,
          child: ListView(
            children: <Widget>[
              SizedBox(height: 150),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: <Widget>[
                  const Text('Enable RTL'),
                  Checkbox(
                    value: rtlEnabled,
                    onChanged: (bool? value) {
                      setState(() {
                        rtlEnabled = value!;
                        if (rtlEnabled == true) {
                          _textDirection = TextDirection.rtl;
                        } else {
                          _textDirection = TextDirection.ltr;
                        }
                      });
                    },
                  ),
                ],
              ),
            ],
          ),
        ),
        body: Directionality(
            textDirection: TextDirection.ltr,
            child: Center(
              child: Center(
                child: Container(
                  color: Colors.white,
                  width: 300,
                  height: 300,
                  child: CustomPaint(
                    painter: MyPainter(),
                  ),
                ),
              ),
            )));
  }
}

class MyPainter extends CustomPainter {
  dynamic x = 'Left';
  dynamic y = 10;

  @override
  void paint(Canvas canvas, Size size) {
    final String text = '$x : $y';
    final TextStyle textStyle = TextStyle(
      color: Colors.black,
      fontSize: 30,
    );
    final TextSpan textSpan = TextSpan(
      text: text,
      style: textStyle,
    );
    final TextPainter textPainter = TextPainter(
      text: textSpan,
      textDirection: _textDirection,
    );
    textPainter.layout();
    textPainter.paint(canvas, Offset.zero);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return false;
  }
}

Run this sample.After that, if we enable the RTL option in app bar, the RTL won't work. It renders the text as 'Left : 10' in both LTR and RTL. But if we change the x as 10 and y as 'Left'. The RTL is applied. It renders the text as '10 : Left' in LTR and 'Left : 10' in RTL. I'm confused whether it's framework issue or not.

1 Answers

add localization in your app. and set your language (which is RTL). this will fix your RTL problems

Related