Skip to content

Instantly share code, notes, and snippets.

@rei-codes
rei-codes / with_hooks.dart
Last active November 3, 2020 12:32
with useState
class HomePage extends HookWidget {
@override
Widget build(BuildContext context) {
final counter = useState(0);
void onPressed() {
counter.value++;
}
return Scaffold(
@rei-codes
rei-codes / without_hooks.dart
Last active November 3, 2020 12:32
without useState
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
int counter = 0;
void onPressed() {
setState(() {
@rei-codes
rei-codes / without_hooks.dart
Created November 3, 2020 12:33
without useFuture
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
Future future;
Future<String> fetchData() async {
await Future.delayed(Duration(seconds: 2));
@rei-codes
rei-codes / with_hooks.dart
Created November 3, 2020 12:33
with useFuture
class HomePage extends HookWidget {
Future<String> fetchData() async {
await Future.delayed(Duration(seconds: 2));
return 'ehe';
}
@override
Widget build(BuildContext context) {
final future = useMemoized(fetchData);
final snapshot = useFuture(future);
@rei-codes
rei-codes / without_hooks.dart
Created November 3, 2020 12:49
without useAnimationController
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage>
with SingleTickerProviderStateMixin {
AnimationController controller;
@override
@rei-codes
rei-codes / with_hooks.dart
Created November 3, 2020 12:49
with useAnimationController
class HomePage extends HookWidget {
@override
Widget build(BuildContext context) {
final controller = useAnimationController(duration: Duration(seconds: 4));
controller.repeat();
return Scaffold(
body: Center(
child: AnimatedBuilder(
animation: controller,
builder: (_, child) {
@rei-codes
rei-codes / without_hooks.dart
Created November 3, 2020 12:56
without useTabController
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage>
with SingleTickerProviderStateMixin {
TabController controller;
@override
@rei-codes
rei-codes / with_hooks.dart
Created November 3, 2020 12:56
with useTabController
class HomePage extends HookWidget {
@override
Widget build(BuildContext context) {
final controller = useTabController(initialLength: 3);
onPressed() => controller.animateTo(Random().nextInt(3));
return Scaffold(
appBar: AppBar(
bottom: TabBar(
@rei-codes
rei-codes / with_hooks.dart
Created November 4, 2020 20:57
with useEffect
class HomePage extends HookWidget {
@override
Widget build(BuildContext context) {
useEffect(
() {
// initState
return () {
// dispose
};
},
@rei-codes
rei-codes / without_hooks.dart
Created November 4, 2020 20:57
without useEffect
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
void initState() {
super.initState();
}