Hi !
The behaviour I see is strange and somewhat hard to explain.
I couldn't find a way to isolate the components that might cause this, so it has to be explained in the context of an entire project.
Please, bear with me.
The way I'm using animate_do, is by creating "Animation Styles" like this:
import 'package:flutter/material.dart';
class AnimStyle {
const AnimStyle(this.type, this.duration, this.from, this.color);
final Duration? duration;
final int type;
final double? from;
final Color color;
}
Then I use another part of the code to wrap the animated widget:
import 'package:anim_do_issue/anim_style.dart';
import 'package:anim_do_issue/glob_utils.dart';
import 'package:animate_do/animate_do.dart';
import 'package:flutter/material.dart';
Widget animBuild(Widget toAnimate, AnimStyle animStyle, int delayId) {
Duration delay = Global.getDelay(delayId);
debugPrint("Here is the delay now ->${delay.toString()}");
switch (animStyle.type) {
case 0:
return FadeIn(
duration: animStyle.duration ?? const Duration(milliseconds: 300),
delay: delay,
child: toAnimate,
);
case 1:
return FadeInDown(
duration: animStyle.duration ?? const Duration(milliseconds: 800),
from: animStyle.from ?? 100,
delay: delay,
child: toAnimate,
);
case 2:
return FadeInDownBig(
from: animStyle.from ?? 600,
duration: animStyle.duration ?? const Duration(milliseconds: 1300),
delay: delay,
child: toAnimate,
);
case 3:
return FadeInUp(
duration: animStyle.duration ?? const Duration(milliseconds: 800),
from: animStyle.from ?? 100,
delay: delay,
child: toAnimate,
);
case 4:
return FadeInUpBig(
duration: animStyle.duration ?? const Duration(milliseconds: 1300),
from: animStyle.from ?? 600,
delay: delay,
child: toAnimate,
);
case 5:
return FadeInLeft(
duration: animStyle.duration ?? const Duration(milliseconds: 800),
from: animStyle.from ?? 100,
delay: delay,
child: toAnimate,
);
case 6:
return FadeInLeftBig(
duration: animStyle.duration ?? const Duration(milliseconds: 1300),
from: animStyle.from ?? 600,
delay: delay,
child: toAnimate,
);
case 7:
return FadeInRight(
duration: animStyle.duration ?? const Duration(milliseconds: 800),
delay: delay,
from: animStyle.from ?? 100,
child: toAnimate,
);
case 8:
return FadeInRightBig(
duration: animStyle.duration ?? const Duration(milliseconds: 1200),
delay: delay,
from: animStyle.from ?? 600,
child: toAnimate,
);
case 9:
return BounceInDown(
duration: animStyle.duration ?? const Duration(milliseconds: 1000),
delay: delay,
from: animStyle.from ?? 75,
child: toAnimate,
);
case 10:
return BounceInUp(
duration: animStyle.duration ?? const Duration(milliseconds: 1000),
from: animStyle.from ?? 75,
delay: delay,
child: toAnimate,
);
case 11:
return BounceInLeft(
duration: animStyle.duration ?? const Duration(milliseconds: 1000),
from: animStyle.from ?? 75,
delay: delay,
child: toAnimate,
);
case 12:
return BounceInRight(
duration: animStyle.duration ?? const Duration(milliseconds: 1000),
from: animStyle.from ?? 75,
delay: delay,
child: toAnimate,
);
case 13:
return ElasticIn(
duration: animStyle.duration ?? const Duration(milliseconds: 1000),
delay: delay,
child: toAnimate,
);
case 14:
return ElasticInDown(
duration: animStyle.duration ?? const Duration(milliseconds: 1000),
from: animStyle.from ?? 200,
delay: delay,
child: toAnimate,
);
case 15:
return ElasticInUp(
duration: animStyle.duration ?? const Duration(milliseconds: 1000),
from: animStyle.from ?? 200,
delay: delay,
child: toAnimate,
);
case 16:
return ElasticInLeft(
duration: animStyle.duration ?? const Duration(milliseconds: 1000),
from: animStyle.from ?? 200,
delay: delay,
child: toAnimate,
);
case 17:
return ElasticInRight(
duration: animStyle.duration ?? const Duration(milliseconds: 1000),
from: animStyle.from ?? 200,
delay: delay,
child: toAnimate,
);
case 18:
return SlideInDown(
duration: animStyle.duration ?? const Duration(milliseconds: 600),
from: animStyle.from ?? 100,
delay: delay,
child: toAnimate,
);
case 19:
return SlideInUp(
duration: animStyle.duration ?? const Duration(milliseconds: 600),
from: animStyle.from ?? 100,
delay: delay,
child: toAnimate,
);
case 20:
return SlideInLeft(
duration: animStyle.duration ?? const Duration(milliseconds: 600),
from: animStyle.from ?? 100,
delay: delay,
child: toAnimate,
);
case 21:
return SlideInRight(
duration: animStyle.duration ?? const Duration(milliseconds: 600),
from: animStyle.from ?? 100,
delay: delay,
child: toAnimate,
);
case 22:
return FlipInX(
duration: animStyle.duration ?? const Duration(milliseconds: 800),
delay: delay,
child: toAnimate,
);
case 23:
return FlipInY(
duration: animStyle.duration ?? const Duration(milliseconds: 800),
delay: delay,
child: toAnimate,
);
case 24:
return ZoomIn(
duration: animStyle.duration ?? const Duration(milliseconds: 500),
delay: delay,
from: animStyle.from ?? 1.0,
child: toAnimate,
);
case 25:
return JelloIn(
duration: animStyle.duration ?? const Duration(milliseconds: 800),
delay: delay,
child: toAnimate,
);
default:
return FadeIn(
duration: animStyle.duration ?? const Duration(milliseconds: 300),
delay: delay,
child: toAnimate,
);
}
}
The issue I'm having comes from the delays. This is what the function computing the delays looks like:
class Global {
factory Global() {
return _singleton;
}
Global._internal();
static final Global _singleton = Global._internal();
static final List<int> _delays = [0, 100, 0, 100, 0, 100, 0, 100];
static Duration getDelay(int id) {
_delays[id]++;
// return Duration(milliseconds: _delays[id] * _delays[id + 1]); // broken !
return Duration(seconds: _delays[id]);
}
static void resetDelay(int id, int delayFactor) {
_delays[id] = 0;
_delays[id + 1] = delayFactor;
}
}
The idea is to animate every element of the screen with a specific delay factor (by default, 100 msec).
I can pass a delay ID value 0, 2, 4 or 6 to the animStyle.build
call, which will then call the Global.getDelay(delayId)
which will increase the number of delayed elements and give me the delay for that specific element in the page.
When I run this idea in a simplified project like this:
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
List<AnimStyle> _toAnimate = [
AnimStyle(7, null, null, Colors.indigo),
AnimStyle(22, null, null, Colors.red),
AnimStyle(12, null, null, Colors.green),
AnimStyle(17, null, null, Colors.pink),
];
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: _createFromStyle(_toAnimate),
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
List<Widget> _createFromStyle(List<AnimStyle> toAnimate) {
final toReturn = <Widget>[];
for (var i = 0; i < toAnimate.length; i++) {
toReturn.add(animBuild(_getContainer(toAnimate[i].color), toAnimate[i], 0));
}
return toReturn;
}
Widget _getContainer(Color color) {
return Container(
height: 50,
width: 50,
color: color,
);
}
}
The result is exactly what is expected:
https://user-images.githubusercontent.com/86661185/170222582-30c622d6-7a58-48ef-b927-19bf6eb61b04.mov
When printing the values of the final delay = Global.getDelay(delayId)
this is seen in the console:
I/flutter (20459): Here is the delay now ->0:00:01.000000
I/flutter (20459): Here is the delay now ->0:00:02.000000
I/flutter (20459): Here is the delay now ->0:00:03.000000
I/flutter (20459): Here is the delay now ->0:00:04.000000
Exactly what we see on the screen. So far, so good.
However, when this rather simple idea is merged within the rest of the project, then the delays given by Global.getDelay()
seem to be ignored and the LAST delay is applied to ALL elements.
My animations look like this instead:
https://user-images.githubusercontent.com/86661185/170224139-fce51749-0bac-4a34-abc3-3dff2bb53a71.mov
When printing the values of the final delay = Global.getDelay(delayId)
this is what's logged:
I/flutter (22042): Here is the delay now ->0:00:00.000000
I/flutter (22042): Here is the delay now ->0:00:01.000000
I/flutter (22042): Here is the delay now ->0:00:02.000000
I/flutter (22042): Here is the delay now ->0:00:03.000000
But this is not what we see. There is actually no delay of 0, 1, 2 and 3 seconds between the displayed elements.
They are all having a 3 seconds delay (ea. the maximum or the last value).
I hope I managed to explain the behaviour...
I am not sure if animate_do or dart or flutter could cause this, but I decided to start here.
Is there any reason to believe that animate_do could cause this?
If you have ideas or workarounds or anything else to try to fix this, I'll take it.
I'm desperate at this point.
Thank you !
flutter doctor -v
[✓] Flutter (Channel stable, 3.0.0, on macOS 12.3.1 21E258 darwin-x64, locale en-BE)
• Flutter version 3.0.0 at /Users/cekrozl1/Desktop/OneSpan/MSS/FLUTTER/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision ee4e09cce0 (2 weeks ago), 2022-05-09 16:45:18 -0700
• Engine revision d1b9a6938a
• Dart version 2.17.0
• DevTools version 2.12.2
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
• Android SDK at /Users/cekrozl1/Library/Android/sdk
• Platform android-31, build-tools 30.0.3
• ANDROID_HOME = /Users/cekrozl1/Library/Android/sdk
• Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 13.3)
• Xcode at /Applications/Xcode.app/Contents/Developer
• CocoaPods version 1.11.2
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 2021.2)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
[✓] IntelliJ IDEA Community Edition (version 2021.2.3)
• IntelliJ at /Applications/IntelliJ IDEA CE.app
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
[✓] VS Code (version 1.67.2)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.40.0
[✓] Connected device (3 available)
• SM G973F (mobile) • R58N42DX67H • android-arm64 • Android 12 (API 31)
• macOS (desktop) • macos • darwin-x64 • macOS 12.3.1 21E258 darwin-x64
• Chrome (web) • chrome • web-javascript • Google Chrome 101.0.4951.64
[✓] HTTP Host Availability
• All required HTTP hosts are available