Chad

优化代码

1 +import 'package:flutter/material.dart';
2 +import 'package:hostex_flutter/generated/l10n.dart';
3 +import 'package:hostex_flutter/generated/assets.dart';
4 +import 'package:hostex_flutter/ui/extension/widget_ext.dart';
5 +
6 +extension WidgetExt on Widget {
7 + Expanded expanded({int flex = 1}) {
8 + return Expanded(flex: flex, child: this);
9 + }
10 +
11 + SafeArea safe() {
12 + return SafeArea(child: this);
13 + }
14 +}
1 import 'dart:async'; 1 import 'dart:async';
2 import 'dart:convert'; 2 import 'dart:convert';
3 +import 'package:Parlando/extension/widget_ext.dart';
3 import 'package:Parlando/models/nearby_response.dart' as nearby; 4 import 'package:Parlando/models/nearby_response.dart' as nearby;
4 import 'package:Parlando/routers/fluro_navigator.dart'; 5 import 'package:Parlando/routers/fluro_navigator.dart';
6 +import 'package:Parlando/util/log_utils.dart';
5 import 'package:Parlando/util/toast_utils.dart'; 7 import 'package:Parlando/util/toast_utils.dart';
6 import 'package:flutter/material.dart'; 8 import 'package:flutter/material.dart';
7 import 'package:Parlando/widgets/my_button.dart'; 9 import 'package:Parlando/widgets/my_button.dart';
...@@ -30,8 +32,7 @@ class AddressSelectPageState extends State<AddressSelectPage> { ...@@ -30,8 +32,7 @@ class AddressSelectPageState extends State<AddressSelectPage> {
30 late StreamSubscription _locationSubscription; 32 late StreamSubscription _locationSubscription;
31 String radius = "1000"; 33 String radius = "1000";
32 String apiKey = "AIzaSyDQZsMULyO-UtiSht4_MFi1uHT4BIqasjw"; 34 String apiKey = "AIzaSyDQZsMULyO-UtiSht4_MFi1uHT4BIqasjw";
33 - nearby.NearbyPlacesResponse nearbyPlacesResponse = 35 + nearby.NearbyPlacesResponse nearbyPlacesResponse = nearby.NearbyPlacesResponse();
34 - nearby.NearbyPlacesResponse();
35 36
36 @override 37 @override
37 void dispose() { 38 void dispose() {
...@@ -65,9 +66,10 @@ class AddressSelectPageState extends State<AddressSelectPage> { ...@@ -65,9 +66,10 @@ class AddressSelectPageState extends State<AddressSelectPage> {
65 } 66 }
66 } 67 }
67 68
68 - _locationSubscription = 69 + _locationSubscription = location.onLocationChanged.listen((LocationData currentLocation) {
69 - location.onLocationChanged.listen((LocationData currentLocation) {
70 _center = LatLng(currentLocation.latitude!, currentLocation.longitude!); 70 _center = LatLng(currentLocation.latitude!, currentLocation.longitude!);
71 + Log.e("currentLocation.latitude ${currentLocation.latitude}");
72 + getNearbyPlaces("");
71 }); 73 });
72 } 74 }
73 75
...@@ -76,8 +78,7 @@ class AddressSelectPageState extends State<AddressSelectPage> { ...@@ -76,8 +78,7 @@ class AddressSelectPageState extends State<AddressSelectPage> {
76 '${'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=${_center.latitude},${_center.longitude}&radius=$radius'}&key=$apiKey&keyword=$keyword'; 78 '${'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=${_center.latitude},${_center.longitude}&radius=$radius'}&key=$apiKey&keyword=$keyword';
77 var url = Uri.parse(uri); 79 var url = Uri.parse(uri);
78 var response = await http.post(url); 80 var response = await http.post(url);
79 - nearbyPlacesResponse = 81 + nearbyPlacesResponse = nearby.NearbyPlacesResponse.fromJson(jsonDecode(response.body));
80 - nearby.NearbyPlacesResponse.fromJson(jsonDecode(response.body));
81 _list = nearbyPlacesResponse.results!; 82 _list = nearbyPlacesResponse.results!;
82 setState(() { 83 setState(() {
83 isLoading = false; 84 isLoading = false;
...@@ -100,40 +101,14 @@ class AddressSelectPageState extends State<AddressSelectPage> { ...@@ -100,40 +101,14 @@ class AddressSelectPageState extends State<AddressSelectPage> {
100 getNearbyPlaces(""); 101 getNearbyPlaces("");
101 } 102 }
102 103
104 + void _goToCurrentCenter() {
105 + mapController.animateCamera(CameraUpdate.newLatLng(_center));
106 + }
107 +
103 @override 108 @override
104 Widget build(BuildContext context) { 109 Widget build(BuildContext context) {
105 - return Scaffold( 110 + var loaderView = const GFLoader().expanded(flex: 11);
106 - resizeToAvoidBottomInset: false, 111 + var realList = ListView.separated(
107 - appBar: SearchBar(
108 - hintText: '搜索地址',
109 - onPressed: (text) async {
110 - isLoading = true;
111 - _controller.animateTo(0.0,
112 - duration: const Duration(milliseconds: 10), curve: Curves.ease);
113 - _index = 0;
114 - // 构造检索参数
115 - getNearbyPlaces(text);
116 - },
117 - ),
118 - body: SafeArea(
119 - child: Column(
120 - children: <Widget>[
121 - Expanded(
122 - flex: 9,
123 - child: GoogleMap(
124 - onMapCreated: _onMapCreated,
125 - initialCameraPosition: CameraPosition(
126 - target: _center,
127 - zoom: 16.0,
128 - ),
129 - markers: Set<Marker>.of(markers.values),
130 - ),
131 - ),
132 - Expanded(
133 - flex: 11,
134 - child: isLoading
135 - ? const GFLoader()
136 - : ListView.separated(
137 controller: _controller, 112 controller: _controller,
138 itemCount: _list.length, 113 itemCount: _list.length,
139 separatorBuilder: (_, index) => const Divider(), 114 separatorBuilder: (_, index) => const Divider(),
...@@ -147,9 +122,39 @@ class AddressSelectPageState extends State<AddressSelectPage> { ...@@ -147,9 +122,39 @@ class AddressSelectPageState extends State<AddressSelectPage> {
147 }, 122 },
148 ); 123 );
149 }, 124 },
150 - ), 125 + ).expanded(flex: 11);
151 - ), 126 + var listHolder = isLoading ? loaderView : realList;
152 - MyButton( 127 +
128 + var searchBar = SearchBar(
129 + hintText: '搜索地址',
130 + onPressed: (text) async {
131 + isLoading = true;
132 + _controller.animateTo(0.0, duration: const Duration(milliseconds: 10), curve: Curves.ease);
133 + _index = 0;
134 + // 构造检索参数
135 + getNearbyPlaces(text);
136 + },
137 + );
138 + var map = GoogleMap(
139 + onMapCreated: _onMapCreated,
140 + initialCameraPosition: CameraPosition(target: _center, zoom: 16.0),
141 + markers: Set<Marker>.of(markers.values),
142 + ).expanded(flex: 9);
143 + return Scaffold(
144 + resizeToAvoidBottomInset: false,
145 + appBar: searchBar,
146 + body: Column(
147 + children: <Widget>[
148 + map,
149 + listHolder,
150 + initButton(),
151 + ],
152 + ).safe(),
153 + );
154 + }
155 +
156 + initButton() {
157 + return MyButton(
153 onPressed: () { 158 onPressed: () {
154 if (_list.isEmpty) { 159 if (_list.isEmpty) {
155 Toast.show('未选择地址!'); 160 Toast.show('未选择地址!');
...@@ -158,10 +163,6 @@ class AddressSelectPageState extends State<AddressSelectPage> { ...@@ -158,10 +163,6 @@ class AddressSelectPageState extends State<AddressSelectPage> {
158 NavigatorUtils.goBackWithParams(context, _list[_index]); 163 NavigatorUtils.goBackWithParams(context, _list[_index]);
159 }, 164 },
160 text: '确认选择地址', 165 text: '确认选择地址',
161 - )
162 - ],
163 - ),
164 - ),
165 ); 166 );
166 } 167 }
167 } 168 }
...@@ -173,6 +174,7 @@ class _AddressItem extends StatelessWidget { ...@@ -173,6 +174,7 @@ class _AddressItem extends StatelessWidget {
173 this.isSelected = false, 174 this.isSelected = false,
174 this.onTap, 175 this.onTap,
175 }) : super(key: key); 176 }) : super(key: key);
177 +
176 final nearby.Results date; 178 final nearby.Results date;
177 final bool isSelected; 179 final bool isSelected;
178 final GestureTapCallback? onTap; 180 final GestureTapCallback? onTap;
...@@ -187,15 +189,8 @@ class _AddressItem extends StatelessWidget { ...@@ -187,15 +189,8 @@ class _AddressItem extends StatelessWidget {
187 height: 50.0, 189 height: 50.0,
188 child: Row( 190 child: Row(
189 children: <Widget>[ 191 children: <Widget>[
190 - Expanded( 192 + Text('${date.name} ${date.vicinity}').expanded(),
191 - child: Text( 193 + Visibility(visible: isSelected, child: const Icon(Icons.done, color: Colors.blue))
192 - '${date.name} ${date.vicinity}',
193 - ),
194 - ),
195 - Visibility(
196 - visible: isSelected,
197 - child: const Icon(Icons.done, color: Colors.blue),
198 - )
199 ], 194 ],
200 ), 195 ),
201 ), 196 ),
......
...@@ -28,7 +28,7 @@ packages: ...@@ -28,7 +28,7 @@ packages:
28 name: archive 28 name: archive
29 url: "https://pub.flutter-io.cn" 29 url: "https://pub.flutter-io.cn"
30 source: hosted 30 source: hosted
31 - version: "3.3.0" 31 + version: "3.1.11"
32 args: 32 args:
33 dependency: transitive 33 dependency: transitive
34 description: 34 description:
...@@ -42,7 +42,7 @@ packages: ...@@ -42,7 +42,7 @@ packages:
42 name: async 42 name: async
43 url: "https://pub.flutter-io.cn" 43 url: "https://pub.flutter-io.cn"
44 source: hosted 44 source: hosted
45 - version: "2.9.0" 45 + version: "2.8.2"
46 boolean_selector: 46 boolean_selector:
47 dependency: transitive 47 dependency: transitive
48 description: 48 description:
...@@ -112,21 +112,21 @@ packages: ...@@ -112,21 +112,21 @@ packages:
112 name: cached_network_image 112 name: cached_network_image
113 url: "https://pub.flutter-io.cn" 113 url: "https://pub.flutter-io.cn"
114 source: hosted 114 source: hosted
115 - version: "3.2.2" 115 + version: "3.2.1"
116 cached_network_image_platform_interface: 116 cached_network_image_platform_interface:
117 dependency: transitive 117 dependency: transitive
118 description: 118 description:
119 name: cached_network_image_platform_interface 119 name: cached_network_image_platform_interface
120 url: "https://pub.flutter-io.cn" 120 url: "https://pub.flutter-io.cn"
121 source: hosted 121 source: hosted
122 - version: "2.0.0" 122 + version: "1.0.0"
123 cached_network_image_web: 123 cached_network_image_web:
124 dependency: transitive 124 dependency: transitive
125 description: 125 description:
126 name: cached_network_image_web 126 name: cached_network_image_web
127 url: "https://pub.flutter-io.cn" 127 url: "https://pub.flutter-io.cn"
128 source: hosted 128 source: hosted
129 - version: "1.0.2" 129 + version: "1.0.1"
130 camera: 130 camera:
131 dependency: "direct main" 131 dependency: "direct main"
132 description: 132 description:
...@@ -168,7 +168,14 @@ packages: ...@@ -168,7 +168,14 @@ packages:
168 name: characters 168 name: characters
169 url: "https://pub.flutter-io.cn" 169 url: "https://pub.flutter-io.cn"
170 source: hosted 170 source: hosted
171 - version: "1.2.1" 171 + version: "1.2.0"
172 + charcode:
173 + dependency: transitive
174 + description:
175 + name: charcode
176 + url: "https://pub.flutter-io.cn"
177 + source: hosted
178 + version: "1.3.1"
172 checked_yaml: 179 checked_yaml:
173 dependency: transitive 180 dependency: transitive
174 description: 181 description:
...@@ -189,7 +196,7 @@ packages: ...@@ -189,7 +196,7 @@ packages:
189 name: clock 196 name: clock
190 url: "https://pub.flutter-io.cn" 197 url: "https://pub.flutter-io.cn"
191 source: hosted 198 source: hosted
192 - version: "1.1.1" 199 + version: "1.1.0"
193 code_builder: 200 code_builder:
194 dependency: transitive 201 dependency: transitive
195 description: 202 description:
...@@ -224,7 +231,7 @@ packages: ...@@ -224,7 +231,7 @@ packages:
224 name: coverage 231 name: coverage
225 url: "https://pub.flutter-io.cn" 232 url: "https://pub.flutter-io.cn"
226 source: hosted 233 source: hosted
227 - version: "1.5.0" 234 + version: "1.2.0"
228 cross_file: 235 cross_file:
229 dependency: transitive 236 dependency: transitive
230 description: 237 description:
...@@ -238,7 +245,7 @@ packages: ...@@ -238,7 +245,7 @@ packages:
238 name: crypto 245 name: crypto
239 url: "https://pub.flutter-io.cn" 246 url: "https://pub.flutter-io.cn"
240 source: hosted 247 source: hosted
241 - version: "3.0.2" 248 + version: "3.0.1"
242 csslib: 249 csslib:
243 dependency: transitive 250 dependency: transitive
244 description: 251 description:
...@@ -336,7 +343,7 @@ packages: ...@@ -336,7 +343,7 @@ packages:
336 name: fake_async 343 name: fake_async
337 url: "https://pub.flutter-io.cn" 344 url: "https://pub.flutter-io.cn"
338 source: hosted 345 source: hosted
339 - version: "1.3.1" 346 + version: "1.3.0"
340 ffi: 347 ffi:
341 dependency: transitive 348 dependency: transitive
342 description: 349 description:
...@@ -463,7 +470,7 @@ packages: ...@@ -463,7 +470,7 @@ packages:
463 name: flutter_native_splash 470 name: flutter_native_splash
464 url: "https://pub.flutter-io.cn" 471 url: "https://pub.flutter-io.cn"
465 source: hosted 472 source: hosted
466 - version: "2.2.10+1" 473 + version: "2.2.9"
467 flutter_plugin_android_lifecycle: 474 flutter_plugin_android_lifecycle:
468 dependency: transitive 475 dependency: transitive
469 description: 476 description:
...@@ -757,6 +764,13 @@ packages: ...@@ -757,6 +764,13 @@ packages:
757 url: "https://pub.flutter-io.cn" 764 url: "https://pub.flutter-io.cn"
758 source: hosted 765 source: hosted
759 version: "4.1.0" 766 version: "4.1.0"
767 + lint:
768 + dependency: transitive
769 + description:
770 + name: lint
771 + url: "https://pub.flutter-io.cn"
772 + source: hosted
773 + version: "1.10.0"
760 lints: 774 lints:
761 dependency: transitive 775 dependency: transitive
762 description: 776 description:
...@@ -805,21 +819,21 @@ packages: ...@@ -805,21 +819,21 @@ packages:
805 name: matcher 819 name: matcher
806 url: "https://pub.flutter-io.cn" 820 url: "https://pub.flutter-io.cn"
807 source: hosted 821 source: hosted
808 - version: "0.12.12" 822 + version: "0.12.11"
809 material_color_utilities: 823 material_color_utilities:
810 dependency: transitive 824 dependency: transitive
811 description: 825 description:
812 name: material_color_utilities 826 name: material_color_utilities
813 url: "https://pub.flutter-io.cn" 827 url: "https://pub.flutter-io.cn"
814 source: hosted 828 source: hosted
815 - version: "0.1.5" 829 + version: "0.1.4"
816 meta: 830 meta:
817 dependency: transitive 831 dependency: transitive
818 description: 832 description:
819 name: meta 833 name: meta
820 url: "https://pub.flutter-io.cn" 834 url: "https://pub.flutter-io.cn"
821 source: hosted 835 source: hosted
822 - version: "1.8.0" 836 + version: "1.7.0"
823 mime: 837 mime:
824 dependency: transitive 838 dependency: transitive
825 description: 839 description:
...@@ -868,7 +882,7 @@ packages: ...@@ -868,7 +882,7 @@ packages:
868 name: path 882 name: path
869 url: "https://pub.flutter-io.cn" 883 url: "https://pub.flutter-io.cn"
870 source: hosted 884 source: hosted
871 - version: "1.8.2" 885 + version: "1.8.1"
872 path_provider: 886 path_provider:
873 dependency: "direct main" 887 dependency: "direct main"
874 description: 888 description:
...@@ -1272,7 +1286,7 @@ packages: ...@@ -1272,7 +1286,7 @@ packages:
1272 name: source_span 1286 name: source_span
1273 url: "https://pub.flutter-io.cn" 1287 url: "https://pub.flutter-io.cn"
1274 source: hosted 1288 source: hosted
1275 - version: "1.9.0" 1289 + version: "1.8.2"
1276 sp_util: 1290 sp_util:
1277 dependency: transitive 1291 dependency: transitive
1278 description: 1292 description:
...@@ -1293,7 +1307,7 @@ packages: ...@@ -1293,7 +1307,7 @@ packages:
1293 name: sqflite 1307 name: sqflite
1294 url: "https://pub.flutter-io.cn" 1308 url: "https://pub.flutter-io.cn"
1295 source: hosted 1309 source: hosted
1296 - version: "2.1.0+1" 1310 + version: "2.0.3+1"
1297 sqflite_common: 1311 sqflite_common:
1298 dependency: transitive 1312 dependency: transitive
1299 description: 1313 description:
...@@ -1335,14 +1349,14 @@ packages: ...@@ -1335,14 +1349,14 @@ packages:
1335 name: string_scanner 1349 name: string_scanner
1336 url: "https://pub.flutter-io.cn" 1350 url: "https://pub.flutter-io.cn"
1337 source: hosted 1351 source: hosted
1338 - version: "1.1.1" 1352 + version: "1.1.0"
1339 sync_http: 1353 sync_http:
1340 dependency: transitive 1354 dependency: transitive
1341 description: 1355 description:
1342 name: sync_http 1356 name: sync_http
1343 url: "https://pub.flutter-io.cn" 1357 url: "https://pub.flutter-io.cn"
1344 source: hosted 1358 source: hosted
1345 - version: "0.3.1" 1359 + version: "0.3.0"
1346 synchronized: 1360 synchronized:
1347 dependency: transitive 1361 dependency: transitive
1348 description: 1362 description:
...@@ -1363,28 +1377,28 @@ packages: ...@@ -1363,28 +1377,28 @@ packages:
1363 name: term_glyph 1377 name: term_glyph
1364 url: "https://pub.flutter-io.cn" 1378 url: "https://pub.flutter-io.cn"
1365 source: hosted 1379 source: hosted
1366 - version: "1.2.1" 1380 + version: "1.2.0"
1367 test: 1381 test:
1368 dependency: "direct dev" 1382 dependency: "direct dev"
1369 description: 1383 description:
1370 name: test 1384 name: test
1371 url: "https://pub.flutter-io.cn" 1385 url: "https://pub.flutter-io.cn"
1372 source: hosted 1386 source: hosted
1373 - version: "1.21.4" 1387 + version: "1.21.1"
1374 test_api: 1388 test_api:
1375 dependency: transitive 1389 dependency: transitive
1376 description: 1390 description:
1377 name: test_api 1391 name: test_api
1378 url: "https://pub.flutter-io.cn" 1392 url: "https://pub.flutter-io.cn"
1379 source: hosted 1393 source: hosted
1380 - version: "0.4.12" 1394 + version: "0.4.9"
1381 test_core: 1395 test_core:
1382 dependency: transitive 1396 dependency: transitive
1383 description: 1397 description:
1384 name: test_core 1398 name: test_core
1385 url: "https://pub.flutter-io.cn" 1399 url: "https://pub.flutter-io.cn"
1386 source: hosted 1400 source: hosted
1387 - version: "0.4.16" 1401 + version: "0.4.13"
1388 timing: 1402 timing:
1389 dependency: transitive 1403 dependency: transitive
1390 description: 1404 description:
...@@ -1405,7 +1419,7 @@ packages: ...@@ -1405,7 +1419,7 @@ packages:
1405 name: typed_data 1419 name: typed_data
1406 url: "https://pub.flutter-io.cn" 1420 url: "https://pub.flutter-io.cn"
1407 source: hosted 1421 source: hosted
1408 - version: "1.3.1" 1422 + version: "1.3.0"
1409 universal_io: 1423 universal_io:
1410 dependency: transitive 1424 dependency: transitive
1411 description: 1425 description:
...@@ -1538,7 +1552,7 @@ packages: ...@@ -1538,7 +1552,7 @@ packages:
1538 name: vm_service 1552 name: vm_service
1539 url: "https://pub.flutter-io.cn" 1553 url: "https://pub.flutter-io.cn"
1540 source: hosted 1554 source: hosted
1541 - version: "9.0.0" 1555 + version: "8.2.2"
1542 wakelock: 1556 wakelock:
1543 dependency: "direct main" 1557 dependency: "direct main"
1544 description: 1558 description:
...@@ -1659,5 +1673,5 @@ packages: ...@@ -1659,5 +1673,5 @@ packages:
1659 source: hosted 1673 source: hosted
1660 version: "3.1.1" 1674 version: "3.1.1"
1661 sdks: 1675 sdks:
1662 - dart: ">=2.18.0 <3.0.0" 1676 + dart: ">=2.17.0 <3.0.0"
1663 - flutter: ">=3.3.0" 1677 + flutter: ">=3.0.0"
......