-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.dart
285 lines (275 loc) · 9.63 KB
/
main.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_opencc_ffi/flutter_opencc_ffi.dart';
import 'package:flutter_opencc_ffi_example/pressure.dart';
import 'package:flutter_opencc_ffi_example/utils.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final GlobalKey<ScaffoldMessengerState> _scaffoldMessengerKey = GlobalKey();
final List<String> types = ["s2t", "t2s", "s2hk", "hk2s", "s2tw", "tw2s", "s2twp", "tw2sp", "t2hk", "hk2t", "t2jp", "jp2t", "t2tw", "tw2t"];
late final Map<String, Converter> converters = {};
late final TextEditingController textEditingController;
final List<bool> originSelections = [true, false, false];
final List<String> originTypes = ['s', 't', 'jp'];
final List<bool> targetSelections = [false, true, false];
final List<String> targetTypes = ['s', 't', 'jp'];
final List<bool> variantSelections = [true, false, false];
final List<String> variantTypes = ['', 'w', 'hk'];
final List<bool> idiomSelections = [true, false, false];
final List<String> idiomTypes = ['', '', 'p'];
bool initialized = false;
String result = '';
String error = '';
String type = '';
bool runningPressureTest = false;
@override
void initState() {
super.initState();
textEditingController = TextEditingController(
text: '''
夸夸其谈 夸父逐日
我干什么不干你事。
太后的头发很干燥。
燕燕于飞,差池其羽。之子于归,远送于野。
请成相,世之殃,愚暗愚暗堕贤良。人主无贤,如瞽无相何伥伥!请布基,慎圣人,愚而自专事不治。主忌苟胜,群臣莫谏必逢灾。
曾经有一份真诚的爱情放在我面前,我没有珍惜,等我失去的时候我才后悔莫及。人事间最痛苦的事莫过于此。如果上天能够给我一个再来一次得机会,我会对那个女孩子说三个字,我爱你。如果非要在这份爱上加个期限,我希望是,一万年。
新的理论被发现了。
金胄不是金色的甲胄。
经理发现后劝谕两人
想到自己一紧张就口吃,我就没胃口吃饭
'''
.trim());
type = getType();
copyAssets().then((dataDir) {
for (String type in types) {
converters[type] = createConverter(kIsWeb ? type : '$dataDir/$type.json');
}
if (mounted) {
setState(() {
initialized = true;
});
}
});
}
String joinOriginTypes(String origin, String variant, String idiom) {
if (origin == 's' || origin == 'jp') {
return origin;
}
if (variant == 'hk') {
return variant;
}
// t, tw, twp
return '$origin$variant$idiom';
}
String joinTargetTypes(String target, String variant, String idiom) {
if (target == 'jp') {
return target;
}
if (target == 's') {
// s, sp
return '$target$idiom';
}
if (variant == 'hk') {
return variant;
}
// t, tw, twp
return '$target$variant$idiom';
}
String getType() {
String originType = originTypes[originSelections.indexWhere((element) => element)];
String targetType = targetTypes[targetSelections.indexWhere((element) => element)];
String variantType = variantTypes[variantSelections.indexWhere((element) => element)];
String idiomType = idiomTypes[idiomSelections.indexWhere((element) => element)];
String origin = joinOriginTypes(originType, variantType, idiomType);
String target = joinTargetTypes(targetType, variantType, idiomType);
String type = '${origin}2$target';
return type;
}
void convert() {
String source = textEditingController.text;
if (!types.contains(type)) {
return;
}
if (mounted) {
setState(() {
result = converters[type]!.convert(source);
});
}
}
@override
Widget build(BuildContext context) {
const gutter = Padding(
padding: EdgeInsets.only(top: 6, left: 6),
);
return MaterialApp(
scaffoldMessengerKey: _scaffoldMessengerKey,
home: Scaffold(
appBar: AppBar(
title: const Text('開放中文轉換 (Open Chinese Convert)'),
actions: [
ElevatedButton(
onPressed: runningPressureTest ? null : () {
setState(() {
runningPressureTest = true;
});
runPressureForConvertList().whenComplete(() {
if(mounted) {
setState(() {
runningPressureTest = false;
});
}
});
},
child: runningPressureTest ? const CircularProgressIndicator() : const Text('Pressure'),
),
],
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(6),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
const Text('原文'),
gutter,
Expanded(
child: ToggleButtons(
isSelected: originSelections,
onPressed: (int index) {
for (int i = 0; i < originSelections.length; i++) {
originSelections[i] = i == index ? true : false;
}
if (mounted) {
setState(() {
type = getType();
});
}
},
children: const [
Text('簡體中文'),
Text('繁體中文'),
Text('日文新字體'),
],
),
),
],
),
gutter,
Row(
children: [
const Text('目標'),
gutter,
Expanded(
child: ToggleButtons(
isSelected: targetSelections,
onPressed: (int index) {
for (int i = 0; i < targetSelections.length; i++) {
targetSelections[i] = i == index ? true : false;
}
if (mounted) {
setState(() {
type = getType();
});
}
},
children: const [
Text('簡體中文'),
Text('繁體中文'),
Text('日文新字體'),
],
),
),
],
),
gutter,
Row(
children: [
const Text('異體字轉換'),
gutter,
Expanded(
child: ToggleButtons(
isSelected: variantSelections,
onPressed: (int index) {
for (int i = 0; i < variantSelections.length; i++) {
variantSelections[i] = i == index ? true : false;
}
if (mounted) {
setState(() {
type = getType();
});
}
},
children: const [
Text('OpenCC標準'),
Text('臺灣標準'),
Text('香港標準'),
],
),
),
],
),
gutter,
Row(
children: [
const Text('地域用詞轉換'),
gutter,
Expanded(
child: ToggleButtons(
isSelected: idiomSelections,
onPressed: (int index) {
for (int i = 0; i < idiomSelections.length; i++) {
idiomSelections[i] = i == index ? true : false;
}
if (mounted) {
setState(() {
type = getType();
});
}
},
children: const [
Text('不轉換'),
Text('中國大陸模式'),
Text('臺灣模式'),
],
),
),
],
),
gutter,
Text(
type,
style: TextStyle(color: types.contains(type) ? Colors.green : Colors.red),
),
gutter,
TextField(
controller: textEditingController,
maxLines: 6,
),
gutter,
Container(
width: double.infinity,
decoration: BoxDecoration(
border: Border.all(color: Colors.blue),
),
padding: const EdgeInsets.all(6),
child: Text(result, softWrap: true),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: convert,
child: const Icon(Icons.translate),
),
),
);
}
}