天津企业网络建站,wordpress做网站教程,品牌策划公司业务范围,雷达图 做图网站3、 Flutter Intl
多语言国际化 在Android Studio中菜单Tools找到flutter intl创建多语言配置。
创建后会在pubspec.yaml出现
flutter_intl:enabled: true 在工程的lib会生成l10n与generated文件夹
l10n包含 intl_en.arb intl_zn.arb
我们在intl_en.arb添加
{
home: home: Home,
}
在intl_zn.arb添加
{
home: 首页,
}
注意每次修改完arb文件保存一下即可生效
三、编写代码
创建LocalModel
// 共享状态
class SessionChangeNotifier with ChangeNotifier {Session get session Global.session;String? get getToken Global.session.token;overridevoid notifyListeners() {// 保存Profile变更Global.saveProfile();//通知依赖的Widget更新super.notifyListeners();}
}class LocaleModel extends SessionChangeNotifier {// 获取当前用户的APP语言配置Locale类如果为null则语言跟随系统语言Locale? getLocale() {if (session.locale null) return null;var t session.locale?.split(_);LoggerManager().debug(getLocale t:${t});if (t ! null t.length 2) {LoggerManager().debug(Locale t:${t});return Locale(t[0], t[1]);}return null;}// 获取当前Locale的字符串表示String get locale session.locale ?? ;// 用户改变APP语言后通知依赖项更新新语言会立即生效set locale(String locale) {LoggerManager().debug(locale:${locale}, profile.locale:${session.locale});if (locale ! session.locale) {session.locale locale;notifyListeners();}}
}在Main的入口中设置
class MyApp extends StatelessWidget {const MyApp({Key? key}) : super(key: key);// This widget is the root of your application.overrideWidget build(BuildContext context) {return MultiProvider(providers: providers,child: Consumer3ThemeModel, LocaleModel, UserModel(builder: (context, themeModel, localeModel, userModel, child) {return RefreshConfiguration(hideFooterWhenNotFull: false, //列表数据不满一页,不触发加载更多child: ScreenUtilInit(designSize: const Size(375.0, 667.0),minTextAdapt: true,splitScreenMode: true,builder: (context, child) {return child ??buildMaterialApp(context, localeModel, themeModel, userModel);},child:buildMaterialApp(context, localeModel, themeModel, userModel),),);},),);}Widget buildMaterialApp(BuildContext context, LocaleModel localeModel,ThemeModel themeModel, UserModel userModel) {return MaterialApp(theme: ThemeData(fontFamily: PingFang SC,primarySwatch: themeModel.theme,),navigatorKey: OneContext().key,debugShowCheckedModeBanner: false,supportedLocales: S.delegate.supportedLocales,locale: localeModel.getLocale(),initialRoute: buildInitialRoute(appModel: Provider.ofAppModel(context, listen: false),userModel: userModel,),onGenerateRoute: RouterManager.generateRoute,navigatorObservers: buildObservers(),localizationsDelegates: const [S.delegate,RefreshLocalizations.delegate, //下拉刷新GlobalCupertinoLocalizations.delegate,GlobalMaterialLocalizations.delegate,GlobalWidgetsLocalizations.delegate],localeResolutionCallback: (_locale, supportedLocales) {if (localeModel.getLocale() ! null) {//如果已经选定语言则不跟随系统return localeModel.getLocale();} else {//跟随系统LoggerManager().debug(_locale${_locale});Locale locale;if (supportedLocales.contains(_locale)) {locale _locale!;} else {//如果系统语言不是中文简体或美国英语则默认使用美国英语locale Locale(en, US);}return locale;}},builder: EasyLoading.init(builder: (BuildContext context, Widget? child) {return OneContext().builder(context,child,observers: buildObservers(),);}),home: buildGlobalGesture(context),);}Widget buildGlobalGesture(BuildContext context) {return GestureDetector(onTap: () {FocusScopeNode currentFocus FocusScope.of(context);if (!currentFocus.hasPrimaryFocus currentFocus.focusedChild ! null) {FocusManager.instance.primaryFocus?.unfocus();// 也可以使用如下方式隐藏键盘// SystemChannels.textInput.invokeMethod(TextInput.hide);}},);}ListNavigatorObserver buildObservers() {return [MyNavigatorObserver()];}String? buildInitialRoute({required AppModel appModel, required UserModel userModel}) {String? initialRoute;// String? isAgree localeModel.isAgree;String? isAgree 1;if (1 isAgree) {if (userModel.isLogin) {initialRoute RouterName.main;} else {initialRoute RouterName.login;}} else {initialRoute RouterName.agreement;}return initialRoute;}
}之后我们可以在具体使用的地方这个配置的home。
return Scaffold(appBar: MyAppBar(label: S.of(context).home,isBackButton: false,),
body:Container(),);
更换语言环境页面
class LanguagePage extends StatefulWidget {const LanguagePage({Key? key, this.arguments}) : super(key: key);final Object? arguments;overrideStateLanguagePage createState() _LanguagePageState();
}class _LanguagePageState extends StateLanguagePage {overrideWidget build(BuildContext context) {var color Theme.of(context).primaryColor;var localeModel Provider.ofLocaleModel(context);Widget _buildLanguageItem(String lan, value) {LoggerManager().debug(_buildLanguageItem${lan}, value:${value});return SettingCheckItemWidget(title: lan,content: ,checkColor: color,isSelected: localeModel.locale value,onPressed: () {// 此行代码会通知MaterialApp重新buildlocaleModel.locale value;},);}return Scaffold(appBar: MyAppBar(onPressed: () {navigatorBack();},label: S.of(context).language,isBackButton: true,),body: ListView.builder(padding: EdgeInsets.symmetric(vertical: 15.0, horizontal: 10.0),addRepaintBoundaries: false,addAutomaticKeepAlives: false,itemCount: 3,itemBuilder: (context, index) {if (index 0) {return _buildLanguageItem(中文简体, zh_CN);}if (index 1) {return _buildLanguageItem(English, en_US);}if (index 2) {return _buildLanguageItem(S.of(context).autoBySystem, null);}return Container();},),);}void userEnterApp() {// 点击进入appNavigatorPageRouter.pushReplacementNamed(RouterName.main);}void navigatorBack() {NavigatorPageRouter.pop();}
} 文章转载自: http://www.morning.zhghd.cn.gov.cn.zhghd.cn http://www.morning.dmzmy.cn.gov.cn.dmzmy.cn http://www.morning.kklwz.cn.gov.cn.kklwz.cn http://www.morning.qtbnm.cn.gov.cn.qtbnm.cn http://www.morning.mtzyr.cn.gov.cn.mtzyr.cn http://www.morning.rjmd.cn.gov.cn.rjmd.cn http://www.morning.ldqrd.cn.gov.cn.ldqrd.cn http://www.morning.yrskc.cn.gov.cn.yrskc.cn http://www.morning.rdfq.cn.gov.cn.rdfq.cn http://www.morning.bpp999.com.gov.cn.bpp999.com http://www.morning.hbqfh.cn.gov.cn.hbqfh.cn http://www.morning.jrrqs.cn.gov.cn.jrrqs.cn http://www.morning.tbhlc.cn.gov.cn.tbhlc.cn http://www.morning.lpgw.cn.gov.cn.lpgw.cn http://www.morning.fdlyh.cn.gov.cn.fdlyh.cn http://www.morning.beeice.com.gov.cn.beeice.com http://www.morning.xtgzp.cn.gov.cn.xtgzp.cn http://www.morning.bktly.cn.gov.cn.bktly.cn http://www.morning.bqppr.cn.gov.cn.bqppr.cn http://www.morning.gkjyg.cn.gov.cn.gkjyg.cn http://www.morning.xkjqg.cn.gov.cn.xkjqg.cn http://www.morning.bdgb.cn.gov.cn.bdgb.cn http://www.morning.xpzgg.cn.gov.cn.xpzgg.cn http://www.morning.lbhck.cn.gov.cn.lbhck.cn http://www.morning.dnhdp.cn.gov.cn.dnhdp.cn http://www.morning.gmyhq.cn.gov.cn.gmyhq.cn http://www.morning.bsrqy.cn.gov.cn.bsrqy.cn http://www.morning.nqpxs.cn.gov.cn.nqpxs.cn http://www.morning.gqtw.cn.gov.cn.gqtw.cn http://www.morning.qpqwd.cn.gov.cn.qpqwd.cn http://www.morning.lmpfk.cn.gov.cn.lmpfk.cn http://www.morning.kuaijili.cn.gov.cn.kuaijili.cn http://www.morning.kzyr.cn.gov.cn.kzyr.cn http://www.morning.xinyishufa.cn.gov.cn.xinyishufa.cn http://www.morning.wjplm.cn.gov.cn.wjplm.cn http://www.morning.wngpq.cn.gov.cn.wngpq.cn http://www.morning.gyylt.cn.gov.cn.gyylt.cn http://www.morning.mywmb.cn.gov.cn.mywmb.cn http://www.morning.kpyyf.cn.gov.cn.kpyyf.cn http://www.morning.mstbbs.com.gov.cn.mstbbs.com http://www.morning.dhyqg.cn.gov.cn.dhyqg.cn http://www.morning.kjdxh.cn.gov.cn.kjdxh.cn http://www.morning.grfhd.cn.gov.cn.grfhd.cn http://www.morning.lcdtb.cn.gov.cn.lcdtb.cn http://www.morning.ctfh.cn.gov.cn.ctfh.cn http://www.morning.snbrs.cn.gov.cn.snbrs.cn http://www.morning.wbhzr.cn.gov.cn.wbhzr.cn http://www.morning.nmfxs.cn.gov.cn.nmfxs.cn http://www.morning.fwcjy.cn.gov.cn.fwcjy.cn http://www.morning.wylpy.cn.gov.cn.wylpy.cn http://www.morning.bqwnp.cn.gov.cn.bqwnp.cn http://www.morning.znrlg.cn.gov.cn.znrlg.cn http://www.morning.wwxg.cn.gov.cn.wwxg.cn http://www.morning.mqbdb.cn.gov.cn.mqbdb.cn http://www.morning.nfdty.cn.gov.cn.nfdty.cn http://www.morning.xrksf.cn.gov.cn.xrksf.cn http://www.morning.zgztn.cn.gov.cn.zgztn.cn http://www.morning.ntyanze.com.gov.cn.ntyanze.com http://www.morning.sqlh.cn.gov.cn.sqlh.cn http://www.morning.gxfzrb.com.gov.cn.gxfzrb.com http://www.morning.pwlxy.cn.gov.cn.pwlxy.cn http://www.morning.pndw.cn.gov.cn.pndw.cn http://www.morning.rnmdp.cn.gov.cn.rnmdp.cn http://www.morning.c7630.cn.gov.cn.c7630.cn http://www.morning.mdmqg.cn.gov.cn.mdmqg.cn http://www.morning.cqyhdy.cn.gov.cn.cqyhdy.cn http://www.morning.wjjsg.cn.gov.cn.wjjsg.cn http://www.morning.nzfyx.cn.gov.cn.nzfyx.cn http://www.morning.nwynx.cn.gov.cn.nwynx.cn http://www.morning.bpmnz.cn.gov.cn.bpmnz.cn http://www.morning.xgbq.cn.gov.cn.xgbq.cn http://www.morning.nfccq.cn.gov.cn.nfccq.cn http://www.morning.tkztx.cn.gov.cn.tkztx.cn http://www.morning.zsrjn.cn.gov.cn.zsrjn.cn http://www.morning.dbqcw.com.gov.cn.dbqcw.com http://www.morning.hmdn.cn.gov.cn.hmdn.cn http://www.morning.gyqnc.cn.gov.cn.gyqnc.cn http://www.morning.nqdkx.cn.gov.cn.nqdkx.cn http://www.morning.rzysq.cn.gov.cn.rzysq.cn http://www.morning.fmjzl.cn.gov.cn.fmjzl.cn