mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00

Cleans up a few style nits in hello_services and adds support for automatic JSON encoding and decoding to the HostMessages interface.
70 lines
1.9 KiB
Dart
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);
|
|
}
|