skip to Main Content

I am new to flutter and I am trying to start a timer when I open a new page and I would like to see the timer values in the new page I am trying for 4 days but I could not handle it

I am expecting that when I click a button and it navigates to another new screen and while it is opening the new page I would like to start the timer and I would like to see the counters values in the new page

2

Answers


  1. this sample widget will increment the counterValue variable every second and show the new state in the Text widget.

    class Bird extends StatefulWidget {
      const Bird({
        super.key,
      });
    
      @override
      State<Bird> createState() => _BirdState();
    }
    
    class _BirdState extends State<Bird> {
      int counterValue = 0;
      late Timer timer;
      
       @override
       void initState() {
       super.initState();
       timer = Timer(Duration(seconds: 1), (_) {
         setState(() {
           counterValue++;
          });
       });
      } 
    
      @override
      Widget build(BuildContext context) {
        return Text(counterValue);
      }
    }
    
    Login or Signup to reply.
  2. You have to initialise your timer value in init method of your page, so whenever page will initialise timer will start from init value. It would be nice if you can add code for better help.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search