扑面项目中的计时器功能面临错误
*扑面项目中的计时器功能面临错误 *请任何人都可以帮助我摆脱此计时器功能错误
import 'dart:async';
import 'package:flutter/material.dart';
class StopwatchHome extends StatefulWidget {
StopwatchHome({Key? key}) : super(key: key);
@override
State<StopwatchHome> createState() => _StopwatchHomeState();
}
class _StopwatchHomeState extends State<StopwatchHome> {
int secounds=0, minutes=0, hours=0;
String digitsecounds="00", digitminutes ="00", digithours="00";
Timer? timer;
bool started= false;
List laps=[];
void stop(){
timer!.cancel();
setState(() {
started=false;
});
}
void reset(){
timer!.cancel();
setState(() {
secounds=0;
minutes=0;
hours=0;
digitsecounds="00";
digitminutes="00";
digithours="00";
started=false;
});
}
void addlaps(){
String lap="$digithours:$digitminutes:$digitsecounds";
setState(() {
laps.add(lap);
});
}
void start(){
started=true;
timer=Timer.periodic(Duration(seconds: 1), (timer) {
int localSecounds=secounds+1;
int localMinutes= minutes=0;
int localHours= hours;
if (localSecounds>59) {
if (localMinutes>59) {
localHours++;
localMinutes==0;
}else{localMinutes++;localSecounds==0;}
}
setState(() {
secounds= localSecounds;
minutes= localMinutes;
hours= localHours;
digitsecounds=(secounds>=10)?"$secounds":"0$secounds";
digithours=(hours>=10)?"$hours":"0$hours";
digitminutes=(minutes>=10)?"$minutes":"0$minutes";
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor:
// const Color(0xff213a20),
Color.fromARGB(255, 138, 11, 87),
body: SafeArea(
child: Padding(
padding: EdgeInsets.all(18),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Center(
child: Text("Stopwatch",
style: TextStyle(color: Colors.white,
fontWeight: FontWeight.bold,fontSize: 40,
fontStyle: FontStyle.italic),),
),
SizedBox(height: 20.0),
Center(child: Text("$digithours:$digitminutes:$digitsecounds",style: TextStyle(color: Colors.white,fontSize: 73.0,fontWeight: FontWeight.bold),),),
Container(
height: 300.0,
decoration: BoxDecoration(color: Color.fromARGB(255, 162, 109, 145),
borderRadius: BorderRadius.circular(10)),
child: ListView.builder(
itemCount: laps.length,
itemBuilder: (context,index){
return Padding(
padding: const EdgeInsets.all(18.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("Lap Number = ${index+1}",style: TextStyle(color: Colors.white,fontSize: 16 ),),
Text("${laps[index]}",style:TextStyle(color: Colors.white,fontSize: 16 ) )
],
),
);
}
),
),
SizedBox(height: 23),
Row(mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(child: RawMaterialButton(onPressed: (){
(!started) ? start():stop();
},
fillColor: Color.fromARGB(255, 73, 119, 4),
shape:StadiumBorder(side: BorderSide(color: Colors.blue)),
child: Text((! started)? "START":"PAUSE",style: TextStyle(color: Colors.white,
fontSize: 18,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.italic
),
),
)
),
SizedBox(width: 8),
IconButton(
onPressed: (){addlaps();}, icon: Icon(Icons.timelapse_rounded,color: Colors.red,)),
Expanded(child: RawMaterialButton(onPressed: (){reset();},
fillColor: Color.fromARGB(255, 132, 9, 23),
shape:StadiumBorder(side: BorderSide(color: Color.fromARGB(255, 40, 129, 11))),
child: Text("RESET",style: TextStyle(color: Colors.white,
fontSize: 18,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.italic
),
),
)
)
],
)
],
),
),
),
);
}
}
,请任何人都可以帮助我摆脱此计时器功能错误
#DigitSecound完成了60个Secounds
#任何人可以提供帮助?我肯定缺少一些东西。
*facing error in timer functionality in flutter project
*please any one can help me to get rid of this timer functionality error
import 'dart:async';
import 'package:flutter/material.dart';
class StopwatchHome extends StatefulWidget {
StopwatchHome({Key? key}) : super(key: key);
@override
State<StopwatchHome> createState() => _StopwatchHomeState();
}
class _StopwatchHomeState extends State<StopwatchHome> {
int secounds=0, minutes=0, hours=0;
String digitsecounds="00", digitminutes ="00", digithours="00";
Timer? timer;
bool started= false;
List laps=[];
void stop(){
timer!.cancel();
setState(() {
started=false;
});
}
void reset(){
timer!.cancel();
setState(() {
secounds=0;
minutes=0;
hours=0;
digitsecounds="00";
digitminutes="00";
digithours="00";
started=false;
});
}
void addlaps(){
String lap="$digithours:$digitminutes:$digitsecounds";
setState(() {
laps.add(lap);
});
}
void start(){
started=true;
timer=Timer.periodic(Duration(seconds: 1), (timer) {
int localSecounds=secounds+1;
int localMinutes= minutes=0;
int localHours= hours;
if (localSecounds>59) {
if (localMinutes>59) {
localHours++;
localMinutes==0;
}else{localMinutes++;localSecounds==0;}
}
setState(() {
secounds= localSecounds;
minutes= localMinutes;
hours= localHours;
digitsecounds=(secounds>=10)?"$secounds":"0$secounds";
digithours=(hours>=10)?"$hours":"0$hours";
digitminutes=(minutes>=10)?"$minutes":"0$minutes";
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor:
// const Color(0xff213a20),
Color.fromARGB(255, 138, 11, 87),
body: SafeArea(
child: Padding(
padding: EdgeInsets.all(18),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Center(
child: Text("Stopwatch",
style: TextStyle(color: Colors.white,
fontWeight: FontWeight.bold,fontSize: 40,
fontStyle: FontStyle.italic),),
),
SizedBox(height: 20.0),
Center(child: Text("$digithours:$digitminutes:$digitsecounds",style: TextStyle(color: Colors.white,fontSize: 73.0,fontWeight: FontWeight.bold),),),
Container(
height: 300.0,
decoration: BoxDecoration(color: Color.fromARGB(255, 162, 109, 145),
borderRadius: BorderRadius.circular(10)),
child: ListView.builder(
itemCount: laps.length,
itemBuilder: (context,index){
return Padding(
padding: const EdgeInsets.all(18.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("Lap Number = ${index+1}",style: TextStyle(color: Colors.white,fontSize: 16 ),),
Text("${laps[index]}",style:TextStyle(color: Colors.white,fontSize: 16 ) )
],
),
);
}
),
),
SizedBox(height: 23),
Row(mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(child: RawMaterialButton(onPressed: (){
(!started) ? start():stop();
},
fillColor: Color.fromARGB(255, 73, 119, 4),
shape:StadiumBorder(side: BorderSide(color: Colors.blue)),
child: Text((! started)? "START":"PAUSE",style: TextStyle(color: Colors.white,
fontSize: 18,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.italic
),
),
)
),
SizedBox(width: 8),
IconButton(
onPressed: (){addlaps();}, icon: Icon(Icons.timelapse_rounded,color: Colors.red,)),
Expanded(child: RawMaterialButton(onPressed: (){reset();},
fillColor: Color.fromARGB(255, 132, 9, 23),
shape:StadiumBorder(side: BorderSide(color: Color.fromARGB(255, 40, 129, 11))),
child: Text("RESET",style: TextStyle(color: Colors.white,
fontSize: 18,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.italic
),
),
)
)
],
)
],
),
),
),
);
}
}
please any one can help me to get rid of this timer functionality error
#the digitsecound is not restarting after completing sixty secounds
#Anyone who can help? I'm definitely missing something.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题在于这一部分:
您正在检查局部记录是否等于0,实际上不是给出值0(与分钟相同的东西)。尝试:
The problem lies in this part:
You are checking if localSecounds is equal to 0, not actually giving it the value 0(Same thing with minutes). Try:
检查此开始计时器功能,它可能会帮助您
Check this start Timer Function, it may help you out
或用户可以使用此简单但功能强大的软件包: stop_watch_timer 来自pub.dev
Or User can use this simple but powerful package : stop_watch_timer from pub.dev