我正在开发具有新概念的新flutter应用,例如多语言支持。
我无法更改MaterialApp的语言环境,
我已经尝试了几种方法,没有希望
我尝试过类似的事情:
1. https://stackoverflow.com/a/50116077/7560822
2. https://hillel.dev/2018/08/15/flutter-how-to-rebuild-the-entire-app-to-change-the-theme-or-locale/
我的主要功能
void main() {
// ---- Connection Check : Interval ---- //
timer = Timer.periodic(Duration(seconds: 10), (Timer t) => testConnection());
// ---- Load Language ---- //
loadLanguage();
// ---- Auth Data + User Data ---- //
loadUserData();
// ---- NavBar - StatusBar Color ---- //
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
systemNavigationBarColor: Colors.green[600],
statusBarColor: Colors.green[600],
));
// ---- Portrait Only Mode ---- //
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
.then((_) {
runApp(
MaterialApp(
// ---- Bebug Ribbon Remove ---- //
debugShowCheckedModeBanner: false,
// ---- Main Title ---- //
title: appTitle,
// ---- Main Theme ---- //
theme: ThemeData(
backgroundColor: Colors.white,
fontFamily: 'DroidKufi',
),
// ---- Routes ---- //
initialRoute: mainRoute,
routes: appRoutes,
// ---- Languages ---- //
supportedLocales: [
Locale('ar'),
Locale('en'),
],
localizationsDelegates: [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
locale: localeOption,
),
);
});
}
和
中的localOption加载var lang = {"lang": 'ar'};
var localeOption = Locale(lang['lang'], '');
loadLanguage() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
if (prefs.getString('lang') != null) {
lang = {
"lang": prefs.getString('lang'),
};
localeOption = Locale(lang['lang'], '');
}
}
我希望有办法,但不能完全退出并重新打开应用程序