【发布时间】:2021-03-23 23:59:13
【问题描述】:
谁能帮助我我正在尝试为电池百分比制作一个圆形百分比指标,但我收到了这个错误,这是我的大学项目工作,第一次写这篇文章我以前从未这样做过,我在编码方面也是新手。
lib/pages/device_dashboard.dart:139:36:错误:参数类型“int”不能分配给参数类型“double”。 百分比:_batteryLevel, ^
这是我的代码
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:percent_indicator/percent_indicator.dart';
import 'package:battery/battery.dart';
import 'package:flutter/material.dart';
class DeviceDashboard extends StatefulWidget {
@override
_DeviceDashboardState createState() => _DeviceDashboardState();
}
class _DeviceDashboardState extends State<DeviceDashboard> {
Battery _battery = Battery();
BatteryState _batteryState;
int _batteryLevel;
StreamSubscription<BatteryState> _batteryStateSubscription;
@override
void initState() {
super.initState();
_batteryStateSubscription =
_battery.onBatteryStateChanged.listen((BatteryState state) {
setState(() {
_batteryState = state;
});
});
}
Future<void> _getLevel() async {
final batteryLevel = await _battery.batteryLevel;
setState(() {
_batteryLevel = batteryLevel;
});
}
@override
void dispose() {
// TODO: implement dispose
super.dispose();
if (_batteryStateSubscription != null) {
_batteryStateSubscription.cancel();
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Device Dashboard'),
),
body: SingleChildScrollView(
child: Column(
children: <Widget>[
SizedBox(
height: 50,
),
Text(
'$_batteryState',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: Colors.grey[400],
),
),
Column(
mainAxisSize: MainAxisSize.min,
children: [
CircularPercentIndicator(
radius: 100.0,
lineWidth: 7.0,
center: MaterialButton(
onPressed: _getLevel,
textColor: Colors.white,
child: Text(
'$_batteryLevel %',
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 20),
),
padding: EdgeInsets.all(20),
shape: CircleBorder(),
),
progressColor: Colors.green,
percent: 0.5, // after putting _batteryLevel here i m getting that error
),
Text(
'Battery',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
],
),
],
),
),
);
}
}
【问题讨论】:
标签: flutter flutter-layout flutter-dependencies flutter-web flutter-animation