-
Notifications
You must be signed in to change notification settings - Fork 199
/
Copy pathbase.dart
534 lines (485 loc) · 16.8 KB
/
base.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
import 'dart:async';
import 'package:city_pickers/modal/base_citys.dart';
import 'package:flutter/material.dart';
import '../../modal/point.dart';
import '../../modal/result.dart';
import '../mod/inherit_process.dart';
import '../show_types.dart';
import '../util.dart';
import './pickers.dart';
class BaseView extends StatefulWidget {
final double? progress;
final String locationCode;
final ShowType showType;
final Map<String, String> provincesData;
final Map<String, dynamic> citiesData;
final ItemWidgetBuilder? itemBuilder;
/// 是否对数据进行排序
final bool isSort;
/// ios选择框的高度. 配合 itemBuilder中的字体使用.
final double? itemExtent;
/// 容器高度
final double height;
/// 取消按钮的Widget
/// 当用户设置该属性. 会优先使用用户设置的widget, 否则使用代码中默认的文本, 使用primary主题色
final Widget? cancelWidget;
/// 确认按钮的widget
/// 当用户设置该属性. 会优先使用用户设置的widget, 否则使用代码中默认的文本, 使用primary主题色
final Widget? confirmWidget;
final double borderRadius;
/// 是否开启全球化数据
final bool? global;
BaseView({
this.progress,
required this.showType,
required this.height,
required this.locationCode,
required this.citiesData,
required this.provincesData,
this.itemBuilder,
this.itemExtent,
this.cancelWidget,
this.confirmWidget,
this.isSort = false,
this.borderRadius = 0,
this.global = false,
}) : assert(!(itemBuilder != null && itemExtent == null),
"\ritemExtent could't be null if itemBuilder exits");
_BaseView createState() => _BaseView();
}
class _BaseView extends State<BaseView> {
Timer? _changeTimer;
bool _resetControllerOnce = false;
FixedExtentScrollController provinceController =
new FixedExtentScrollController();
FixedExtentScrollController cityController =
new FixedExtentScrollController();
FixedExtentScrollController areaController =
new FixedExtentScrollController();
FixedExtentScrollController villageController =
new FixedExtentScrollController(); // 增加第4级(村/镇)选择
// 所有省的列表. 因为性能等综合原因,
// 没有一次性构建整个以国为根的树. 动态的构建以省为根的树, 效率高.
late List<Point> provinces;
late CityTree cityTree;
late Point targetProvince;
Point? targetCity;
Point? targetArea;
Point? targetVillage; // 增加第4级(村/镇)选择
@override
void initState() {
super.initState();
provinces =
new Provinces(metaInfo: widget.provincesData, sort: widget.isSort)
.provinces;
cityTree = new CityTree(
metaInfo: widget.citiesData, provincesInfo: widget.provincesData);
try {
_initLocation(widget.locationCode);
_initController();
} catch (e) {
print('Exception details:\n 初始化地理位置信息失败, 请检查省分城市数据 \n $e');
}
}
void dispose() {
provinceController.dispose();
cityController.dispose();
areaController.dispose();
villageController.dispose();
// 增加第4级(村/镇)选择
if (_changeTimer?.isActive ?? false) {
_changeTimer!.cancel();
}
super.dispose();
}
// 初始化controller, 为了使给定的默认值, 在选框的中心位置
void _initController() {
ShowType showType = widget.showType;
if (showType.contain(ShowType.p)) {
provinceController = new FixedExtentScrollController(
initialItem:
provinces.indexWhere((Point p) => p.code == targetProvince.code));
}
if (showType.contain(ShowType.c)) {
cityController = new FixedExtentScrollController(
initialItem: targetProvince.children
.indexWhere((Point p) => p.code == targetCity!.code));
}
if (showType.contain(ShowType.a)) {
areaController = new FixedExtentScrollController(
initialItem: targetCity!.children
.indexWhere((Point p) => p.code == targetArea!.code));
}
// 增加第4级(村/镇)选择
if (showType.contain(ShowType.v)) {
villageController = new FixedExtentScrollController(
initialItem: targetArea!.children
.indexWhere((Point p) => p.code == targetVillage!.code));
}
}
// 重置Controller的原因在于, 无法手动去更改initialItem, 也无法通过
// jumpTo or animateTo去更改, 强行更改, 会触发 _onProvinceChange _onCityChange 与 _onAreacChange
// 只为覆盖初始化化的参数initialItem
void _resetController() {
if (_resetControllerOnce) return;
provinceController = new FixedExtentScrollController(initialItem: 0);
cityController = new FixedExtentScrollController(initialItem: 0);
areaController = new FixedExtentScrollController(initialItem: 0);
villageController =
new FixedExtentScrollController(initialItem: 0); // 增加第4级(村/镇)选择
_resetControllerOnce = true;
}
// initialize tree by locationCode
void _initLocation(String? locationCode) {
String _locationCode;
if (locationCode != null) {
try {
_locationCode = locationCode;
} catch (e) {
print(ArgumentError(
"The Argument locationCode must be valid like: '100000' but get '$locationCode' "));
return;
}
targetProvince = cityTree.initTreeByCode(_locationCode);
/// 为用户给出的locationCode不正确做一个容错
if (targetProvince.isNull) {
targetProvince = cityTree.initTreeByCode(provinces.first.code!);
}
targetProvince.children.forEach((Point _city) {
if (_city.code == _locationCode) {
targetCity = _city;
targetArea = _getTargetChildFirst(_city);
// 增加第4级(村/镇)选择
targetVillage = _getTargetChildFirst(targetArea!);
}
_city.children.forEach((Point _area) {
if (_area.code == _locationCode) {
targetCity = _city;
targetArea = _area;
// 增加第4级(村/镇)选择
targetVillage = _getTargetChildFirst(_area);
}
_area.children.forEach((Point _village) {
if (_village.code == _locationCode) {
targetCity = _city;
targetArea = _area;
// 增加第4级(村/镇)选择
targetVillage = _village;
}
});
});
});
} else {
/// 本来默认想定在北京, 但是由于有可能出现用户的省份数据为不包含北京, 所以采用第一个省份做为初始
targetProvince = cityTree.initTreeByCode(widget.provincesData.keys.first);
}
// 尝试试图匹配到下一个级别的第一个,
if (targetCity == null) {
targetCity = _getTargetChildFirst(targetProvince);
}
// 尝试试图匹配到下一个级别的第一个,
if (targetArea == null) {
targetArea = _getTargetChildFirst(targetCity!);
}
// 增加第4级(村/镇)选择
// 尝试试图匹配到下一个级别的第一个,
if (targetVillage == null) {
targetVillage = _getTargetChildFirst(targetArea!);
}
}
Point? _getTargetChildFirst(Point target) {
if (target == Point.nullPoint()) {
return Point.nullPoint();
}
if (target.children.isNotEmpty && target.children.isNotEmpty) {
return target.children.first;
}
return Point.nullPoint();
}
// 通过选中的省份, 构建以省份为根节点的树型结构
List<String> getCityItemList() {
List<String> result = [];
result.addAll(targetProvince.children.toList().map((p) => p.name).toList());
return result;
}
List<String> getAreaItemList() {
List<String> result = [];
if (targetCity != null) {
result.addAll(targetCity!.children.toList().map((p) => p.name).toList());
}
return result;
}
// 增加第4级(村/镇)选择
List<String> getVillageItemList() {
List<String> result = [];
if (targetArea != null) {
result.addAll(targetArea!.children.toList().map((p) => p.name).toList());
}
return result;
}
// province change handle
// 加入延时处理, 减少构建树的消耗
_onProvinceChange(Point _province) {
if (_changeTimer != null && _changeTimer!.isActive) {
_changeTimer!.cancel();
}
_changeTimer = new Timer(Duration(milliseconds: 100), () {
Point _provinceTree = cityTree.initTree(_province.code.toString());
setState(() {
targetProvince = _provinceTree;
targetCity = _getTargetChildFirst(_provinceTree);
if (!targetCity!.isNull) {
targetArea = _getTargetChildFirst(targetCity!);
targetVillage = _getTargetChildFirst(targetArea!); // 增加第4级(村/镇)选择
} else {
targetArea = Point.nullPoint();
targetVillage = Point.nullPoint();
}
_resetController();
});
});
}
_onCityChange(Point _targetCity) {
print('_onCityChange');
if (_changeTimer != null && _changeTimer!.isActive) {
_changeTimer!.cancel();
}
_changeTimer = new Timer(Duration(milliseconds: 100), () {
if (!mounted) return;
setState(() {
targetCity = _targetCity;
targetArea = _getTargetChildFirst(targetCity!);
if (!targetArea!.isNull) {
targetVillage = _getTargetChildFirst(targetArea!); // 增加第4级(村/镇)选择
} else {
targetVillage = Point.nullPoint();
}
});
});
_resetController();
}
_onAreaChange(Point _targetArea) {
if (_changeTimer != null && _changeTimer!.isActive) {
_changeTimer!.cancel();
}
_changeTimer = new Timer(Duration(milliseconds: 100), () {
if (!mounted) return;
setState(() {
targetArea = _targetArea;
targetVillage = _getTargetChildFirst(targetArea!);
});
});
}
// 增加第4级(村/镇)选择
_onVillageChange(Point _targetVillage) {
if (_changeTimer != null && _changeTimer!.isActive) {
_changeTimer!.cancel();
}
_changeTimer = new Timer(Duration(milliseconds: 100), () {
if (!mounted) return;
setState(() {
targetVillage = _targetVillage;
});
});
}
Result _buildResult() {
Result result = Result();
ShowType showType = widget.showType;
if (showType.contain(ShowType.p)) {
result.provinceId = targetProvince.code.toString();
result.provinceName = targetProvince.name;
}
if (showType.contain(ShowType.c)) {
result.provinceId = targetProvince.code.toString();
result.provinceName = targetProvince.name;
result.cityId = targetCity?.code.toString();
result.cityName = targetCity?.name;
}
if (showType.contain(ShowType.a)) {
result.provinceId = targetProvince.code.toString();
result.provinceName = targetProvince.name;
result.cityId = targetCity?.code.toString();
result.cityName = targetCity?.name;
result.areaId = targetArea?.code.toString();
result.areaName = targetArea?.name;
}
// 增加第4级(村/镇)选择
if (showType.contain(ShowType.v)) {
result.provinceId = targetProvince.code.toString();
result.provinceName = targetProvince.name;
result.cityId = targetCity!.code.toString();
result.cityName = targetCity?.name;
result.areaId = targetArea?.code.toString();
result.areaName = targetArea?.name;
result.villageId = targetVillage?.code.toString();
result.villageName = targetVillage?.name;
}
// 台湾异常数据. 需要过滤
// if (result.provinceId == "710000") {
// result.cityId = null;
// result.cityName = null;
// result.areaId = null;
// result.areaName = null;
// result.villageId = null;
// result.villageName = null;
// }
return result;
}
Widget _bottomBuild() {
List<Widget> pickerRows = [];
if (widget.showType.contain(ShowType.p)) {
pickerRows.add(new ScrollPicker(
key: Key('province'),
isShow: widget.showType.contain(ShowType.p),
controller: provinceController,
itemBuilder: widget.itemBuilder,
itemExtent: widget.itemExtent,
value: targetProvince.name,
itemList: provinces.toList().map((v) => v.name).toList(),
changed: (index) {
_onProvinceChange(provinces[index]);
},
));
}
if (widget.showType.contain(ShowType.c)) {
pickerRows.add(new ScrollPicker(
key: Key('citys'),
// 这个属性是为了强制刷新
isShow: widget.showType.contain(ShowType.c),
controller: cityController,
itemBuilder: widget.itemBuilder,
itemExtent: widget.itemExtent,
value: targetCity?.name,
itemList: getCityItemList(),
changed: (index) {
_onCityChange(targetProvince.children[index]);
},
));
}
if (widget.showType.contain(ShowType.a)) {
pickerRows.add(new ScrollPicker(
key: Key('towns'),
isShow: widget.showType.contain(ShowType.a),
controller: areaController,
itemBuilder: widget.itemBuilder,
itemExtent: widget.itemExtent,
value: targetArea?.name,
itemList: getAreaItemList(),
changed: (index) {
_onAreaChange(targetCity!.children[index]);
},
));
}
if (widget.showType.contain(ShowType.v)) {
pickerRows.add(new ScrollPicker(
// 增加第4级(村/镇)选择
// key: Key('villages'),
isShow: widget.showType.contain(ShowType.v),
controller: villageController,
itemBuilder: widget.itemBuilder,
itemExtent: widget.itemExtent,
value: targetVillage?.name,
itemList: getVillageItemList(),
changed: (index) {
_onVillageChange(targetArea!.children[index]);
},
));
}
return new Container(
width: double.infinity,
decoration: BoxDecoration(
color: Theme.of(context).scaffoldBackgroundColor,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(widget.borderRadius),
topRight: Radius.circular(widget.borderRadius)),
),
child: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
new Row(
children: <Widget>[
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: widget.cancelWidget ??
new Text(
'取消',
style: new TextStyle(
color: Theme.of(context).primaryColor,
),
),
),
TextButton(
onPressed: () {
Navigator.pop(context, _buildResult());
},
child: widget.confirmWidget ??
new Text(
'确定',
style: new TextStyle(
color: Theme.of(context).primaryColor,
),
),
),
],
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
),
Expanded(
child: new Row(
children: pickerRows,
),
)
],
));
}
Widget build(BuildContext context) {
final route = InheritRouteWidget.of(context)!.router;
return new AnimatedBuilder(
animation: route.animation!,
builder: (BuildContext context, Widget? child) {
return new CustomSingleChildLayout(
delegate: _WrapLayout(
progress: route.animation!.value, height: widget.height),
child: new GestureDetector(
child: new Material(
color: Colors.transparent,
child:
new Container(width: double.infinity, child: _bottomBuild()),
),
),
);
},
);
}
}
class _WrapLayout extends SingleChildLayoutDelegate {
_WrapLayout({
required this.progress,
required this.height,
});
final double progress;
final double height;
@override
BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
double maxHeight = height;
return new BoxConstraints(
minWidth: constraints.maxWidth,
maxWidth: constraints.maxWidth,
minHeight: 0.0,
maxHeight: maxHeight,
);
}
@override
Offset getPositionForChild(Size size, Size childSize) {
double height = size.height - childSize.height * progress;
return new Offset(0.0, height);
}
@override
bool shouldRelayout(_WrapLayout oldDelegate) {
return progress != oldDelegate.progress;
}
}