-
Notifications
You must be signed in to change notification settings - Fork 8
/
finder.dart
87 lines (72 loc) · 2.11 KB
/
finder.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
// ignore_for_file: library_private_types_in_public_api
import 'package:flutter/material.dart';
import 'package:universe/universe.dart';
class LocationFinderMap extends StatefulWidget {
const LocationFinderMap({super.key});
@override
State<LocationFinderMap> createState() => _LocationFinderMapState();
}
class _LocationFinderMapState extends State<LocationFinderMap> {
final _mapKey = UniqueKey();
final _mapController = MapController();
final _locationName = 'Highbury Square';
bool _isSearching = false;
@override
void dispose() {
_mapController.dispose();
super.dispose();
}
void _startSearching() {
setState(() {
_isSearching = true;
});
}
void _stopSearching() {
setState(() {
_isSearching = false;
});
}
Future _findLocation(context) async {
_startSearching();
LatLng? location = await _mapController.findLocation(_locationName);
// this is only to show a nice transition between two loading text
// do not use in production
await Future.delayed(const Duration(milliseconds: 500));
_stopSearching();
final message = location != null
? 'Latlng: ${location.toSimpleString()}'
: 'Location not found!';
ScaffoldMessenger.of(context).hideCurrentSnackBar();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(message)),
);
}
@override
Widget build(BuildContext context) {
return Stack(
children: [
U.OpenStreetMap(
key: _mapKey,
controller: _mapController,
// set map center by location name (not latlng)
center: 'Emirates Stadium',
zoom: 16,
),
Positioned(
left: 20,
bottom: 90,
child: ElevatedButton(
onPressed: () => _findLocation(context),
style: ElevatedButton.styleFrom(
foregroundColor: Colors.white,
backgroundColor: Colors.lightBlue,
),
child: Text(
_isSearching ? 'Searching...' : 'Find latlng for $_locationName',
),
),
),
],
);
}
}