If you want to change app language without restarting the app and also without any plugin, you can follow the bellow steps:
-
In main file of the application, change the default
MyHomePageto aStatefullWidget, inStatefullWedgetfor exampleMyHomePagecreate astaticmethodsetLocalas followclass 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
-
In your
statecreate astaticmethodchangeLanguage: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, ); } } -
Now from pages of your application you can change the language by calling the
setLocalmethod and pass a newLocaleas follow:Locale newLocale = Locale('ps', 'AFG'); MyHomePage.setLocale(context, newLocale); -
Please remember you need to create a
LocalizationDelegate, -
Here is the link to the Written Tutorial and Demo Application