How to get the current locale text direction in Flutter using Intl Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to get the current locale text direction in Flutter using Intl without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I’m creating a new flutter UI component that contains selection and getting more info about the product.

enter image description here

I want this component to support RTL also, So I need to get the current locale language direction which will allow me to know which corners of the selection shape will be rounded.

The LTR shape code is like this

shape: RoundedRectangleBorder(
  borderRadius: BorderRadius.only(
    bottomLeft: Radius.circular(35),
    topLeft: Radius.circular(35),
    ),
  )

The RTL shape code will be like

shape: RoundedRectangleBorder(
  borderRadius: BorderRadius.only(
    bottomRight: Radius.circular(35),
    topRight: Radius.circular(35),
    ),
  )

I know that intl provides functionality to get the direction of specific text while I want to get the default direction of the current select locale, So if the current locale is Arabic, Farsi or any other right to left language I will return the RLT component. I don’t know exactly how to do it.

Answer

Thanks to @chunhunghan I created a static method, this method takes the context and returns true based on the current locale of the app, because if you do not pass language code the function always returns false.

  import 'package:intl/intl.dart' as intl;
  static bool isDirectionRTL(BuildContext context){
   return intl.Bidi.isRtlLanguage( Localizations.localeOf(context).languageCode);
  }
We are here to answer your question about How to get the current locale text direction in Flutter using Intl - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji