select_address_page.dart
4.32 KB
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
import 'package:flutter/material.dart';
import 'package:Parlando/routers/fluro_navigator.dart';
import 'package:Parlando/util/toast_utils.dart';
import 'package:Parlando/widgets/my_button.dart';
import 'package:Parlando/widgets/search_bar.dart';
import 'package:flutter_baidu_mapapi_map/flutter_baidu_mapapi_map.dart';
import 'package:flutter_baidu_mapapi_base/flutter_baidu_mapapi_base.dart';
import '../../map/flutter_2d_amap.dart';
import '../../map/interface/amap_2d_controller.dart';
import '../../map/poi_search_model.dart';
class AddressSelectPage extends StatefulWidget {
const AddressSelectPage({Key? key}) : super(key: key);
@override
AddressSelectPageState createState() => AddressSelectPageState();
}
class AddressSelectPageState extends State<AddressSelectPage> {
List<PoiSearch> _list = [];
int _index = 0;
final ScrollController _controller = ScrollController();
AMap2DController? _aMap2DController;
late BMFMapController myMapController;
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
void initState() {
super.initState();
// TODO 需要根据项目单独设置keys
Flutter2dAMap.setApiKey(
iOSKey: 'd94fbf50f5bfa86cd4e793c9ed4a9a97',
);
}
/// 创建完成回调
void onBMFMapCreated(BMFMapController controller) {
myMapController = controller;
/// 地图加载回调
myMapController.setMapDidLoadCallback(callback: () {
print('mapDidLoad-地图加载完成');
});
}
/// 设置地图参数
BMFMapOptions initMapOptions() {
BMFMapOptions mapOptions = BMFMapOptions(
center: BMFCoordinate(39.917215, 116.380341),
zoomLevel: 12,
);
return mapOptions;
}
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
appBar: SearchBar(
hintText: '搜索地址',
onPressed: (text) {
_controller.animateTo(0.0,
duration: const Duration(milliseconds: 10), curve: Curves.ease);
_index = 0;
_aMap2DController?.search(text);
},
),
body: SafeArea(
child: Column(
children: <Widget>[
Expanded(
flex: 9,
child: BMFMapWidget(
onBMFMapCreated: onBMFMapCreated,
mapOptions: initMapOptions(),
),
),
Expanded(
flex: 11,
child: ListView.separated(
controller: _controller,
itemCount: _list.length,
separatorBuilder: (_, index) => const Divider(),
itemBuilder: (_, index) {
return _AddressItem(
isSelected: _index == index,
date: _list[index],
onTap: () {
_index = index;
_aMap2DController?.move(
_list[index].latitude!, _list[index].longitude!);
setState(() {});
},
);
},
),
),
MyButton(
onPressed: () {
if (_list.isEmpty) {
Toast.show('未选择地址!');
return;
}
NavigatorUtils.goBackWithParams(context, _list[_index]);
},
text: '确认选择地址',
)
],
),
),
);
}
}
class _AddressItem extends StatelessWidget {
const _AddressItem({
Key? key,
required this.date,
this.isSelected = false,
this.onTap,
}) : super(key: key);
final PoiSearch date;
final bool isSelected;
final GestureTapCallback? onTap;
@override
Widget build(BuildContext context) {
return InkWell(
onTap: onTap,
child: Container(
alignment: Alignment.centerLeft,
padding: const EdgeInsets.symmetric(horizontal: 16.0),
height: 50.0,
child: Row(
children: <Widget>[
Expanded(
child: Text(
'${date.provinceName!} ${date.cityName!} ${date.adName!} ${date.title!}',
),
),
Visibility(
visible: isSelected,
child: const Icon(Icons.done, color: Colors.blue),
)
],
),
),
);
}
}