Created
May 13, 2019 15:37
-
-
Save t-artikov/ef7ea17c6caffff340e337bbacbee6e5 to your computer and use it in GitHub Desktop.
flutter hooks test
This file contains hidden or 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 '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