Search code examples
flutterflutter-animation

FadeAnimation code errors while trying to update simple_animations: ^2.2.1 after migration using sa_v1_migration: ^1.1.2


I'm trying to create a Fade animation and it is working it gives problems and says that it's deprecated and I need to migrate:

I get the following three problems:

  1. 'MultiTrackTween' is deprecated and shouldn't be used. See migration guide: https://pub.dev/packages/sa_v1_migration. Try replacing the use of the deprecated member with the replacement.

  2. 'Track' is deprecated and shouldn't be used. See migration guide: https://pub.dev/packages/sa_v1_migration. Try replacing the use of the deprecated member with the replacement.

  3. 'Track' is deprecated and shouldn't be used. See migration guide: https://pub.dev/packages/sa_v1_migration. Try replacing the use of the deprecated member with the replacement.

following is the code for my FadeAnimation.dart:

import 'package:flutter/material.dart';
import 'package:simple_animations/simple_animations.dart';


class FadeAnimation extends StatelessWidget {
  final double delay;
  final Widget child;

  FadeAnimation(this.delay, this.child, {AssetImage image});

  @override
  Widget build(BuildContext context) {
    final tween = MultiTrackTween([
      Track("opacity").add(Duration(milliseconds: 500), Tween(begin: 0.0, end: 1.0)),
      Track("translateY").add(
        Duration(milliseconds: 500), Tween(begin: -30.0, end: 0.0),
        curve: Curves.easeOut)
    ]);

    return PlayAnimation(
      delay: Duration(milliseconds: (500 * delay).round()),
      duration: tween.duration,
      tween: tween,
      child: child,
      builder: (context, child, animation) => Opacity(
        opacity: animation["opacity"],
        child: Transform.translate(
          offset: Offset(0, animation["translateY"]), 
          child: child
        ),
      ),
    );
  }
}

and here is my pubspec.yaml:

name: MyApp
description: A new Flutter project.

# The following line prevents the package from being accidentally published to
# pub.dev using `pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev

# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1

environment:
  sdk: ">=2.7.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter


  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.3
  simple_animations: ^2.2.1
  sa_v1_migration: ^1.1.2
  google_fonts: ^1.1.0

dev_dependencies:
  flutter_test:
    sdk: flutter

  change_app_package_name: ^0.1.2
  flutter_launcher_icons: ^0.7.5

flutter_icons:
  android: "launcher_icon"
  ios: true
  image_path: "assets/icon/icon.png"
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

# 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

  # To add assets to your application, add an assets section, like this:
  # assets:
  assets:
   - assets/images/
  #   - images/a_dot_burr.jpeg
  #   - images/a_dot_ham.jpeg


  # An image asset can refer to one or more resolution-specific "variants", see
  # https://flutter.dev/assets-and-images/#resolution-aware.

  # For details regarding adding assets from package dependencies, see
  # https://flutter.dev/assets-and-images/#from-packages

  # To add custom fonts to your application, add a fonts section here,
  # in this "flutter" section. Each entry in this list should have a
  # "family" key with the font family name, and a "fonts" key with a
  # list giving the asset and other descriptors for the font. For
  # example:


  # fonts:
  #   - family: Schyler
  #     fonts:
  #       - asset: fonts/Schyler-Regular.ttf
  #       - asset: fonts/Schyler-Italic.ttf
  #         style: italic
  #   - family: Trajan Pro
  #     fonts:
  #       - asset: fonts/TrajanPro.ttf
  #       - asset: fonts/TrajanPro_Bold.ttf
  #         weight: 700
  #
  # For details regarding fonts from package dependencies,
  # see https://flutter.dev/custom-fonts/#from-packages

I'm unable to upgrade the code in a manner that I get the desire results. Can someone please help me update this code so I don't get the deprecated errors.


Solution

  • You can copy paste run full code below
    You can define an enum AniProps and use MultiTween<AniProps>

    code snippet

    enum AniProps { opacity, translateY }
    
    class FadeAnimation extends StatelessWidget {
      final double delay;
      final Widget child;
    
      FadeAnimation(this.delay, this.child, {AssetImage image});
    
      @override
      Widget build(BuildContext context) {
        final tween = MultiTween<AniProps>()
          ..add(AniProps.opacity, 0.0.tweenTo(1.0), 500.milliseconds)
          ..add(AniProps.translateY, (-30.0).tweenTo(0.0), 500.milliseconds,
              Curves.easeOut);
    
    
        return PlayAnimation<MultiTweenValues<AniProps>>(
          delay: Duration(milliseconds: (500 * delay).round()),
          duration: tween.duration,
          tween: tween,
          child: child,
          builder: (context, child, value) => Opacity(
            opacity: value.get(AniProps.opacity),
            child: Transform.translate(
                offset: Offset(0, value.get(AniProps.translateY)), child: child),
          ),
        );
      }
    }
    
    ...
    FadeAnimation(
                10.0,
                Container(
                  height: 100,
                  width: 100,
                  color: Colors.blue,
                )),
    

    working demo

    enter image description here

    full code

    import 'package:flutter/material.dart';
    import 'package:simple_animations/simple_animations.dart';
    import 'package:supercharged/supercharged.dart';
    
    enum AniProps { opacity, translateY }
    
    class FadeAnimation extends StatelessWidget {
      final double delay;
      final Widget child;
    
      FadeAnimation(this.delay, this.child, {AssetImage image});
    
      @override
      Widget build(BuildContext context) {
        final tween = MultiTween<AniProps>()
          ..add(AniProps.opacity, 0.0.tweenTo(1.0), 500.milliseconds)
          ..add(AniProps.translateY, (-30.0).tweenTo(0.0), 500.milliseconds,
              Curves.easeOut);
        /*
        final tween = MultiTrackTween([
          Track("opacity").add(Duration(milliseconds: 500), Tween(begin: 0.0, end: 1.0)),
          Track("translateY").add(
              Duration(milliseconds: 500), Tween(begin: -30.0, end: 0.0),
              curve: Curves.easeOut)
        ]);*/
    
        return PlayAnimation<MultiTweenValues<AniProps>>(
          delay: Duration(milliseconds: (500 * delay).round()),
          duration: tween.duration,
          tween: tween,
          child: child,
          builder: (context, child, value) => Opacity(
            opacity: value.get(AniProps.opacity),
            child: Transform.translate(
                offset: Offset(0, value.get(AniProps.translateY)), child: child),
          ),
        );
      }
    }
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
            visualDensity: VisualDensity.adaptivePlatformDensity,
          ),
          home: MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);
    
      final String title;
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      int _counter = 0;
    
      void _incrementCounter() {
        setState(() {
          _counter++;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                FadeAnimation(
                    10.0,
                    Container(
                      height: 100,
                      width: 100,
                      color: Colors.blue,
                    )),
                Text(
                  'You have pushed the button this many times:',
                ),
                Text(
                  '$_counter',
                  style: Theme.of(context).textTheme.headline4,
                ),
              ],
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: Icon(Icons.add),
          ),
        );
      }
    }