{"id":224,"date":"2020-10-20T06:08:12","date_gmt":"2020-10-20T06:08:12","guid":{"rendered":"http:\/\/bartvwezel.nl\/?p=224"},"modified":"2020-10-20T06:09:06","modified_gmt":"2020-10-20T06:09:06","slug":"animated-bar-chart-with-flutter-hooks","status":"publish","type":"post","link":"https:\/\/bartvwezel.nl\/flutter\/animated-bar-chart-with-flutter-hooks\/","title":{"rendered":"Animated Bar Chart with Flutter Hooks"},"content":{"rendered":"\n
In this blog post<\/a>, we described how to use animations to animate a bar chart. In this blog post were are going to add another possible way to animate the bar chart. We are going to use Flutter Hooks to extract the disposing and setup of the animation controller into the hook. This means we do not have to do this for each animation we want to create in our application. <\/p>\n\n\n\n Before we can start with coding, we are going to add a dependency to the project, namely Flutter Hooks<\/a>. For this we have to update our pubspec.yaml<\/strong> with the following dependency: <\/p>\n\n\n\n Do not forget to install the dependency, running the following command:<\/p>\n\n\n\n That is it! We can now use Flutter Hooks in the project. <\/p>\n\n\n\n Let’s take a quick look at the previous animation before we start:<\/p>\n\n\n\n Here we have to manage the controller and the animation. We have to add logic to the initState and to the dispose methods, to set up and clean up the animation and controller. So how would we do this with a hook:<\/p>\n\n\n\n Here we extended the BarChart<\/strong> with an HookWidget<\/strong>. This allows us to use hooks. The hooks were are using is:<\/p>\n\n\n\n The logic of disposing and setting up the controller is done in the useAnimationController<\/strong> Hook. We had to wrap the BarChartPainter<\/strong> in a BarChartContainer<\/strong>. The reason for this is that the BarChartPainter<\/strong> already extends the CustomPainter<\/strong> and the Widget<\/strong> that gets passed the AnimationController<\/strong> should extend the AnimatedWidget<\/strong>. That was all we had to do, to update a default animation to animation with a hook. Thanks for reading, you can find the code here<\/a>! <\/p>\n\n\n\nConfiguring the project<\/h2>\n\n\n\n
dependencies:\n flutter_hooks: ^0.14.1<\/code><\/pre>\n\n\n\n
flutter pub get<\/code><\/pre>\n\n\n\n
Using the HookWidget<\/h2>\n\n\n\n
class _BarChartState extends State<BarChart> with SingleTickerProviderStateMixin {\n Map<String, int> data = {\n "Banana": 16,\n "Orange": 8,\n "Appple": 10,\n "Kiwi": 10,\n "Pear": 3,\n };\n Animation<double> animation;\n AnimationController controller;\n @override\n void initState() {\n super.initState();\n controller = AnimationController(duration: const Duration(seconds: 2));\n animation = Tween<double>(begin: 0, end: 100).animate(controller)\n ..addListener(() {\n setState(() {});\n });\n controller.forward();\n }\n\n @override\n Widget build(BuildContext context) {\n return CustomPaint(\n painter: BarChartPainter(data, "Favorite Fruit", animation.value),\n child: Container(\n width: 350,\n ),\n );\n }\n\n @override\n void dispose() {\n controller.dispose();\n super.dispose();\n }\n}<\/code><\/pre>\n\n\n\n
class BarChart extends HookWidget {\n final Duration duration = const Duration(seconds: 2);\n\n @override\n Widget build(BuildContext context) {\n final controller = useAnimationController(duration: duration);\n controller.forward();\n return BarChartContainer(controller: controller);\n }\n}\n\nclass BarChartContainer extends AnimatedWidget {\n BarChartContainer({AnimationController controller})\n : super(\n listenable: Tween<double>(begin: 0, end: 100).animate(controller));\n\n static const Map<String, int> data = {\n "Banana": 16,\n "Orange": 8,\n "Appple": 10,\n "Kiwi": 10,\n "Pear": 3,\n };\n\n @override\n Widget build(BuildContext context) {\n Animation<double> animation = listenable;\n return CustomPaint(\n painter: BarChartPainter(data, "Favorite Fruit", animation.value),\n child: Container(\n width: 350,\n ),\n );\n }\n}<\/code><\/pre>\n\n\n\n
final controller = useAnimationController(duration: duration);\n controller.forward();<\/code><\/pre>\n\n\n\n