1 多语言

[语言]_[地区].dart

(1) 编写多语言字典

  1. const Map<String, String> en_US = {
  2. 'title': 'This is Title!',
  3. 'login': 'logged in as @name with email @email',
  4. };
  1. const Map<String, String> zh_Hans = {
  2. 'title': '这是标题',
  3. 'login': '登录用户 @name,邮箱账号 @email',
  4. };
  1. const Map<String, String> zh_HK = {
  2. 'title': '這是標題',
  3. 'login': '登錄用戶 @name,郵箱賬號 @email',
  4. };

(2) 继承 Translations

  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3. import 'zh_Hans.dart';
  4. import 'zh_HK.dart';
  5. import 'en_US.dart';
  6. class TranslationService extends Translations {
  7. static Locale? get locale => Get.deviceLocale;
  8. static final fallbackLocale = Locale('en', 'US');
  9. @override
  10. Map<String, Map<String, String>> get keys => {
  11. 'en_US': en_US,
  12. 'zh_Hans': zh_Hans,
  13. 'zh_HK': zh_HK,
  14. };
  15. }

(3) GetMaterialApp初始化

  1. class MyApp extends StatelessWidget {
  2. const MyApp({Key? key}) : super(key: key);
  3. @override
  4. Widget build(BuildContext context) {
  5. return GetMaterialApp(
  6. debugShowCheckedModeBanner: false,
  7. initialRoute: AppPages.INITIAL,
  8. getPages: AppPages.routes,
  9. unknownRoute: AppPages.unknownRoute,
  10. // 下面这三条配置国际化多语言
  11. locale: TranslationService.locale,
  12. fallbackLocale: TranslationService.fallbackLocale,
  13. translations: TranslationService(),
  14. );
  15. }

(4) view

  1. class LangView extends StatelessWidget {
  2. const LangView({Key? key}) : super(key: key);
  3. @override
  4. Widget build(BuildContext context) {
  5. return Scaffold(
  6. appBar: AppBar(
  7. title: Text("多语言"),
  8. ),
  9. body: Center(
  10. child: Column(
  11. children: [
  12. Text(
  13. "title -> " + 'title'.tr,
  14. style: TextStyle(fontSize: 24),
  15. ),
  16. Divider(),
  17. Text(
  18. "login -> " +
  19. 'login'.trParams(
  20. {'name': 'ducafecat', 'email': 'ducafecat@gmail.com'}),
  21. style: TextStyle(fontSize: 24),
  22. ),
  23. Divider(),
  24. ListTile(
  25. title: Text("切换语言"),
  26. subtitle: Text('zh-Hans'),
  27. onTap: () {
  28. var locale = Locale('zh', 'Hans');
  29. Get.updateLocale(locale);
  30. },
  31. ),
  32. ListTile(
  33. title: Text("切换语言"),
  34. subtitle: Text('en-US'),
  35. onTap: () {
  36. var locale = Locale('en', 'US');
  37. Get.updateLocale(locale);
  38. },
  39. ),
  40. ],
  41. ),
  42. ),
  43. );
  44. }
  45. }

2 主题

  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3. class ThemeView extends StatelessWidget {
  4. const ThemeView({Key? key}) : super(key: key);
  5. @override
  6. Widget build(BuildContext context) {
  7. return Scaffold(
  8. appBar: AppBar(
  9. title: Text("主题"),
  10. ),
  11. body: Center(
  12. child: Column(
  13. children: [
  14. Container(
  15. height: 100,
  16. child: Align(
  17. alignment: Alignment.center,
  18. child: Text(
  19. "是否黑色主题 -> " + Get.isDarkMode.toString(),
  20. style: TextStyle(fontSize: 24),
  21. )),
  22. ),
  23. Divider(),
  24. ListTile(
  25. title: Text("切换主题"),
  26. onTap: () {
  27. Get.changeTheme(
  28. Get.isDarkMode ? ThemeData.light() : ThemeData.dark());
  29. },
  30. ),
  31. ],
  32. ),
  33. ),
  34. );
  35. }
  36. }