Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to slow down a fling animation in Flutter?

Tags:

flutter

I was playing with the fling animation based on the grid demo in Flutter Gallery. I made the example below work, but the animation plays very fast. I could barely see it unless I slow it down by using timeDilation. Changing the value of velocity doesn't seem to have much effect. Should I look at something else? Thanks!

import 'package:flutter/animation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart' show timeDilation;

const kLogoUrl =
    "https://raw.githubusercontent.com/dart-lang/logos/master/logos_and_wordmarks/dart-logo.png";

class LogoWidget extends StatelessWidget {
  // Leave out the height and width so it fills the animating parent
  build(BuildContext context) {
    return new Container(
        margin: new EdgeInsets.symmetric(vertical: 10.0),
        child: new Image.network(kLogoUrl));
  }
}

class TranslateTransition extends StatelessWidget {
  TranslateTransition({this.child, this.animation});

  Widget child;
  Animation<Offset> animation;

  Widget build(BuildContext context) {
    return new AnimatedBuilder(
        animation: animation,
        builder: (BuildContext context, Widget child) {
          return new Center(
            child: new Transform(
              transform: new Matrix4.identity()
                ..translate(animation.value.dx, animation.value.dy),
              child: new Container(
                height: 100.0,
                width: 100.0,
                child: child,
              ),
            ),
          );
        },
        child: child);
  }
}

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

class LogoAppState extends State<LogoApp> with TickerProviderStateMixin {
  Animation<Offset> _flingAnimation;
  AnimationController _controller;

  initState() {
    super.initState();
    timeDilation = 5.0;

    _controller = new AnimationController(
      vsync: this,
    );

    _flingAnimation = new Tween<Offset>(
      begin: new Offset(-150.0, -150.0),
      end: new Offset(150.0, 150.0),
    )
        .animate(_controller);

    _controller
      ..value = 0.0
      ..fling(velocity: 0.1)
      ..addListener(() {
//        print(_flingAnimation.value);
      });
  }

  Widget build(BuildContext context) {
    return new TranslateTransition(
        child: new LogoWidget(), animation: _flingAnimation);
  }

  @override
  dispose() {
    _controller.dispose();
  }
}

void main() {
  runApp(new LogoApp());
}
like image 502
Tao Avatar asked Aug 14 '17 22:08

Tao


People also ask

What is vsync in animation Flutter?

What is vsync ? Vsync basically keeps the track of screen, so that Flutter does not renders the animation when the screen is not being displayed.

What is fling velocity?

Fling-based animation uses a friction force that is proportional to an object's velocity. Use it to animate a property of an object and to end the animation gradually. It has an initial momentum, which is mostly received from the gesture velocity, and gradually slows down.

How do you fade transition in Flutter?

FadeTransition allows you to blur a widget in and out by animating its opacity. In this way, you simply need to give the opacity boundary with animation and a child widget on which the animation must be performed.

How do I stop tween animation in Flutter?

All you need is Controller. reset() to stop the animation and Controller. repeat() to start it. However if you need to start the animation just once, use Controller.


1 Answers

fling uses a SpringSimulation with default parameters, one of which is the spring constant. Even if you start with velocity zero, a spring will spring at a speed determined by the spring constant. So what's happening is that you're going from 0.0 to 1.0 with a pretty tight critically-damped string.

Also, because you're using a NetworkImage, you don't see anything because the image takes longer to load than the animation takes to run.

If you replace LogoWidget with FlutterLogo, you'll see what's happening better.

If you want it to go slower, you can use animateWith instead of fling to pass it a specific SpringSimulation with your own custom parameters.

The existence of fling is a bit of a historical accident. It's designed to be used primarily with AnimationControllers with a lowerBound and upperBound given in pixels, rather than over the 0.0...1.0 default range.

like image 106
Ian Hickson Avatar answered Nov 15 '22 10:11

Ian Hickson