我在我的 Flutter 应用程序上使用计步器插件 ( https://pub.dev/packages/pedometer#-installing-tab- )。有人可以回答我如何重置步数值吗?已经尝试使用计时器将值重置为 0,但该值仍来自最后一步计数。
这是我用于测试计步器的代码:
请帮助我,我是新手。谢谢
@override
void initState() {
super.initState();
setUpPedometer();
}
void setUpPedometer() {
Pedometer pedometer = new Pedometer();
_subscription = pedometer.stepCountStream.listen(_onData,
onError: _onError, onDone: _onDone, cancelOnError: true);
}
void _onData(stepCountValue) async {
setState(() {
_stepCountValue = "$stepCountValue";
_step = stepCountValue;
});
var dist = _step;
double y = (dist + .0);
setState(() {
_numerox =
y;
});
var long3 = (_numerox);
long3 = num.parse(y.toStringAsFixed(2));
var long4 = (long3 / 10000);
int decimals = 1;
int fac = pow(10, decimals);
double d = long4;
d = (d * fac).round() / fac;
print("d: $d");
getDistanceRun(_numerox);
setState(() {
_convert = d;
print(_convert);
});
}
void reset() {
setState(() {
int stepCountValue = 0;
stepCountValue = 0;
_stepCountValue = "$stepCountValue";
});
}
void _onDone() {}
void _onError(error) {
print("Flutter Pedometer Error: $error");
}
//function to determine the distance run in kilometers using number of steps
void getDistanceRun(double _numerox) {
var distance = ((_numerox * 78) / 100000);
distance = num.parse(distance.toStringAsFixed(2)); //dos decimales
var distancekmx = distance * 1000000;//34;
distancekmx = num.parse(distancekmx.toStringAsFixed(2));
//print(distance.runtimeType);
setState(() {
_km = "$distance";
//print(_km);
});
setState(() {
_kmx = num.parse(distancekmx.toStringAsFixed(2));
});
}
//function to determine the calories burned in kilometers using number of steps
void getBurnedRun() {
setState(() {
var calories = _kmx; //dos decimales
_calories = calories==null?"0":"$calories";
//print(_calories);
});
}