Flutter: периодический таймер
Во Flutter за таймеры отвечает класс Timer, а за периодический в частности Timer.periodic
Для примера использования, реализуем отчет переменной от 30 до 0:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import 'dart:async'; Timer?_timer; // ? говорит о том, что переменная в начале пути может быть null int timercount=30; bool timout_passed=false; //вышел таймаут разблокировки кнопки ... _timer = Timer.periodic(Duration(seconds: 1), (Timer _) { if (timercount>0) { timercount = timercount - 1; }; if (timercount==0) { timout_passed=true; _timer?.cancel(); // закончили отчёт } }); |
