Commit 34c63aff authored by Michael Thomsen's avatar Michael Thomsen Committed by GitHub

Update platform-services to new APIs (#8488)

parent 1b52d46b
...@@ -13,7 +13,16 @@ class PlatformServices extends StatefulWidget { ...@@ -13,7 +13,16 @@ class PlatformServices extends StatefulWidget {
} }
class _PlatformServicesState extends State<PlatformServices> { class _PlatformServicesState extends State<PlatformServices> {
Future<dynamic> _locationRequest; static const PlatformMethodChannel platform = const PlatformMethodChannel('geo');
String _location = 'Unknown location.';
Future<Null> _getLocation() async {
List<double> result = await platform.invokeMethod('getLocation', 'network');
setState(() {
_location = 'Latitude ${result[0]}, Longitude ${result[1]}.';
});
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
...@@ -22,45 +31,16 @@ class _PlatformServicesState extends State<PlatformServices> { ...@@ -22,45 +31,16 @@ class _PlatformServicesState extends State<PlatformServices> {
child: new Column( child: new Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly, mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[ children: <Widget>[
new Text('Hello from Flutter!'),
new RaisedButton( new RaisedButton(
child: new Text('Get Location'), child: new Text('Get Location'),
onPressed: _requestLocation, onPressed: _getLocation,
),
new FutureBuilder<dynamic>(
future: _locationRequest,
builder: _buildLocation,
), ),
new Text(_location)
], ],
), ),
), ),
); );
} }
void _requestLocation() {
setState(() {
_locationRequest = const PlatformMethodChannel('geo').invokeMethod(
'getLocation',
'network',
);
});
}
Widget _buildLocation(BuildContext context, AsyncSnapshot<dynamic> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
return new Text('Press button to request location');
case ConnectionState.waiting:
return new Text('Awaiting response...');
default:
try {
final List<double> location = snapshot.requireData;
return new Text('Lat. ${location[0]}, Long. ${location[1]}');
} on PlatformException catch (e) {
return new Text('Request failed: ${e.message}');
}
}
}
} }
void main() { void main() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment