面对 flutter 项目中计时器功能的错误

facing error in timer functionality in flutter project

*flutter 项目中的定时器功能面临错误 *请任何人帮助我摆脱这个计时器功能错误

    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 秒后没有重新启动

#谁能帮忙?我肯定错过了什么。

检查这个启动定时器功能,它可能会帮助你

late Timer _timer;
  int seconds = 00;
  int minutes = 00;
  int hours = 00;

void startTimer() {
    const oneSec = const Duration(seconds: 1);
    _timer = new Timer.periodic(
      oneSec,
      (Timer timer) => setState(
        () {
          seconds++;
          if (seconds > 59) {
            minutes += 1;
            seconds = 0;
            if (minutes > 59) {
              hours += 1;
              minutes = 0;
            }
          }
        },
      ),
    );
  }

问题出在这部分:

else{localMinutes++;localSecounds==0;}

您正在检查 localSecounds 是否等于 0,而不是实际给它值 0(与分钟相同)。尝试:

    if (localSecounds>59) {
        if (localMinutes>59) {
          localHours++;
          localMinutes=0;
        }else{
           localMinutes++;
           localSecounds=0;
        }
    }

或者用户可以使用这个简单但功能强大的软件包:stop_watch_timer 来自 pub.dev