Want to change a Flutter app language without restarting the app? Wrap your MaterialApp into a StreamBuilder which will be responsible for providing the Locale value to your application. And it will enable you to dynamically change it without restarting your app.
This is an example using the rxdart package to implement the stream:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | @override Widget build(BuildContext context) { return StreamBuilder( stream: setLocale, initialData: Locale('ar',''), builder: (context, localeSnapshot) { return MaterialApp( // other arguments locale: localeSnapshot.data, ); } ); } Stream<Locale> setLocale(int choice) { var localeSubject = BehaviorSubject<Locale>() ; choice == 0 ? localeSubject.sink.add( Locale('ar','') ) : localeSubject.sink.add( Locale('en','') ) ; return localeSubject.stream.distinct() ; } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.