select_address_page.dart
5.35 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
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
import 'dart:async';
import 'dart:math';
import 'package:location/location.dart';
import 'package:flutter/material.dart';
import 'package:Parlando/widgets/my_button.dart';
import 'package:Parlando/widgets/search_bar.dart';
import 'package:getwidget/getwidget.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import '../../routers/fluro_navigator.dart';
import '../../util/toast_utils.dart';
class AddressSelectPage extends StatefulWidget {
const AddressSelectPage({Key? key}) : super(key: key);
@override
AddressSelectPageState createState() => AddressSelectPageState();
}
class AddressSelectPageState extends State<AddressSelectPage> {
// List<BMFSuggestionInfo> _list = [];
int _index = 0;
final ScrollController _controller = ScrollController();
LatLng _center = const LatLng(45.521563, -122.677433);
late GoogleMapController mapController;
bool isLoading = false;
int _markerIdCounter = 1;
Map<MarkerId, Marker> markers = <MarkerId, Marker>{};
late StreamSubscription _locationSubscription;
@override
void dispose() {
_controller.dispose();
_locationSubscription.cancel();
super.dispose();
}
@override
void initState() {
super.initState();
_getCurrentLocation();
}
Future<void> _getCurrentLocation() async {
Location location = Location();
bool serviceEnabled;
PermissionStatus permissionGranted;
serviceEnabled = await location.serviceEnabled();
if (!serviceEnabled) {
serviceEnabled = await location.requestService();
if (!serviceEnabled) {
return;
}
}
permissionGranted = await location.hasPermission();
if (permissionGranted == PermissionStatus.denied) {
permissionGranted = await location.requestPermission();
if (permissionGranted != PermissionStatus.granted) {
return;
}
}
_locationSubscription =
location.onLocationChanged.listen((LocationData currentLocation) {
_center = LatLng(currentLocation.latitude!, currentLocation.longitude!);
final String markerIdVal = 'marker_id_$_markerIdCounter';
_markerIdCounter++;
final MarkerId markerId = MarkerId(markerIdVal);
final Marker marker = Marker(
markerId: markerId,
position: LatLng(currentLocation.latitude!, currentLocation.longitude!),
);
LatLng sydney =
LatLng(currentLocation.latitude!, currentLocation.longitude!);
mapController.moveCamera(CameraUpdate.newLatLng(sydney));
setState(() {
markers[markerId] = marker;
});
});
}
void _onMapCreated(GoogleMapController controller) {
mapController = controller;
}
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
appBar: SearchBar(
hintText: '搜索地址',
onPressed: (text) async {
isLoading = true;
_controller.animateTo(0.0,
duration: const Duration(milliseconds: 10), curve: Curves.ease);
_index = 0;
// 构造检索参数
},
),
body: SafeArea(
child: Column(
children: <Widget>[
Expanded(
flex: 9,
child: GoogleMap(
onMapCreated: _onMapCreated,
initialCameraPosition: CameraPosition(
target: _center,
zoom: 16.0,
),
markers: Set<Marker>.of(markers.values),
),
),
Expanded(
flex: 11,
child: isLoading
? const GFLoader()
: ListView.separated(
controller: _controller,
itemCount: 1,
separatorBuilder: (_, index) => const Divider(),
itemBuilder: (_, index) {
return _AddressItem(
isSelected: _index == index,
onTap: () {
_index = index;
setState(() {});
},
);
},
),
),
MyButton(
onPressed: () {
// if (_list.isEmpty) {
// Toast.show('未选择地址!');
// return;
// }
// NavigatorUtils.goBackWithParams(context, _list[_index]);
},
text: '确认选择地址',
)
],
),
),
);
}
}
class _AddressItem extends StatelessWidget {
const _AddressItem({
Key? key,
this.isSelected = false,
this.onTap,
}) : super(key: key);
// final BMFSuggestionInfo poi;
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(
'', // '${poi.city} ${poi.district} ${poi.address}',
),
),
Visibility(
visible: isSelected,
child: const Icon(Icons.done, color: Colors.blue),
)
],
),
),
);
}
}