Created
January 6, 2019 15:13
-
-
Save deven98/3d8a1e17025b7db6e35684004f6f3490 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:geolocator/geolocator.dart'; | |
import 'dart:async'; | |
class GeoListenPage extends StatefulWidget { | |
@override | |
_GeoListenPageState createState() => _GeoListenPageState(); | |
} | |
class _GeoListenPageState extends State<GeoListenPage> { | |
Geolocator geolocator = Geolocator(); | |
Position userLocation; | |
@override | |
void initState() { | |
// TODO: implement initState | |
super.initState(); | |
_getLocation().then((position) { | |
userLocation = position; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
userLocation == null | |
? CircularProgressIndicator() | |
: Text("Location:" + | |
userLocation.latitude.toString() + | |
" " + | |
userLocation.longitude.toString()), | |
Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: RaisedButton( | |
onPressed: () { | |
_getLocation().then((value) { | |
setState(() { | |
userLocation = value; | |
}); | |
}); | |
}, | |
color: Colors.blue, | |
child: Text( | |
"Get Location", | |
style: TextStyle(color: Colors.white), | |
), | |
), | |
), | |
], | |
), | |
), | |
); | |
} | |
Future<Position> _getLocation() async { | |
var currentLocation; | |
try { | |
currentLocation = await geolocator.getCurrentPosition( | |
desiredAccuracy: LocationAccuracy.best); | |
} catch (e) { | |
currentLocation = null; | |
} | |
return currentLocation; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment