Home > Blockchain >  best practices of RTL support in the flutter project
best practices of RTL support in the flutter project

Time:10-29

I am working on one existing Flutter project. In which, We want to add RTL support in the flutter project, I went through some of the stuff on the internet but was unable to figure out the best possible way or best practice to do that. Please share your thoughts.

CodePudding user response:

Well Flutter takes care of that. However, some widgets may not look nice or wouldn't be as designed to be so you could manipulate it when its RTL and keep it as is if it's LTR.

What I did in similar scenario is:

First create a builder class like so

class DirectionBuilder extends StatelessWidget {  
final Widget Function(bool) builder;

const DirectionBuilder({Key? key, required this.builder})
    : assert(builder != null),
      super(key: key);
@override
Widget build(BuildContext context) =>
  builder(Directionality.of(context) == TextDirection.ltr);
}

The boolean value will be true if device alignment is RTL

Then you can use it like so:

DirectionBuilder(
   builder: (bool isLTR) => Align(
       alignment: isLTR
          ? Alignment.centerRight 
          : Alignment.centerLeft,
                child: Text('bool value is $isLTR')))
  • Related