Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter implementing repeat Elastic animation

for implementing this animation

enter image description here

i wrote this below code but, Elastic animation doesn't work on project and i'm not sure whats problem,

i want to have repeat of this animation

import 'package:flutter/material.dart';

void main()=>runApp(MaterialApp(home: Avatar(),));

class Avatar extends StatefulWidget {
  @override
  State<StatefulWidget> createState()=>_Avatar();
}

class _Avatar extends State<Avatar> with TickerProviderStateMixin{
  AnimationController avatarController;
  Animation<double> avatarSize;

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

    avatarController= AnimationController(
      duration: const Duration(seconds: 1),
      vsync: this,
    );

    avatarSize = new Tween(begin: 0.0, end: 1.0).animate(
      new CurvedAnimation(
        parent: avatarController,
        curve: new Interval(
          0.100,
          0.400,
          curve: Curves.elasticOut,
        ),
      ),
    );

    avatarController.repeate();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        fit:StackFit.expand,
        children: <Widget>[
          AnimatedBuilder(
            animation: avatarController,
            builder: (context, widget) => Align(
              child: Container(
                width: 50.0,
                height: 50.0,
                color:Colors.green
              ),
            ),
          )
        ],
      ),
    );
  }
}
like image 747
DolDurma Avatar asked Dec 18 '22 16:12

DolDurma


1 Answers

Output:

enter image description here


You can play with duration and Tween to fine grain it.

void main() => runApp(MaterialApp(home: Avatar()));

class Avatar extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _Avatar();
}

class _Avatar extends State<Avatar> with TickerProviderStateMixin {
  AnimationController _controller;
  Tween<double> _tween = Tween(begin: 0.75, end: 2);

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

    _controller = AnimationController(duration: const Duration(milliseconds: 700), vsync: this);
    _controller.repeat(reverse: true);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: <Widget>[
          Align(
            child: ScaleTransition(
              scale: _tween.animate(CurvedAnimation(parent: _controller, curve: Curves.elasticOut)),
              child: SizedBox(
                height: 100,
                width: 100,
                child: CircleAvatar(backgroundImage: AssetImage(chocolateImage)),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
like image 115
CopsOnRoad Avatar answered Jan 12 '23 01:01

CopsOnRoad