Skip to content

Instantly share code, notes, and snippets.

@yathit
Created March 14, 2018 01:21
Show Gist options
  • Select an option

  • Save yathit/db6c6f0673c46e71b1b4e699e4d1ed07 to your computer and use it in GitHub Desktop.

Select an option

Save yathit/db6c6f0673c46e71b1b4e699e4d1ed07 to your computer and use it in GitHub Desktop.

Revisions

  1. yathit created this gist Mar 14, 2018.
    95 changes: 95 additions & 0 deletions highlighted_icon
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,95 @@
    import 'dart:async';

    import 'package:flutter/material.dart';

    class HighLightedIcon extends StatefulWidget {
    final IconData icon;
    final double size;
    final Color color;

    HighLightedIcon(
    this.icon, {
    Key key,
    this.size = 24.0,
    this.color,
    })
    : super(key: key);
    @override
    State<HighLightedIcon> createState() {
    return new _AnimatedIconState();
    }
    }

    class _AnimatedIconState extends State<HighLightedIcon>
    with SingleTickerProviderStateMixin {
    final double dx = 4.0;
    AnimationController controller;
    Animation<double> animation;

    @override
    initState() {
    super.initState();
    controller = new AnimationController(
    duration: const Duration(milliseconds: 300), vsync: this);
    animation = new Tween(begin: widget.size, end: widget.size + dx)
    .animate(controller);

    animation.addStatusListener((status) {
    if (status == AnimationStatus.completed) {
    controller.reverse();
    } else if (status == AnimationStatus.dismissed) {
    new Future.delayed(const Duration(seconds: 2), () {
    if (!mounted) return;
    controller?.forward();
    });
    }
    });
    controller.forward();
    }

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

    @override
    Widget build(BuildContext context) {
    return new _Animator(
    icon: widget.icon,
    animation: animation,
    color: widget.color,
    size: widget.size + dx,
    );
    }
    }

    class _Animator extends AnimatedWidget {
    final double size;
    final IconData icon;
    final Color color;
    _Animator({
    Key key,
    this.icon,
    this.size,
    this.color,
    Animation<double> animation,
    })
    : super(key: key, listenable: animation);

    @override
    Widget build(BuildContext context) {
    final Animation<double> animation = listenable;
    return new Container(
    width: size,
    height: size,
    child: new Center(
    child: new Icon(
    icon,
    size: animation.value,
    color: color,
    ),
    ),
    );
    }
    }