@@ -13,7 +13,16 @@ class PlatformServices extends StatefulWidget {
13
13
}
14
14
15
15
class _PlatformServicesState extends State <PlatformServices > {
16
- Future <dynamic > _locationRequest;
16
+ static const PlatformMethodChannel platform = const PlatformMethodChannel ('geo' );
17
+ String _location = 'Unknown location.' ;
18
+
19
+ Future <Null > _getLocation () async {
20
+ List <double > result = await platform.invokeMethod ('getLocation' , 'network' );
21
+
22
+ setState (() {
23
+ _location = 'Latitude ${result [0 ]}, Longitude ${result [1 ]}.' ;
24
+ });
25
+ }
17
26
18
27
@override
19
28
Widget build (BuildContext context) {
@@ -22,45 +31,16 @@ class _PlatformServicesState extends State<PlatformServices> {
22
31
child: new Column (
23
32
mainAxisAlignment: MainAxisAlignment .spaceEvenly,
24
33
children: < Widget > [
25
- new Text ('Hello from Flutter!' ),
26
34
new RaisedButton (
27
35
child: new Text ('Get Location' ),
28
- onPressed: _requestLocation,
29
- ),
30
- new FutureBuilder <dynamic >(
31
- future: _locationRequest,
32
- builder: _buildLocation,
36
+ onPressed: _getLocation,
33
37
),
38
+ new Text (_location)
34
39
],
35
40
),
36
41
),
37
42
);
38
43
}
39
-
40
- void _requestLocation () {
41
- setState (() {
42
- _locationRequest = const PlatformMethodChannel ('geo' ).invokeMethod (
43
- 'getLocation' ,
44
- 'network' ,
45
- );
46
- });
47
- }
48
-
49
- Widget _buildLocation (BuildContext context, AsyncSnapshot <dynamic > snapshot) {
50
- switch (snapshot.connectionState) {
51
- case ConnectionState .none:
52
- return new Text ('Press button to request location' );
53
- case ConnectionState .waiting:
54
- return new Text ('Awaiting response...' );
55
- default :
56
- try {
57
- final List <double > location = snapshot.requireData;
58
- return new Text ('Lat. ${location [0 ]}, Long. ${location [1 ]}' );
59
- } on PlatformException catch (e) {
60
- return new Text ('Request failed: ${e .message }' );
61
- }
62
- }
63
- }
64
44
}
65
45
66
46
void main () {
0 commit comments