How to create a Fling animation in Flutter

In this Google flutter code example we are going to learn how to create a fling animation in Flutter.

You can copy and adopt this source code example to your android project without reinventing the wheel.

The code is based on flutter version 1.

Main.dart (App entry point)

import 'package:flutter/material.dart';
import 'fling_animation_task-13.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: LogoWidget(),
    );
  }
}

pubspec.yaml

# The following section is specific to Flutter.
flutter:
  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true

  assets:
    - assets/images/umbrella.png
    - assets/images/dnut.png
    - assets/star_anim.flr

  fonts:
    - family: AvenirNext
      fonts:
        - asset: assets/fonts/AvenirNextLTPro-Regular.otf
    - family: PlayfairDisplay
      fonts:
        - asset: assets/fonts/PlayfairDisplay-Regular.ttf
    - family: Thasadith
      fonts:
        - asset: assets/fonts/Thasadith-Regular.ttf

 

fling_animation_task-13.dart

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

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.asset('assets/images/dnut.png'));
  }
}

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 FlingAnimation extends StatefulWidget {
  FlingAnimationState createState() => new FlingAnimationState();
}

class FlingAnimationState extends State<FlingAnimation>
    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 AnimatedBuilder(
        animation: _flingAnimation,
        builder: (BuildContext context, Widget child) {
          return new Center(
            child: new Transform(
              transform: new Matrix4.identity()
                ..translate(_flingAnimation.value.dx, _flingAnimation.value.dy),
              child: new Container(
                height: 100.0,
                width: 100.0,
                child: child,
              ),
            ),
          );
        },
        child: new Container(
            margin: new EdgeInsets.symmetric(vertical: 10.0),
            child: new Image.asset('assets/images/dnut.png')));
    /* return new TranslateTransition(
        child: new Container(
        margin: new EdgeInsets.symmetric(vertical: 10.0),
        child: new Image.asset('assets/images/dnut.png')), animation: _flingAnimation); */
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }
}
/* 
void main() {
  runApp(new FlingAnimation());
} */

If you have any questions or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment