flutter/examples/platform_channel/lib/main.dart
Sarah Zakarias d274888be6 Rename platform_services to platform_channel (#9018)
* Rename platform_services to platform_channel

* rename in README

* renamed tests

* update modules.xml
2017-03-27 11:17:31 +02:00

54 lines
1.5 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 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class PlatformChannel extends StatefulWidget {
@override
_PlatformChannelState createState() => new _PlatformChannelState();
}
class _PlatformChannelState extends State<PlatformChannel> {
static const PlatformMethodChannel platform = const PlatformMethodChannel('battery');
String _batteryLevel = '';
Future<Null> _getBatteryLevel() async {
String batteryLevel;
try {
final int result = await platform.invokeMethod('getBatteryLevel');
batteryLevel = 'Battery level at $result % .';
} on PlatformException catch (e) {
batteryLevel = "Failed to get battery level: '${e.message}'.";
}
setState(() {
_batteryLevel = batteryLevel;
});
}
@override
Widget build(BuildContext context) {
return new Material(
child: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
new RaisedButton(
child: new Text('Get Battery Level'),
onPressed: _getBatteryLevel,
),
new Text(_batteryLevel, key: new Key('Battery level label')),
],
),
),
);
}
}
void main() {
runApp(new PlatformChannel());
}