-
Notifications
You must be signed in to change notification settings - Fork 0
/
LocalizationAbout.dart
70 lines (58 loc) · 1.77 KB
/
LocalizationAbout.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_starter_notes/component/CommonTitle.dart';
class LocalizationAbout extends StatelessWidget {
LocalizationAbout({Key key, this.title});
final String title;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: SingleChildScrollView(
child: Column(
children: <Widget>[
CommonTitle('自定义多语言实现-切换系统语言回来试试'),
Container(
padding: EdgeInsets.all(16),
child: Text(DemoLocalizations.of(context).testDesc),
),
],
),
),
);
}
}
//Locale资源类
class DemoLocalizations {
DemoLocalizations(this.isZh);
//是否为中文
bool isZh = false;
//为了使用方便,我们定义一个静态方法
static DemoLocalizations of(BuildContext context) {
return Localizations.of<DemoLocalizations>(context, DemoLocalizations);
}
//Locale相关值,title为应用标题
String get testDesc {
return isZh ? "我是中文" : "I am English";
}
//... 其它的值
}
//Locale代理类
class DemoLocalizationsDelegate
extends LocalizationsDelegate<DemoLocalizations> {
const DemoLocalizationsDelegate();
//是否支持某个Local
@override
bool isSupported(Locale locale) => ['en', 'zh'].contains(locale.languageCode);
// Flutter会调用此类加载相应的Locale资源类
@override
Future<DemoLocalizations> load(Locale locale) {
return SynchronousFuture<DemoLocalizations>(
DemoLocalizations(locale.languageCode == "zh"));
}
@override
bool shouldReload(DemoLocalizationsDelegate old) => false;
}