Skip to content

Instantly share code, notes, and snippets.

@RobertBrunhage
Last active October 23, 2020 05:12
Show Gist options
  • Save RobertBrunhage/c9fefa125eba0280d456a9e86b2cf87c to your computer and use it in GitHub Desktop.
Save RobertBrunhage/c9fefa125eba0280d456a9e86b2cf87c to your computer and use it in GitHub Desktop.
import 'dart:async';
import 'package:flutter/widgets.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
int useInfiniteTimer(BuildContext context) {
return use(const _InfiniteTimer());
}
class _InfiniteTimer extends Hook<int> {
const _InfiniteTimer();
@override
__InfiniteTimerState createState() => __InfiniteTimerState();
}
class __InfiniteTimerState extends HookState<int, _InfiniteTimer> {
Timer _timer;
int _incrementedCounter = 0;
@override
void initHook() {
super.initHook();
_timer = Timer.periodic(Duration(seconds: 1), (timer) {
setState(() {
_incrementedCounter = timer.tick;
});
});
}
@override
int build(BuildContext context) {
return _incrementedCounter;
}
@override
void dispose() {
_timer.cancel();
super.dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment