Skip to content

Instantly share code, notes, and snippets.

@t-artikov
Created May 13, 2019 15:37
Show Gist options
  • Save t-artikov/ef7ea17c6caffff340e337bbacbee6e5 to your computer and use it in GitHub Desktop.
Save t-artikov/ef7ea17c6caffff340e337bbacbee6e5 to your computer and use it in GitHub Desktop.
flutter hooks test
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
double smooth(double value,
{duration = const Duration(milliseconds: 250), curve: Curves.easeInOut}) {
final controller = useAnimationController(
initialValue: value,
lowerBound: double.negativeInfinity,
upperBound: double.infinity,
);
useValueChanged(value, (_, __) {
controller.animateTo(value, duration: duration, curve: curve);
});
return useAnimation(controller);
}
class MyHomePage extends HookWidget {
@override
Widget build(BuildContext context) {
final counter = useState(0.0);
return Scaffold(
appBar: AppBar(
title: Text('Hooks'),
),
body: Center(
child: Text(
smooth(counter.value).toStringAsFixed(2),
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
counter.value++;
},
child: Icon(Icons.add),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment