这是用于秒表功能的。 xCountUpDuration 是秒表的默认时间,我在 2 个地方设置了它,一个在 listview (代码 1) 之外,值设置为 0,另一个在 listview (代码 3)内部 并且该值设置为 10 分钟。问题是当我在 (Code 3) 的列表视图中调用 (Code 2) 小部件时,该值设置为 0 而不是 10 分钟。我怎样才能让它使用Code 3中设置的xCountUpDuration值。
代码1
Duration xCountUpDuration = Duration();
Timer? xCountUpTime;
void xCountUpActionStart() {
xCountUpActionEnd();
xCountUpTime = Timer.periodic(Duration(seconds: 1), (_) => xCountUpActionAddTime());
}
void xCountUpActionEnd() {
setState(() => xCountUpTime?.cancel());
}
void xCountUpActionAddTime() {
final xAddSeconds = 1;
setState(() {
final seconds = xCountUpDuration.inSeconds + xAddSeconds;
xCountUpDuration = Duration(seconds: seconds);
});
}
代码2
Widget xCountUpTimerWidget() {
String xDigitConverter(int n) => n.toString().padLeft(2, '0');
final xHours = xDigitConverter(xCountUpDuration.inHours);
final xMinutes = xDigitConverter(xCountUpDuration.inMinutes.remainder(60));
final xSeconds = xDigitConverter(xCountUpDuration.inSeconds.remainder(60));
return Text(
'$xHours:$xMinutes:$xSeconds',
style: TextStyle(
fontSize: 60,
fontWeight: FontWeight.w700,
),
);
}
代码3
return ListView.builder(
itemBuilder: (context, index) {
Duration xCountUpDuration = Duration(hours: 00, minutes: 10, seconds: 00);
return Column(
children: [
xCountUpTimerWidget(),
]
)
}
让您的 Widget 了解变量的值:
return ListView.builder(
itemBuilder: (context, index) {
Duration xInnerCountUpDuration = Duration(hours: 00, minutes: 10, seconds: 00);
return Column(
children: [
xCountUpTimerWidget(xInnerCountUpDuration ),
]
)}
代码2:
Widget xCountUpTimerWidget(Duration param) {
String xDigitConverter(int n) => n.toString().padLeft(2, '0');
final xHours = xDigitConverter(param.inHours);
final xMinutes = xDigitConverter(param.inMinutes.remainder(60));
final xSeconds = xDigitConverter(param.inSeconds.remainder(60));
return Text(
'$xHours:$xMinutes:$xSeconds',
style: TextStyle(
fontSize: 60,
fontWeight: FontWeight.w700,
),
);