-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.dart
121 lines (112 loc) · 3.58 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
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:license_plate_number/license_plate_number.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
Brightness _brightness = Brightness.light;
PlateStyles _plateStyles = PlateStyles.light;
final KeyboardController _keyboardController = KeyboardController();
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'License plate number Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
brightness: _brightness,
),
home: Scaffold(
appBar: AppBar(
title: Text('License plate number'),
actions: [
/// AppBar 弹出菜单
PopupMenuButton(
itemBuilder: (context) => [
PopupMenuItem(child: Text('dark'), value: 'dark'),
PopupMenuItem(child: Text('light'), value: 'light'),
],
onSelected: (action) {
switch (action) {
/// 切换成黑暗主题
case 'dark':
_plateStyles = PlateStyles.dark;
_brightness = Brightness.dark;
break;
/// 切换成明亮主题
case 'light':
_plateStyles = PlateStyles.light;
_brightness = Brightness.light;
break;
}
setState(() {});
},
),
],
),
body: GestureDetector(
behavior: HitTestBehavior.translucent,
child: MyHomePage(
plateStyles: _plateStyles,
keyboardController: _keyboardController,
),
onTap: () {
/// 点击键盘外区域收起键盘
_keyboardController.hideKeyboard();
},
),
),
);
}
}
class MyHomePage extends StatelessWidget {
MyHomePage({
this.plateStyles,
this.keyboardController,
});
final PlateStyles plateStyles;
final KeyboardController keyboardController;
@override
Widget build(BuildContext context) {
MediaQueryData mediaQuery = MediaQuery.of(context);
double screenWidth = mediaQuery.size.width;
double screenHeight = mediaQuery.size.height;
double inputFieldWidth = min(screenWidth, screenHeight) / 10;
double inputFieldHeight = inputFieldWidth * 4 / 3;
if (screenWidth < screenHeight) {
SystemChrome.setEnabledSystemUIOverlays(
[SystemUiOverlay.top, SystemUiOverlay.bottom]);
} else {
SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.bottom]);
}
return Container(
alignment: Alignment.topCenter,
padding: EdgeInsets.only(top: 30),
/// 车牌号输入框
child: PlateInputField(
styles: plateStyles,
inputFieldWidth: inputFieldWidth,
inputFieldHeight: inputFieldHeight,
keyboardController: keyboardController,
plateSeparatorPadding: 8,
plateSeparatorSize: 6,
onChanged: (List<String> array, String value) {
debugPrint('--- changed ---');
debugPrint(array.toString());
debugPrint(value);
},
onCompleted: (List<String> array, String value) {
debugPrint('--- completed ---');
debugPrint(array.toString());
debugPrint(value);
},
),
);
}
}