Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_hooks/flutter_hooks.dart';
  3.  
  4. void main() => runApp(MyApp());
  5.  
  6. class MyApp extends StatelessWidget {
  7. @override
  8. Widget build(BuildContext context) {
  9. return MaterialApp(
  10. home: MyHomePage(),
  11. );
  12. }
  13. }
  14.  
  15. double smooth(double value,
  16. {duration = const Duration(milliseconds: 250), curve: Curves.easeInOut}) {
  17. final controller = useAnimationController(
  18. initialValue: value,
  19. lowerBound: double.negativeInfinity,
  20. upperBound: double.infinity,
  21. );
  22.  
  23. useValueChanged(value, (_, __) {
  24. controller.animateTo(value, duration: duration, curve: curve);
  25. });
  26.  
  27. return useAnimation(controller);
  28. }
  29.  
  30. class MyHomePage extends HookWidget {
  31. @override
  32. Widget build(BuildContext context) {
  33. final counter = useState(0.0);
  34.  
  35. return Scaffold(
  36. appBar: AppBar(
  37. title: Text('Hooks'),
  38. ),
  39. body: Center(
  40. child: Text(
  41. smooth(counter.value).toStringAsFixed(2),
  42. ),
  43. ),
  44. floatingActionButton: FloatingActionButton(
  45. onPressed: () {
  46. counter.value++;
  47. },
  48. child: Icon(Icons.add),
  49. ),
  50. );
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement