Skip to content

Instantly share code, notes, and snippets.

@deven98
Last active February 23, 2021 00:45
Show Gist options
  • Save deven98/28500c1b3cbc2df4e955189762dd3056 to your computer and use it in GitHub Desktop.
Save deven98/28500c1b3cbc2df4e955189762dd3056 to your computer and use it in GitHub Desktop.
class GetLocationPage extends StatefulWidget {
@override
_GetLocationPageState createState() => _GetLocationPageState();
}
class _GetLocationPageState extends State<GetLocationPage> {
var location = new Location();
Map<String, double> userLocation;
@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<Map<String, double>> _getLocation() async {
var currentLocation = <String, double>{};
try {
currentLocation = await location.getLocation();
} catch (e) {
currentLocation = null;
}
return currentLocation;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment