Skip to content

Instantly share code, notes, and snippets.

@deven98
Created January 6, 2019 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deven98/3d8a1e17025b7db6e35684004f6f3490 to your computer and use it in GitHub Desktop.
Save deven98/3d8a1e17025b7db6e35684004f6f3490 to your computer and use it in GitHub Desktop.
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