2
class ChangeLanguage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Change Language'),
      ),
      body: Center(
        child: RaisedButton(
          child: Text('Change'),
          onPressed: () {
            navigator.pop(context);
          },
        ),
      ),
    );
  }
  • I want to refresh previous screen according to the new language after pressing on raised button and calling navigator pop(context).
  • I am using easy_localization 1.3.1 and when i navigate back to the previous screen the layout doesn't reversed
  • Your approach is might wrong. Because when you change the language it will affect all the screens globally no need to refresh the page. Please follow the standers and take a look at the documentation for the library which you are using it will help you out. – HaSnen Tai Dec 04 '19 at 10:41
  • is that works for RTL approach and invert the layout from left to right to right to left because it works only for the translation not the layout widgets? – Asmaa Mohamed Helali Dec 04 '19 at 10:55
  • can you update the question which you have done till now. Or can you please help me with the plugin name if you're using for translation – HaSnen Tai Dec 04 '19 at 11:52
  • I am using easy_localization 1.3.1 – Asmaa Mohamed Helali Dec 05 '19 at 11:42
  • 1
    Does this answer your question? [How to change a Flutter app language without restarting the app?](https://stackoverflow.com/questions/55889889/how-to-change-a-flutter-app-language-without-restarting-the-app) – Adam Sep 20 '20 at 13:06

1 Answers1

1

If you want to change app language without restarting the app and also without any plugin, you can follow the bellow steps:

  1. In main file of the application, in StatefullWedget for example MyHomePage create a static method setLocal as follow

    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key}) : super(key: key);
    
       static void setLocale(BuildContext context, Locale newLocale) async {
          _MyHomePageState state =
               context.findAncestorStateOfType<_MyHomePageState>();());
            state.changeLanguage(newLocale);
         }
    
      @override
     _MyHomePageState createState() => _MyHomePageState();
    }
    

where _MyHomePageState is the state of your MyHomePage widget

  1. In your state create a static method changeLanguage:

     class _MyHomePageState extends State<MyHomePage> {
      Locale _locale;
    
       changeLanguage(Locale locale) {
         setState(() {
          _locale = locale;
         });
        }
    
      @override
      Widget build(BuildContext context) {
          return MaterialApp(
            debugShowCheckedModeBanner: false,
            title: 'Afghanistan',
            theme: ThemeData(primaryColor: Colors.blue[800]),
            supportedLocales: [
              Locale('fa', 'IR'),
              Locale('en', 'US'),
              Locale('ps', 'AFG'),
            ],
            locale: _locale,
            localizationsDelegates: [
              AppLocalizationsDelegate(),
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate
            ],
            localeResolutionCallback: (locale, supportedLocales) {
              for (var supportedLocale in supportedLocales) {
                if (supportedLocale.languageCode == locale.languageCode &&
                    supportedLocale.countryCode == locale.countryCode) {
                  return supportedLocale;
                }
              }
              return supportedLocales.first;
            },
            initialRoute: splashRoute,
            onGenerateRoute: Router.generatedRoute,
          );
       }
      }
    
  2. Now from pages of your application you can change the language by calling the setLocal method and pass a new Locale as follow:

    Locale newLocale = Locale('ps', 'AFG');
    MyHomePage.setLocale(context, newLocale);
    
    1. Please remember you need to create a LocalizationDelegate,

    2. Here is the link to the Written Tutorial and Demo Application

Seddiq Sorush
  • 2,709
  • 2
  • 20
  • 20