我会尽量保持简短。 因此,我有一个计时器,该计时器通过Provider程序包获取其int值。
Consumer<TimeData>(
builder: (context, timeData, child) {
return Timer(timeData.durations[playerNumber]);
},
我的意图是,当按下按钮时,计时器会自动更新。因此,我使用playerNumber变量。
这是Button的onPressed功能
onPressed: () {
setState(() {
if (Provider.of<PlayerData>(context, listen: false)
.players[playerNumber + 1] !=
null) {
playerNumber++;
resetController();
//createController();
}
});
}
但是,即使按下Button并增加playerNumber(在setState方法中),Timer也不会更新。有谁知道如何重置计时器?
这是Timer类:
import 'package:countdown_flutter/countdown_flutter.dart';
import 'package:flutter/material.dart';
class Timer extends StatelessWidget {
final int duration;
Timer(this.duration);
@override
Widget build(BuildContext context) {
return Center(
child: Countdown(
duration: Duration(seconds: duration),
builder: (BuildContext ctx, Duration remaining) {
return Text(
'${remaining.inMinutes}:${remaining.inSeconds - (remaining.inMinutes * 60)}',
style: TextStyle(
color: Colors.white,
fontSize: 99.9,
),
);
},
),
);
}
}
这是我的TimeData类,数据来自哪里
import 'package:flutter/foundation.dart';
class TimeData extends ChangeNotifier {
List<int> durations = [];
void addTime(int time) {
final duration = time;
durations.add(duration);
notifyListeners();
}
void deleteList() {
durations.clear();
notifyListeners();
}
}