Update platform-services to new APIs (#8488)

This commit is contained in:
Michael Thomsen 2017-03-01 17:36:07 +01:00 committed by GitHub
parent 1b52d46b0e
commit 34c63aff84

View file

@ -13,7 +13,16 @@ class PlatformServices extends StatefulWidget {
}
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
Widget build(BuildContext context) {
@ -22,45 +31,16 @@ class _PlatformServicesState extends State<PlatformServices> {
child: new Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
new Text('Hello from Flutter!'),
new RaisedButton(
child: new Text('Get Location'),
onPressed: _requestLocation,
),
new FutureBuilder<dynamic>(
future: _locationRequest,
builder: _buildLocation,
onPressed: _getLocation,
),
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() {