/
main.dart
368 lines (347 loc) · 10.7 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:flutter/material.dart';
import 'package:thai_idcard_reader_flutter/thai_idcard_reader_flutter.dart';
import 'package:thai_idcard_reader_flutter/common.dart';
import 'package:intl/date_symbol_data_local.dart';
import 'dart:typed_data';
import 'package:intl/intl.dart';
void main() {
Intl.defaultLocale = 'th_TH';
initializeDateFormatting('th_TH', null);
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
ThaiIDCard? _data;
var _error;
UsbDevice? _device;
var _card;
StreamSubscription? subscription;
final List _idCardType = [
ThaiIDType.cid,
ThaiIDType.photo,
ThaiIDType.nameTH,
ThaiIDType.nameEN,
ThaiIDType.gender,
ThaiIDType.birthdate,
ThaiIDType.address,
ThaiIDType.issueDate,
ThaiIDType.expireDate,
];
List<String> selectedTypes = [];
@override
void initState() {
super.initState();
ThaiIdcardReaderFlutter.deviceHandlerStream.listen(_onUSB);
}
void _onUSB(usbEvent) {
try {
if (usbEvent.hasPermission) {
subscription =
ThaiIdcardReaderFlutter.cardHandlerStream.listen(_onData);
} else {
if (subscription == null) {
subscription?.cancel();
subscription = null;
}
_clear();
}
setState(() {
_device = usbEvent;
});
} catch (e) {
setState(() {
_error = "_onUSB " + e.toString();
});
}
}
void _onData(readerEvent) {
try {
setState(() {
_card = readerEvent;
});
if (readerEvent.isReady) {
readCard(only: selectedTypes);
} else {
_clear();
}
} catch (e) {
setState(() {
_error = "_onData " + e.toString();
});
}
}
readCard({List<String> only = const []}) async {
try {
var response = await ThaiIdcardReaderFlutter.read(only: only);
setState(() {
_data = response;
});
} catch (e) {
setState(() {
_error = 'ERR readCard $e';
});
}
}
formattedDate(dt) {
try {
DateTime dateTime = DateTime.parse(dt);
String formattedDate = DateFormat.yMMMMd('th_TH').format(dateTime);
return formattedDate;
} catch (e) {
return dt.split('').toString() + e.toString();
}
}
_clear() {
setState(() {
_data = null;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Thai ID Card Reader'),
),
body: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (_device != null)
UsbDeviceCard(
device: _device,
),
if (_card != null) Text(_card.toString()),
if (_device == null || !_device!.isAttached) ...[
const EmptyHeader(
text: 'เสียบเครื่องอ่านบัตรก่อน',
),
],
if (_error != null) Text(_error.toString()),
if (_data == null &&
(_device != null && _device!.hasPermission)) ...[
const EmptyHeader(
icon: Icons.credit_card,
text: 'เสียบบัตรประชาชนได้เลย',
),
SizedBox(
height: 200,
child: Wrap(children: [
Row(
mainAxisSize: MainAxisSize.min,
children: [
Checkbox(
value: selectedTypes.isEmpty,
onChanged: (val) {
setState(() {
if (selectedTypes.isNotEmpty) {
selectedTypes = [];
}
});
}),
const Text('readAll'),
],
),
for (var ea in _idCardType)
Row(
mainAxisSize: MainAxisSize.min,
children: [
Checkbox(
value: selectedTypes.contains(ea),
onChanged: (val) {
print(ea);
setState(() {
if (selectedTypes.contains(ea)) {
selectedTypes.remove(ea);
} else {
selectedTypes.add(ea);
}
});
}),
Text('$ea'),
],
),
]),
),
],
if (_data != null) ...[
const Padding(padding: EdgeInsets.all(8.0)),
if (_data!.photo.isNotEmpty)
Center(
child: Image.memory(
Uint8List.fromList(_data!.photo),
),
),
if (_data!.cid != null)
DisplayInfo(title: 'เลขบัตรประชาชน', value: _data!.cid!),
if (_data!.firstnameTH != null)
DisplayInfo(
title: 'ชื่อ-นามสกุล (ภาษาไทย)',
value:
'${_data!.titleTH} ${_data!.firstnameTH} ${_data?.lastnameTH!}'),
if (_data!.firstnameEN != null)
DisplayInfo(
title: 'ชื่อ-นามสกุล (ภาษาอังกฤษ)',
value:
'${_data!.titleEN} ${_data!.firstnameEN} ${_data!.lastnameEN}'),
if (_data!.gender != null)
DisplayInfo(
title: 'เพศ',
value:
'(${_data!.gender}) ${_data!.gender == 1 ? 'ชาย' : 'หญิง'}'),
if (_data!.birthdate != null)
DisplayInfo(
title: 'วันเดือนปีเกิด',
value:
'${_data!.birthdate.toString()}\n${formattedDate(_data!.birthdate)}'),
if (_data!.address != null)
DisplayInfo(title: 'ที่อยู่', value: _data!.address!),
if (_data!.issueDate != null)
DisplayInfo(
title: 'วันออกบัตร',
value:
'${_data!.issueDate.toString()}\n${formattedDate(_data!.issueDate)}'),
if (_data!.expireDate != null)
DisplayInfo(
title: 'วันหมดอายุ',
value:
'${_data!.expireDate.toString()}\n${formattedDate(_data!.expireDate)}'),
],
],
),
),
),
);
}
}
class EmptyHeader extends StatelessWidget {
final IconData? icon;
final String? text;
const EmptyHeader({
this.icon,
this.text,
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Card(
child: SizedBox(
height: 300,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
icon ?? Icons.usb,
size: 60,
),
Center(
child: Text(
text ?? 'Empty',
textAlign: TextAlign.center,
style: const TextStyle(
fontSize: 32,
fontWeight: FontWeight.bold,
),
)),
],
)));
}
}
class UsbDeviceCard extends StatelessWidget {
final dynamic device;
const UsbDeviceCard({
Key? key,
this.device,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Opacity(
opacity: device.isAttached ? 1.0 : 0.5,
child: Card(
child: ListTile(
leading: const Icon(
Icons.usb,
size: 32,
),
title: Text('${device!.manufacturerName} ${device!.productName}'),
subtitle: Text(device!.identifier ?? ''),
trailing: Container(
padding: const EdgeInsets.all(8),
color: device!.hasPermission ? Colors.green : Colors.grey,
child: Text(
device!.hasPermission
? 'Listening'
: (device!.isAttached ? 'Connected' : 'Disconnected'),
style: const TextStyle(
fontWeight: FontWeight.bold,
)),
),
),
),
);
}
}
class DisplayInfo extends StatelessWidget {
const DisplayInfo({
Key? key,
required this.title,
required this.value,
}) : super(key: key);
final String title;
final String value;
@override
Widget build(BuildContext context) {
TextStyle sTitle =
const TextStyle(fontSize: 24, fontWeight: FontWeight.bold);
TextStyle sVal = const TextStyle(fontSize: 28);
_copyFn(value) {
Clipboard.setData(ClipboardData(text: value)).then((_) {
ScaffoldMessenger.of(context)
.showSnackBar(const SnackBar(content: Text("Copy it already")));
});
}
return Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
Row(
children: [
Text(
'$title : ',
style: sTitle,
),
],
),
Stack(
alignment: Alignment.centerRight,
children: [
Row(
children: [
Flexible(
child: Text(
value,
style: sVal,
),
),
],
),
GestureDetector(
onTap: () => _copyFn(value),
child: const Icon(Icons.copy),
)
],
),
const Divider(
color: Colors.black,
),
],
),
);
}
}