扑面项目中的计时器功能面临错误

发布于 2025-01-30 01:51:17 字数 5415 浏览 4 评论 0原文

*扑面项目中的计时器功能面临错误 *请任何人都可以帮助我摆脱此计时器功能错误

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遥远的她 2025-02-06 01:51:17

问题在于这一部分:

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

您正在检查局部记录是否等于0,实际上不是给出值0(与分钟相同的东西)。尝试:

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

The problem lies in this part:

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

You are checking if localSecounds is equal to 0, not actually giving it the value 0(Same thing with minutes). Try:

    if (localSecounds>59) {
        if (localMinutes>59) {
          localHours++;
          localMinutes=0;
        }else{
           localMinutes++;
           localSecounds=0;
        }
    }
︶ ̄淡然 2025-02-06 01:51:17

检查此开始计时器功能,它可能会帮助您

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;
            }
          }
        },
      ),
    );
  }

Check this start Timer Function, it may help you out

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;
            }
          }
        },
      ),
    );
  }
天涯离梦残月幽梦 2025-02-06 01:51:17

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

Or User can use this simple but powerful package : stop_watch_timer from pub.dev

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文