AnimatedIcon Example in Flutter

This Article is posted by seven.srikanth at 11/25/2019 6:06:56 PM



In this post, we are going to see how to create an AnimatedIcon in Flutter as below.
 
As you already might know, flutter has great support for animations. And the above animation can be created with the out of the box widget, AnimatedIcon. As below,
  AnimatedIcon(
    icon: AnimatedIcons.play_pause,
    progress: controller,
  ),
However, the trickiest part of this example is to add the controller. AnimationController is a special Animation object that generates a new value whenever the hardware is ready for a new frame. By default, an AnimationController linearly produces the numbers from 0.0 to 1.0 during a given duration, (but it can be out of range in few cases). The generation of numbers is tied to the screen refresh, so typically 60 numbers are generated per second. When creating an AnimationController, you pass it a vsync argument. The presence of vsync prevents offscreen animations from consuming unnecessary resources. You can use your stateful object as the vsync by adding SingleTickerProviderStateMixin to the class definition. Here is the code from main.dart file.
import 'package:flutter/animation.dart';

import 'package:flutter/material.dart';

void main() => runApp(LogoApp());

class LogoApp extends StatefulWidget {
  _LogoAppState createState() => _LogoAppState();
}

class _LogoAppState extends State<LogoApp> with SingleTickerProviderStateMixin {
  bool isPlaying = false;

  Animation animation;

  AnimationController controller;

  @override
  void initState() {
    super.initState();

    controller = AnimationController(
        duration: const Duration(milliseconds: 500), vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
            child: IconButton(
          iconSize: 70,
          icon: AnimatedIcon(
            icon: AnimatedIcons.play_pause,
            progress: controller,
          ),
          onPressed: () => _onpressed(),
        )),
      ),
    );
  }

  @override
  void dispose() {
    controller.dispose();

    super.dispose();
  }

  _onpressed() {
    setState(() {
      isPlaying = !isPlaying;

      isPlaying ? controller.forward() : controller.reverse();
    });
  }
}


Hope this is helpful to you. 
Thanks,
Srikanth

Tags: AnimatedIcon in Flutter; Animate a Icon;








0 Comments
Login to comment.
Recent Comments

Be the first to Comment. You can ask a Query or Share your toughts or Just say thanks.


© 2018 - Fluttercentral | Email to me - seven.srikanth@gmail.com