Last active
October 23, 2020 05:12
-
-
Save RobertBrunhage/c9fefa125eba0280d456a9e86b2cf87c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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