useInterval function
Calls onTick of the latest build once every period, from the first
build on, on the clock of the app the widget is in, until the element is
disposed. The ticks are Steps of the app's clock: they fall in the
frames at the multiples of period since the app started, so the first
comes within one period of the build, not after a whole one, and all
that step by the same period tick in the same frame. A tick is called in
a frame, where a setState is as usual.
Muted while a TickerMode above is off, as useTicker is: no tick and
no frame asked for, and none made up for when it is on again. A changed
period restarts it at the next multiple of the new one; a period not
longer than zero is an ArgumentError. Outside an app it never ticks.
A clock of the seconds, redrawn once each:
HookBuilder(
builder: (context) {
final seconds = useState(0);
useInterval(
const Duration(seconds: 1),
() => seconds.value++,
);
return Text('${seconds.value} s');
},
)
Implementation
void useInterval(Duration period, void Function() onTick) =>
use(_IntervalHook(period, onTick));