123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- import 'package:flutter/material.dart';
- import 'package:device_info/device_info.dart';
- import 'dart:io';
- import 'package:battery_plus/battery_plus.dart';
- import 'package:shuqi/public.dart';
- class BatteryView extends StatefulWidget {
- @override
- _BatteryViewState createState() => _BatteryViewState();
- }
- class _BatteryViewState extends State<BatteryView> {
- double batteryLevel = 0;
- @override
- void initState() {
- super.initState();
- getBatteryLevel();
- }
- getBatteryLevel() async {
- DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
- if (Platform.isAndroid) {
- var androidInfo = await deviceInfo.androidInfo;
- if (!androidInfo.isPhysicalDevice) {
- return;
- }
- }
- if (Platform.isIOS) {
- var iosInfo = await deviceInfo.iosInfo;
- if (!iosInfo.isPhysicalDevice) {
- return;
- }
- }
- var battery = Battery();
- var level = await battery.batteryLevel;
- battery.onBatteryStateChanged.listen((BatteryState state) {
- // Do something with new state
- });
- setState(() {
- this.batteryLevel = level / 100.0;
- });
- }
- @override
- Widget build(BuildContext context) {
- return Container(
- width: 27,
- height: 12,
- child: Stack(
- children: <Widget>[
- Image.asset('assets/img/reader_battery.png'),
- Container(
- margin: EdgeInsets.fromLTRB(2, 2, 2, 2),
- width: 20 * batteryLevel,
- color: SQColor.golden,
- )
- ],
- ),
- );
- }
- }
|