2024-03-05 22:42:22 -05:00
|
|
|
import 'package:async/async.dart';
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
|
|
|
|
|
2025-07-29 00:34:03 +05:30
|
|
|
RestartableTimer useTimer(Duration duration, void Function() callback) {
|
|
|
|
|
return use(_TimerHook(duration: duration, callback: callback));
|
2024-03-05 22:42:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _TimerHook extends Hook<RestartableTimer> {
|
|
|
|
|
final Duration duration;
|
|
|
|
|
final void Function() callback;
|
|
|
|
|
|
2025-07-29 00:34:03 +05:30
|
|
|
const _TimerHook({required this.duration, required this.callback});
|
2024-03-05 22:42:22 -05:00
|
|
|
@override
|
2025-07-25 08:07:22 +05:30
|
|
|
HookState<RestartableTimer, Hook<RestartableTimer>> createState() => _TimerHookState();
|
2024-03-05 22:42:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _TimerHookState extends HookState<RestartableTimer, _TimerHook> {
|
|
|
|
|
late RestartableTimer timer;
|
|
|
|
|
@override
|
|
|
|
|
void initHook() {
|
|
|
|
|
super.initHook();
|
|
|
|
|
timer = RestartableTimer(hook.duration, hook.callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
RestartableTimer build(BuildContext context) {
|
|
|
|
|
return timer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void dispose() {
|
|
|
|
|
timer.cancel();
|
|
|
|
|
super.dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|