forked from Sky24n/flutter_wanandroid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpinyin_page.dart
273 lines (262 loc) · 10.2 KB
/
pinyin_page.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
import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:lpinyin/lpinyin.dart';
class PinyinPage extends StatefulWidget {
final String title;
PinyinPage(this.title);
@override
State<StatefulWidget> createState() {
return new _PinyinPage();
}
}
class _PinyinPage extends State<PinyinPage> {
TextEditingController controller = new TextEditingController();
String _inputText = "天府广场";
PinyinFormat _pinyinFormat = PinyinFormat.WITH_TONE_MARK;
String _pinyinResult;
static const int TYPE_PINYIN = 1;
static const int TYPE_SIMPLIFIED = 2;
static const int TYPE_TRADITIONAL = 3;
int _convertType = TYPE_PINYIN;
WidgetUtil widgetUtil = new WidgetUtil();
@override
void initState() {
super.initState();
}
void textChange(String text) {
_inputText = text;
if (text == null || text.length == 0) {
setState(() {
_pinyinResult = "";
});
} else {
setState(() {
convertToPinyin(text);
});
}
}
void convertToPinyin(String text) {
switch (_convertType) {
case TYPE_PINYIN:
try {
_pinyinResult = PinyinHelper.getPinyin(text,
separator: " ", format: _pinyinFormat);
} catch (ex) {
_pinyinResult = ex.toString();
}
break;
case TYPE_SIMPLIFIED:
_pinyinResult = ChineseHelper.convertToSimplifiedChinese(text);
break;
case TYPE_TRADITIONAL:
_pinyinResult = ChineseHelper.convertToTraditionalChinese(text);
break;
}
}
@override
Widget build(BuildContext context) {
widgetUtil.asyncPrepare(context, true, (Rect rect) {
controller.text = _inputText;
textChange(_inputText);
});
return Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
centerTitle: true,
),
body: new Column(
children: <Widget>[
new Card(
elevation: 4.0,
margin: const EdgeInsets.all(10.0),
child: new Column(
children: <Widget>[
new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Padding(
padding: const EdgeInsets.only(
top: 16.0, left: 5.0, right: 5.0),
child: new Column(
children: <Widget>[
new Text("带声调",
style: new TextStyle(
fontSize: 12.0, color: Colors.grey[700])),
new Checkbox(
value: (_pinyinFormat ==
PinyinFormat.WITH_TONE_MARK &&
_convertType == TYPE_PINYIN),
onChanged: (value) {
if (value) {
setState(() {
_convertType = TYPE_PINYIN;
_pinyinFormat = PinyinFormat.WITH_TONE_MARK;
convertToPinyin(_inputText);
});
}
})
],
),
),
new Padding(
padding: const EdgeInsets.only(
top: 16.0, left: 5.0, right: 5.0),
child: new Column(
children: <Widget>[
new Text("不带声调",
style: new TextStyle(
fontSize: 12.0, color: Colors.grey[700])),
new Checkbox(
value:
(_pinyinFormat == PinyinFormat.WITHOUT_TONE &&
_convertType == TYPE_PINYIN),
onChanged: (value) {
if (value) {
setState(() {
_convertType = TYPE_PINYIN;
_pinyinFormat = PinyinFormat.WITHOUT_TONE;
convertToPinyin(_inputText);
});
}
})
],
),
),
new Padding(
padding: const EdgeInsets.only(
top: 16.0, left: 5.0, right: 5.0),
child: new Column(
children: <Widget>[
new Text("带数字声调",
style: new TextStyle(
fontSize: 12.0, color: Colors.grey[700])),
new Checkbox(
value: (_pinyinFormat ==
PinyinFormat.WITH_TONE_NUMBER &&
_convertType == TYPE_PINYIN),
onChanged: (value) {
if (value) {
setState(() {
_convertType = TYPE_PINYIN;
_pinyinFormat =
PinyinFormat.WITH_TONE_NUMBER;
convertToPinyin(_inputText);
});
}
})
],
),
),
new Padding(
padding: const EdgeInsets.only(
top: 16.0, left: 5.0, right: 5.0),
child: new Column(
children: <Widget>[
new Text("繁->简",
style: new TextStyle(
fontSize: 12.0, color: Colors.grey[700])),
new Checkbox(
value: _convertType == TYPE_SIMPLIFIED,
onChanged: (value) {
if (value) {
setState(() {
_convertType = TYPE_SIMPLIFIED;
convertToPinyin(_inputText);
});
}
})
],
),
),
new Padding(
padding: const EdgeInsets.only(
top: 16.0, left: 5.0, right: 5.0),
child: new Column(
children: <Widget>[
new Text("简->繁",
style: new TextStyle(
fontSize: 12.0, color: Colors.grey[700])),
new Checkbox(
value: _convertType == TYPE_TRADITIONAL,
onChanged: (value) {
if (value) {
setState(() {
_convertType = TYPE_TRADITIONAL;
convertToPinyin(_inputText);
});
}
})
],
),
),
],
),
new Container(
padding: const EdgeInsets.only(left: 10.0, right: 10.0),
child: new TextField(
controller: controller,
autofocus: false,
style:
new TextStyle(fontSize: 14.0, color: Colors.grey[900]),
decoration: new InputDecoration(
hintText: "请输入...",
hintStyle: new TextStyle(fontSize: 14.0)),
onChanged: (value) {
textChange(value);
},
),
),
new Container(
alignment: Alignment.topLeft,
height: 66.0,
padding: const EdgeInsets.all(10.0),
child: new Text(
'$_pinyinResult',
textAlign: TextAlign.start,
style:
new TextStyle(color: Colors.grey[600], fontSize: 14.0),
),
),
// new Container(
// height: 320.0,
// child: new Center(
// child: new Text(
// "" +
// "Flutter 国际化/多语言库" +
// "\n" +
// "https://github.com/Sky24n/fluintl" +
// "\n" +
// "\n" +
// "Dart 常用工具类库" +
// "\n" +
// "https://github.com/Sky24n/common_utils" +
// "\n" +
// "\n" +
// "Flutter 常用工具类库" +
// "\n" +
// "https://github.com/Sky24n/flustars" +
// "\n" +
// "\n" +
// "Flutter 汉字转拼音库" +
// "\n" +
// "https://github.com/flutterchina/lpinyin" +
// "\n" +
// "\n" +
// "Flutter 城市列表" +
// "\n" +
// "https://github.com/flutterchina/flukit" +
// "",
// style: new TextStyle(
// color: Colors.blueAccent, fontSize: 16.0),
// ),
// ),
// )
],
),
),
],
),
resizeToAvoidBottomPadding: false,
);
}
}