flutter/examples/hello_services/lib/main.dart
Adam Barth 0b6df634af Polish HostMessages (#4515)
Cleans up a few style nits in hello_services and adds support for automatic
JSON encoding and decoding to the HostMessages interface.
2016-06-10 15:04:01 -07:00

70 lines
1.9 KiB
Dart

// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
final Random random = new Random();
Future<dynamic> handleGetRandom(Map<String, dynamic> message) async {
final double min = message['min'].toDouble();
final double max = message['max'].toDouble();
return <String, double>{
'value': (random.nextDouble() * (max - min)) + min
};
}
class HelloServices extends StatefulWidget {
@override
_HelloServicesState createState() => new _HelloServicesState();
}
class _HelloServicesState extends State<HelloServices> {
double _latitude;
double _longitude;
@override
Widget build(BuildContext context) {
return new Material(
child: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
new Text('Hello from Flutter!'),
new RaisedButton(
child: new Text('Get Location'),
onPressed: _getLocation
),
new Text('Latitude: $_latitude, Longitude: $_longitude'),
]
)
)
);
}
Future<Null> _getLocation() async {
final Map<String, String> message = <String, String>{'provider': 'network'};
final Map<String, dynamic> reply = await HostMessages.sendJSON('getLocation', message);
// If the widget was removed from the tree while the message was in flight,
// we want to discard the reply rather than calling setState to update our
// non-existant appearance.
if (!mounted)
return;
setState(() {
_latitude = reply['latitude'].toDouble();
_longitude = reply['longitude'].toDouble();
});
}
}
void main() {
runApp(new HelloServices());
HostMessages.addJSONMessageHandler('getRandom', handleGetRandom);
}