Json_model

此依赖包是用来将json文件转换为对应的dart model类的。
依赖设置:

  1. dependencies:
  2. # Your other regular dependencies here
  3. json_annotation: ^2.0.0
  4. dev_dependencies:
  5. # Your other dev_dependencies here
  6. build_runner: ^1.0.0
  7. json_serializable: ^2.0.0

在项目根目录下创建jsons文件夹,在lib是文件夹下创建models文件夹,最后执行如下命令即可:

  1. flutter packages pub run json_model

Intl

此依赖包是用来实现国际化功能的。
依赖配置:

  1. dependencies:
  2. flutter:
  3. sdk: flutter
  4. flutter_localizations:
  5. sdk: flutter
  6. intl: ^0.15.7
  7. dev_dependencies:
  8. #...省略无关项
  9. intl_translation: ^0.17.2
  • 在项目根目录下创建一个i10n-arb目录
  • 在lib/i10n目录下新建一个“localization_intl.dart”的文件,内容如下:

    1. import 'package:flutter/material.dart';
    2. import 'package:intl/intl.dart';
    3. import 'messages_all.dart'; //1
    4. class DemoLocalizations {
    5. static Future<DemoLocalizations> load(Locale locale) {
    6. final String name = locale.countryCode.isEmpty ? locale.languageCode : locale.toString();
    7. final String localeName = Intl.canonicalizedLocale(name);
    8. //2
    9. return initializeMessages(localeName).then((b) {
    10. Intl.defaultLocale = localeName;
    11. return new DemoLocalizations();
    12. });
    13. }
    14. static DemoLocalizations of(BuildContext context) {
    15. return Localizations.of<DemoLocalizations>(context, DemoLocalizations);
    16. }
    17. String get title {
    18. return Intl.message(
    19. 'Flutter APP',
    20. name: 'title',
    21. desc: 'Title for the Demo application',
    22. );
    23. }
    24. }
    25. //Locale代理类
    26. class DemoLocalizationsDelegate extends LocalizationsDelegate<DemoLocalizations> {
    27. const DemoLocalizationsDelegate();
    28. //是否支持某个Local
    29. @override
    30. bool isSupported(Locale locale) => ['en', 'zh'].contains(locale.languageCode);
    31. // Flutter会调用此类加载相应的Locale资源类
    32. @override
    33. Future<DemoLocalizations> load(Locale locale) {
    34. //3
    35. return DemoLocalizations.load(locale);
    36. }
    37. // 当Localizations Widget重新build时,是否调用load重新加载Locale资源.
    38. @override
    39. bool shouldReload(DemoLocalizationsDelegate old) => false;
    40. }

    注意:
    注释1的”messages_all.dart”文件是通过intl_translation工具从arb文件生成的代码,所以在第一次运行生成命令 之前,此文件不存在。注释2处的initializeMessages()方法和”messages_all.dart”文件一样,是同时生成的。
    注释3处和上一节示例代码不同,这里我们直接调用DemoLocalizations.load()即可。

  • 生成arb文件,内容如下

    1. {
    2. "@@last_modified": "2018-12-10T17:37:28.505088",
    3. "title": "Flutter APP",
    4. "@title": {
    5. "description": "Title for the Demo application",
    6. "type": "text",
    7. "placeholders": {}
    8. },
    9. "remainingEmailsMessage": "{howMany,plural, =0{There are no emails left}=1{There is {howMany} email left}other{There are {howMany} emails left}}",
    10. "@remainingEmailsMessage": {
    11. "description": "How many emails remain after archiving.",
    12. "type": "text",
    13. "placeholders": {
    14. "howMany": {
    15. "example": 42
    16. }
    17. }
    18. }
    19. }
  • 创建intl_zh_CN.arb文件,并翻译内容

    1. {
    2. "@@last_modified": "2018-12-10T15:46:20.897228",
    3. "@@locale":"zh_CN",
    4. "title": "Flutter应用",
    5. "@title": {
    6. "description": "Title for the Demo application",
    7. "type": "text",
    8. "placeholders": {}
    9. },
    10. "remainingEmailsMessage": "{howMany,plural, =0{没有未读邮件}=1{有{howMany}封未读邮件}other{有{howMany}封未读邮件}}",
    11. "@remainingEmailsMessage": {
    12. "description": "How many emails remain after archiving.",
    13. "type": "text",
    14. "placeholders": {
    15. "howMany": {
    16. "example": 42
    17. }
    18. }
    19. }
    20. }
  • 生成dart代码

    1. flutter pub pub run intl_translation:generate_from_arb --output-dir=lib/i10n --no-use-deferred-loading lib/i10n/localization_intl.dart i10n-arb/intl_*.arb