How to change app language on click of a button?
How to change app language on click of a button and without restarting the app ?
you need to add a static method in your myApp , i.e
class MyApp extends StatefulWidget { @override _MyAppState createState() => _MyAppState();
static _MyAppState of(BuildContext context) => context.findAncestorStateOfType<_MyAppState>(); }
class _MyAppState extends State<MyApp> { Locale _locale;
void setLocale(Locale value) { setState(() { _locale = value; }); }
@override Widget build(BuildContext context) { return MaterialApp( locale: _locale, home: Dashboard(), ); } }
class Dashboard extends StatelessWidget { @override Widget build(BuildContext context) { return Column( children: [ TextButton( child: Text("Set locale to English"), onPressed: () => MyApp.of(context).setLocale(Locale.fromSubtags(languageCode: 'en')), ), TextButton( child: Text("Set locale to EK"), onPressed: () => MyApp.of(context).setLocale(Locale.fromSubtags(languageCode: 'ek')), ), ], ); } }