mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00
59 lines
1.6 KiB
Dart
59 lines
1.6 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:io';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
class PlatformServices extends StatefulWidget {
|
|
@override
|
|
_PlatformServicesState createState() => new _PlatformServicesState();
|
|
}
|
|
|
|
class _PlatformServicesState extends State<PlatformServices> {
|
|
static const PlatformMethodChannel platform = const PlatformMethodChannel('battery');
|
|
String _batteryLevel = 'Unknown battery level.';
|
|
|
|
Future<Null> _getBatteryLevel() async {
|
|
String batteryLevel;
|
|
if (Platform.isIOS) {
|
|
batteryLevel = "iOS is not supported yet.";
|
|
} else {
|
|
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)
|
|
],
|
|
)
|
|
)
|
|
);
|
|
}
|
|
}
|
|
|
|
void main() {
|
|
runApp(new PlatformServices());
|
|
}
|