-
-
Notifications
You must be signed in to change notification settings - Fork 238
/
main.dart
358 lines (323 loc) · 11.8 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
import 'package:flustars/flustars.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
_initAsync();
/// 配置设计稿尺寸
/// 如果设计稿尺寸默认配置一致,无需该设置。默认 width:360.0 / height:640.0 / density:3.0
/// Configuration design draft size.
/// If the default configuration of design draft size is the same, this setting is not required. default width:360.0 / height:640.0 / density:3.0
setDesignWHD(360.0, 640.0, density: 3);
}
/// SpUtil example.
void _initAsync() async {
await SpUtil.getInstance();
SpUtil.putString("username", "sky24");
String? userName = SpUtil.getString("username", defValue: "");
LogUtil.e("userName: $userName");
/// save object example.
/// 存储实体对象示例。
City city = City(name: "成都市");
SpUtil.putObject("loc_city", city);
City? hisCity = SpUtil.getObj(
"loc_city", (v) => City.fromJson(v as Map<String, dynamic>));
LogUtil.e("City: " + (hisCity == null ? "null" : hisCity.toString()));
/// save object list example.
/// 存储实体对象list示例。
List<City> list = [];
list.add(City(name: "成都市"));
list.add(City(name: "北京市"));
SpUtil.putObjectList("loc_city_list", list);
List<City>? dataList = SpUtil.getObjList(
"loc_city_list", (v) => City.fromJson(v as Map<String, dynamic>));
LogUtil.e("CityList: " + (dataList == null ? "null" : dataList.toString()));
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MainPage(),
);
}
}
class MainPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return MainPageState();
}
}
/// 在MainPage使用依赖不context方法获取屏幕参数及适配,需要build方法内调用[MediaQuery.of(context)]。
/// 或者使用依赖context方法获取屏幕参数及适配。
/// In MainPage, the dependency-free context method is used to obtain screen parameters and adaptions, which requires a call to [MediaQuery. of (context)] within the build method.
/// Or use context-dependent methods to obtain screen parameters and adaptions.
class MainPageState extends State<MainPage> {
void test2() async {
LogUtil.e("xxxxxxxxxxx test7......");
await DirectoryUtil.getInstance();
String? tempPath = DirectoryUtil.getTempPath(
category: 'Pictures', fileName: 'demo', format: 'png');
LogUtil.e("tempPath: $tempPath");
String? appDocPath = DirectoryUtil.getAppDocPath(
category: 'Pictures', fileName: 'demo', format: 'png');
LogUtil.e("appDocPath: $appDocPath");
String? appSupportPath = DirectoryUtil.getAppSupportPath(
category: 'Pictures', fileName: 'demo', format: 'png');
LogUtil.e("appSupportPath: $appSupportPath");
String? storagePath = DirectoryUtil.getStoragePath(
category: 'Pictures', fileName: 'demo', format: 'png');
LogUtil.e("storagePath: $storagePath");
}
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
/// 如果使用依赖不context方法获取屏幕参数及适配,需要调用此方法。
/// If you use a dependent context-free method to obtain screen parameters and adaptions, you need to call this method.
MediaQuery.of(context);
double statusBar = ScreenUtil.getInstance().statusBarHeight;
double width = ScreenUtil.getInstance().screenWidth;
double height = ScreenUtil.getInstance().screenHeight;
double density = ScreenUtil.getInstance().screenDensity;
double sp = ScreenUtil.getInstance().getAdapterSize(24);
double spc = ScreenUtil.getInstance().getAdapterSize(24);
double adapterW = ScreenUtil.getInstance().getAdapterSize(360);
LogUtil.e(
"MainPage statusBar: $statusBar, width: $width, height: $height, density: $density, sp: $sp, spc: $spc, adapterW: $adapterW");
return Scaffold(
// 一个不需要GlobalKey就可以openDrawer的AppBar
appBar: AppBar(
leading: Builder(builder: (BuildContext ctx) {
return IconButton(
icon: ClipOval(
child: Image.asset(('assets/images/ali_connors.png')),
),
onPressed: () {
Scaffold.of(ctx).openDrawer();
});
}),
title: const Text('Flustars Demos'),
centerTitle: true,
actions: <Widget>[
IconButton(
icon: Icon(Icons.search),
onPressed: () {
LogUtil.e("onPressed......");
//test2();
// Navigator.push(context,
// CupertinoPageRoute<void>(builder: (ctx) => SecondPage()));
},
),
],
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
width: 360.0,
height: 50,
color: Colors.grey,
child: Center(
child: Text(
"未适配宽",
style: TextStyle(fontSize: 24.0),
),
),
),
Container(
width: ScreenUtil.getInstance().getAdapterSize(360.0),
height: 50,
color: Colors.grey,
child: Center(
child: Text(
"已适配宽",
style: TextStyle(fontSize: 24.0),
),
),
),
Container(
width: 100,
height: 100,
color: Colors.grey,
child: Center(
child: Text(
"你好你好你好",
style: TextStyle(fontSize: 24.0),
),
),
),
Container(
margin: EdgeInsets.only(top: 10.0),
width: ScreenUtil.getInstance().getAdapterSize(100.0),
height: ScreenUtil.getInstance().getAdapterSize(100.0),
color: Colors.grey,
child: Center(
child: Text(
"你好你好你好",
style: TextStyle(
fontSize: ScreenUtil.getInstance().getAdapterSize(24.0)),
),
),
),
],
),
drawer: MyDrawer(),
);
}
}
class MyDrawer extends StatelessWidget {
@override
Widget build(BuildContext context) {
double statusBar = ScreenUtil.getInstance().statusBarHeight;
double width = ScreenUtil.getInstance().screenWidth;
double height = ScreenUtil.getInstance().screenHeight;
LogUtil.e("MyDrawer statusBar: $statusBar, width: $width, height: $height");
return Container(
color: Colors.white,
width: ScreenUtil.getInstance().getWidth(240),
child: ListView(
padding: EdgeInsets.zero,
children: <Widget>[
Container(
color: Colors.teal,
padding:
EdgeInsets.only(top: ScreenUtil.getInstance().statusBarHeight),
child: Center(
child: Text(
"Sky24n",
style: TextStyle(fontSize: 16, color: Colors.white),
),
),
height: 160,
)
],
),
);
}
}
class SecondPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return SecondPageState();
}
}
class SecondPageState extends State<SecondPage> {
@override
void initState() {
super.initState();
_init();
// _initWithCtx();
}
void _init() {
double screenWidth = ScreenUtil.getInstance().screenWidth;
double screenHeight = ScreenUtil.getInstance().screenHeight;
double screenDensity = ScreenUtil.getInstance().screenDensity;
double statusBarHeight = ScreenUtil.getInstance().statusBarHeight;
double bottomBarHeight = ScreenUtil.getInstance().bottomBarHeight;
double appBarHeight = ScreenUtil.getInstance().appBarHeight;
double adapterW100 = ScreenUtil.getInstance().getWidth(100);
double adapterH100 = ScreenUtil.getInstance().getHeight(100);
double adapterSp100 = ScreenUtil.getInstance().getSp(100);
double adapterW100px = ScreenUtil.getInstance().getWidthPx(300);
double adapterH100px = ScreenUtil.getInstance().getHeightPx(300);
LogUtil.e("SecondPage _init screenWidth: $screenWidth, screenHeight: $screenHeight, screenDensity: $screenDensity" +
", statusBarHeight: $statusBarHeight, bottomBarHeight: $bottomBarHeight, appBarHeight: $appBarHeight" +
", adapterW100: $adapterW100, adapterH100: $adapterH100, adapterSp100: $adapterSp100" +
", adapterW100px: $adapterW100px, adapterH100px: $adapterH100px");
}
void _initWithCtx() {
double screenWidth = ScreenUtil.getScreenW(context);
double screenHeight = ScreenUtil.getScreenH(context);
double screenDensity = ScreenUtil.getScreenDensity(context);
double statusBarHeight = ScreenUtil.getStatusBarH(context);
double bottomBarHeight = ScreenUtil.getBottomBarH(context);
double adapterW100 = ScreenUtil.getScaleW(context, 100);
double adapterH100 = ScreenUtil.getScaleH(context, 100);
double adapterSp100 = ScreenUtil.getScaleSp(context, 100);
Orientation orientation = ScreenUtil.getOrientation(context);
LogUtil.e("SecondPage _initWithCtx screenWidth: $screenWidth, screenHeight: $screenHeight, screenDensity: $screenDensity" +
", statusBarHeight: $statusBarHeight, bottomBarHeight: $bottomBarHeight" +
", adapterW100: $adapterW100, adapterH100: $adapterH100, adapterSp100: $adapterSp100");
}
@override
Widget build(BuildContext context) {
double statusBar = ScreenUtil.getInstance().statusBarHeight;
double width = ScreenUtil.getInstance().screenWidth;
double height = ScreenUtil.getInstance().screenHeight;
LogUtil.e(
"SecondPage statusBar: $statusBar, width: $width, height: $height");
return Scaffold(
appBar: AppBar(
title: Text("Second Page"),
centerTitle: true,
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
width: 100,
height: 100,
color: Colors.grey,
child: Center(
child: Text(
"你好你好你好",
style: TextStyle(fontSize: 24.0),
),
),
),
Container(
margin: EdgeInsets.only(top: 10.0),
width: ScreenUtil.getInstance().getAdapterSize(100.0),
height: ScreenUtil.getInstance().getAdapterSize(100.0),
color: Colors.grey,
child: Center(
child: Text(
"你好你好你好",
style: TextStyle(
fontSize: ScreenUtil.getInstance().getAdapterSize(24.0)),
),
),
),
Container(
margin: EdgeInsets.only(top: 10.0),
width: ScreenUtil.getAdapterSizeCtx(context, 100.0),
height: ScreenUtil.getAdapterSizeCtx(context, 100.0),
color: Colors.grey,
child: Center(
child: Text(
"你好你好你好",
style: TextStyle(
fontSize: ScreenUtil.getAdapterSizeCtx(context, 24.0)),
),
),
),
],
),
);
}
}
class City {
String name;
City({required this.name});
/// must.
City.fromJson(Map<String, dynamic> json) : name = json['name'];
/// must.
Map<String, dynamic> toJson() => {
'name': name,
};
@override
String toString() {
StringBuffer sb = StringBuffer('{');
sb.write("\"name\":\"$name\"");
sb.write('}');
return sb.toString();
}
}