Last active
February 23, 2021 00:45
-
-
Save deven98/28500c1b3cbc2df4e955189762dd3056 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
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