0

i want to start a function after 5 seconds when i press the button just one time , for this i use Timer Class, but when i put it on initState, it start whitout clicking on button,

This is my code :

https://www.dartpad.dev/b6409e10de32b280b8938aa75364fa7b

Timer time;

  @override
  void initState() {        
    super.initState();

    _time = Timer.periodic(Duration(seconds: 5), (Timer t) {
       print('yaaay');      
       t.cancel();
    });    

  }

  void start() {    
    _time ;   
  }


  @override
  Widget build(BuildContext context) {

    return Scaffold(

      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,

        children : [

           ElevatedButton(
             child:Text('press me'),             

             onPressed:(){
               start();
             }
          ),
        ]);    
     );

Thank you !

swing13
  • 33
  • 1
  • 12

3 Answers3

0

Trigger it in the onPressed property of your button. For instance:

 onPressed:(){

 Future.delayed(Duration(seconds:5)).whenComplete((){
  start();
  }); 

 }
Iván Yoed
  • 3,878
  • 31
  • 44
Biruk Telelew
  • 1,161
  • 7
  • 13
  • it didn't work, because it trigger every time i press the button, but i want it to be just one time only. – swing13 Mar 16 '21 at 12:36
  • use a variable to check if the button is pressed for the first time or not – Biruk Telelew Mar 16 '21 at 12:47
  • declare bool pressed=false; and then onpressed set pressed=true; using setstate then surround the Future.delayed code by this condition if(pressed==false){//Future.delayed code....}else{start();} – Biruk Telelew Mar 16 '21 at 13:23
0

I would recommend you not putting it in the initState method, because that means it will always trigger when this widgets initiates its state, that is

when this object is inserted into the tree

From: https://api.flutter.dev/flutter/widgets/State/initState.html

And that's why this functions is being triggered "automatically". Instead just place your function where you can manually trigger it. For instance, inside the onPress property of your button, since you said that's what you're trying to achieve ;). @biruk IS's solution seems appropriate.

Iván Yoed
  • 3,878
  • 31
  • 44
0

It's because when you're using the following code:

_time = Timer.periodic(Duration(seconds: 5), (Timer t) {
  print('yaaay');      
  t.cancel();
});    

it means you're creating a Timer and run it.

So, if you want to start the timer when clicking the button, you need to modify your code like the following:

void _startTimer() {
    _time = Timer.periodic(Duration(seconds: 5), (Timer t) {
      print('yaaay');      
      t.cancel();
    });  
}

void _stopTimer() {
   if(_time != null) _time.cancel();
}

then use it on your button:

ElevatedButton(
    child:Text('press me'),             

      onPressed:(){
        _startTimer();
      }
),
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
  • it didn't work, because it trigger every time i press the button, but i want it to be just one time only. (like the other solution) – swing13 Mar 16 '21 at 12:36