Home > front end >  How to define a function in a text widget?
How to define a function in a text widget?

Time:09-11

  void valuerandomer() {
    Future.delayed(Duration(milliseconds: 500), () {
      int count = 0;
      int max = 1000;
      int min = 1;
      Random rnd = new Random();
      while (count != -1) {
        count  ;
        value  = rnd.nextInt(6)   (-5);
      }
      if (value > (max - 1)) {
        value = 999;
      } else if (value < 0) {
        value = 0;
      }
      print(value);
    });
  }

I want the function to print every 500 miliseconds in the text widget so the value parameter starts with the value of 75 and changes every 500 milliseconds with this function. How do I do that? How do I declare this function in the text widget like Text('$valuerandomer')? cuz its just dont work. I tried just to type there $value but still doesnt work

CodePudding user response:

For every x time, try using Timer.periodic

  Timer? _timer;
  String text = "initText";
  @override
  void dispose() {
    _timer?.cancel();
    super.dispose();
  }

  void valuerandomer() {
    _timer = Timer.periodic(
      Duration(milliseconds: 500),
      (t) {
        //perform your work
        text = "newText ";
        setState(() {});
      },
    );
  }

CodePudding user response:

Use funtion

Timer.periodic(Duration(/..),(timer){
 //Put your logic
 setState((){});
})
  • Related