battery 0.1.1
battery: ^0.1.1 copied to clipboard
Flutter plugin for accessing information about the battery state (full, charging, discharging) on Android and iOS.
example/lib/main.dart
// Copyright 2017 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:battery/battery.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Battery _battery = new Battery();
BatteryState _batteryState;
StreamSubscription<BatteryState> _batteryStateSubscription;
@override
void initState() {
super.initState();
_batteryStateSubscription =
_battery.onBatteryStateChanged.listen((BatteryState state) {
setState(() {
_batteryState = state;
});
});
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: const Text('Plugin example app'),
),
body: new Center(
child: new Text('$_batteryState'),
),
floatingActionButton: new FloatingActionButton(
child: const Icon(Icons.battery_unknown),
onPressed: () async {
showDialog<Null>(
context: context,
child: new AlertDialog(
content: new Text('Battery: ${await _battery.batteryLevel}%'),
actions: <Widget>[
new FlatButton(
child: const Text('OK'),
onPressed: () {
Navigator.pop(context);
},
)
],
),
);
},
),
);
}
@override
void dispose() {
super.dispose();
if (_batteryStateSubscription != null) {
_batteryStateSubscription.cancel();
}
}
}