flutter plugins插件【三】【Flutter Intl】

news2024/10/7 10:14:06

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",
}

在intl_zn.arb添加

{
'home': "首页",
}

 

三、编写代码

创建LocalModel

// 共享状态
class SessionChangeNotifier with ChangeNotifier {
  Session get session => Global.session;

  String? get getToken => Global.session.token;

  @override
  void 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.
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: providers,
      child: Consumer3<ThemeModel, 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.of<AppModel>(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');
        }
      },
    );
  }

  List<NavigatorObserver> 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;

  @override
  State<LanguagePage> createState() => _LanguagePageState();
}

class _LanguagePageState extends State<LanguagePage> {
  @override
  Widget build(BuildContext context) {
    var color = Theme.of(context).primaryColor;
    var localeModel = Provider.of<LocaleModel>(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重新build
          localeModel.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() {
    // 点击进入app
    NavigatorPageRouter.pushReplacementNamed(RouterName.main);
  }

  void navigatorBack() {
    NavigatorPageRouter.pop();
  }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/963303.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

常见的存储结构

分析&回答 本文只作为了解&#xff0c;让大家理解 B数跟透传&#xff0c;可以不刷哈。 常见的存储结构&#xff1a; 我们计算机的主存基本都是随机访问存储器(Random-Access Memory&#xff0c;RAM)&#xff0c;他分为两类&#xff1a;静态随机访问存储器&#xff08;SRA…

iPhone 15 Ultra都要来啦?顶配8GB 内存、2TB存储,满足任何想象

据相关媒体透露&#xff0c;苹果计划在今年9月推出全新的iPhone 15系列。除了已确认的iPhone 15 Pro和iPhone 15 Pro Max之外&#xff0c;还有一款名为iPhone 15 Ultra至尊版的机型将会问世。 这款iPhone 15 Ultra将成为苹果旗舰系列的巅峰之作&#xff0c;预计将配备更高配置和…

【算法竞赛宝典】语言之争

【算法竞赛宝典】语言之争 题目描述代码展示 题目描述 代码展示 //语言之争 #include<fstream> #include<string>using namespace std;ifstream cin("language.in"); ofstream cout("language.out");string a; int n;int main() {int i;bool …

快速建设数字工厂管理系统,需要做好哪些准备

随着工业4.0的推进&#xff0c;数字工厂管理系统已经在全球范围内得到了广泛的应用。在中国&#xff0c;许多制造业企业也逐步认识到数字化转型的重要性&#xff0c;开始积极探索和实施数字工厂管理系统。那么&#xff0c;在快速建设数字工厂管理系统的过程中&#xff0c;需要做…

SpringBoot 使用MyBatis分页插件实现分页功能

SpringBoot 使用MyBatis分页插件实现分页功能 1、集成pagehelper2、配置pagehelper3、编写代码4、分页效果 案例地址&#xff1a; https://gitee.com/vinci99/paging-pagehelper-demo/tree/master 1、集成pagehelper <!-- 集成pagehelper --> <dependency><gr…

大数据课程K17——Spark的协同过滤法

文章作者邮箱&#xff1a;yugongshiyesina.cn 地址&#xff1a;广东惠州 ▲ 本章节目的 ⚪ 了解Spark的协同过滤概念&#xff1b; 一、协同过滤概念 1. 概念 协同过滤是一种借助众包智慧的途径。它利用大量已有的用户偏好来估计用户对其未接触过的物品的喜好程…

最快速度!北京银行6个月完成40个业务系统数字化升级

近日&#xff0c;北京银行的数字化升级步调不断加快。过去 6 个月&#xff0c;基于原生分布式数据库 OceanBase&#xff0c;北京银行已完成包括客户信息系统等在内的 40 个关键业务系统升级。 北京银行将数字化转型作为统领“五大转型”的发展战略&#xff0c;要以技术为创新驱…

开发部门源代码防泄密方案

在企业内部&#xff0c;最核心的部门无外乎企业的研发部门&#xff0c;研发部门可以说是每一家企业的核心动力&#xff0c;研发部门研发的资料一般为源代码、图纸两种类型最多。那么企业投入大最的人力物力&#xff0c;当研发离职时&#xff0c;都会把在企业做过的源代码或图纸…

Systrace分析App性能学习笔记

学习Gracker Systrace系列文章&#xff0c;总结使用Systrace分析App性能的方法。推荐想通过Systrace学习Framework的同学&#xff0c;去看原文。 文章目录 概述Systrace使用流程Systrace 文件生成图形方式(不推荐)命令行方式 Systrace分析快捷键使用帧状态线程状态查看线程唤醒…

使用正则表达式在中英文之间添加空格

有时为了排版需要&#xff0c;我们可能需要在文章的中英文之间添加空格&#xff0c;特别是中文中引用了英文单词时&#xff0c;这种情况使用正则表达式整体修订是最明智的做法。首先&#xff0c;推荐使用在线的正则表格式工具&#xff1a;https://regex101.com/ , 该工具非常强…

抖音小程序开发教学系列(1)- 抖音小程序简介

章节一&#xff1a;抖音小程序简介 1.1 抖音小程序的背景和概述 抖音小程序的发展背景和市场趋势&#xff1a; 抖音作为一款热门的短视频社交平台&#xff0c;用户群体庞大&#xff0c;社交共享的特性也为小程序的发展提供了广阔的空间。抖音小程序作为抖音在社交和用户粘性…

【算法竞赛宝典】统计字符

【算法竞赛宝典】统计字符 题目描述代码展示 题目描述 代码展示 //统计出其中英文字母、空格、数字和其他字符的个数 # include <iostream> #include <cstdlib>using namespace std;int main() {freopen("letter.in", "r", stdin);freopen(&q…

软件产品鉴定测试

1. 服务流程 2. 服务内容 该项测试从技术和应用的角度对商用软件产品的质量特性进行全面地、系统地测试和评估&#xff0c;测试内容涵盖了功能性测试、性能测试、可靠性测试、易用性测试、维护性测试及可移植性测试。 3. 周期 7-15个工作日 4. 报告用途 可作为进行省级、国…

支付宝使用OceanBase的历史库实践分享

为解决因业务增长引发的数据库存储空间问题&#xff0c;支付宝基于 OceanBase 数据库启动了历史库项目&#xff0c;通过历史数据归档、过期数据清理、异常数据回滚&#xff0c;实现了总成本降低 80%。 历史数据归档&#xff1a;将在线库&#xff08;SSD 磁盘&#xff09;数据归…

Javase | IO流

目录&#xff1a; 1.输入 (Intput/Read)2.输出 (Output/Write)3.IO4.IO流5.IO流的分类&#xff1a;5.1 分类总述5.2 按照 “流的方向” 进行分类5.3 按照 “读取数据的方式” 进行分类 6.IO包下要重点掌握的流&#xff1a;6.1 文件专属 (流)6.2 转换流 ( 将字节流转换为字符流 …

解决npm install报错: No module named gyp

今天运行一个以前vue项目&#xff0c;启动时报错如下&#xff1a; ERROR Failed to compile with 1 error上午10:19:33 error in ./src/App.vue?vue&typestyle&index0&langscss& Syntax Error: Error: Missing binding D:\javacode\Springboot-MiMall-RSA\V…

【STM32】DMA初步使用

DMA简介 DMA(Direct Memory Access)—直接存储器存取&#xff0c;是单片机的一个外设&#xff0c;它的主要功能 是用来搬数据&#xff0c;但是不需要占用 CPU&#xff0c;即在传输数据的时候&#xff0c;CPU 可以干其他的事情。 以STM32F103单片机为例 F03有DMA1和DMA2两组DM…

项目总结知识点记录-文件上传下载(三)

&#xff08;1&#xff09;文件上传 代码&#xff1a; RequestMapping(value "doUpload", method RequestMethod.POST)public String doUpload(ModelAttribute BookHelper bookHelper, Model model, HttpSession session) throws IllegalStateException, IOExcepti…

windows如何更改/禁用系统更新

提示&#xff1a;首先说明这属于将更新时间更改&#xff0c;不过你可以的将更新时间更改为十年一百年 废话不多说开始正文&#xff1a; 1.首先:winR打开运行&#xff0c;输入regedit&#xff0c;进入注册表编辑器 2.进入编辑器后依次点击&#xff1a;HKEY_LOCAL_MACHINE\SOFT…

Clion 使用ffmpeg 学习1 开发环境配置

Clion 使用ffmpeg 学习1 开发环境配置 一、准备工作1. 准备环境2. 下载FFmpeg 二、操作步骤1. Clion 新建一个C项目2. 修改 CMakeLists.txt3. 修改配置4. 运行测试5. 打印rtsp 流信息的 demo 一、准备工作 在视频处理和多媒体应用程序开发中&#xff0c;FFmpeg 是一个强大的开…