Different in and out curve for animated opacity widget flutter? - flutter

I have code below , a AnimatedOpacity widget with animation curve Curves.elasticIn.
That curve used in both opacity 1 to 0 and 0 to 1.How i can use two different curve for opacity 1 to 0 and 0 to 1. ?
AnimatedOpacity(
opacity: _trueOrFalse? 1.0 : 0.0,
curve: Curves.elasticIn,
duration: const Duration(milliseconds: 2000),
child: Center(
child: SvgPicture.asset("assets/....svg"),
),

You can use it as you have done with opacity.
AnimatedOpacity(
opacity: _trueOrFalse? 1.0 : 0.0,
*//if opacity is 1.0 then use elasticIn Curve else use elasticOut for 0.0 opacity*
curve: _trueOrFalse? Curves.elasticIn: Curves.elasticOut,
duration: const Duration(milliseconds: 2000),
child: Center(
child: SvgPicture.asset("assets/....svg"),
),

Related

Flutter: animate layout changes

Is it possible to animate page items as they appear? (not with fade effect) like on android, with
android:animateLayoutChanges
Example 1
Example 2.
You can try delayed_display pub package.
But if you want to do something custom, for example:
AnimatedContainer(
duration: Duration(milliseconds: 500),
height: 50,
width: isVisible ? 100 : 50,
color: isVisible ? Colors.yellow : Colors.blue,
);

flutter animation acceleration or speed change

how can we change the acceleration of the animation in duration or make some stops in it's duration?
for example scale a widget to 2x in 3 second than stop for a second than scale it for another 2x in 3 seconds.
AnimationController_anim1 = AnimationController(
vsync: this,
duration: Duration(milliseconds: 3000),
);
ScaleTransition(
scale: Tween(begin: 1.0, end: 2).animate(_anim1),
child: ClipRect(
clipBehavior: Clip.hardEdge,
child: OverflowBox(
maxHeight: 70,
maxWidth: 70,
child: Center(
child: Container(
decoration: BoxDecoration(
color: Colors.white38,
shape: BoxShape.circle,
),
),
),
),
),
)
You can use Curve class which provides a way to map the unit interval of animation to the desired unit interval that your want
for example, curves should implement a transform function whic are mapper that receives a time value in a range of 0,1 which indicates animations current time progress and you can return a value to indicate what should be the new progress of the animation for example you can say if time is greater than 0 then return time2 which speeds up your animation as 2x also note that you should are allowed to return value in the range of 0 to 1 so in this example, your should bound the time2 to be at most 1
Here is link to Curve class documentation :
https://api.flutter.dev/flutter/animation/Curve-class.html
also, there are a few ready to use curves in the Curves class that you can already use or look to the implementation of those to inspire :
https://api.flutter.dev/flutter/animation/Curves-class.html
as I searched around, I found the solution in two different ways, one is what Amir Hossein Mirzaei mentioned, which use Curve class, it is very powerful and flexible but it is a bit complicated and hard solution (beside being so powerful), but the second way is more handy which handled by TweenSequence as bellow :
1 - make an animation controller 2- make TweenSequence and add TweenSequenceItem for each step of animation 3- set it to widget
AnimationController _anim1 = AnimationController(
vsync: this,
duration: Duration(milliseconds: 8500),
//reverseDuration: Duration(milliseconds: 1000),
)..repeat(reverse: true);
Animation<double> _animation1 = TweenSequence(
<TweenSequenceItem<double>>[
TweenSequenceItem<double>(
tween: Tween<double>(begin: 1.0, end: 1.5),
weight: 23.5,
),
TweenSequenceItem<double>(
tween: ConstantTween<double>(1.5),
weight: 6.0,
),
TweenSequenceItem<double>(
tween: Tween<double>(begin: 1.5, end: 2.0),
weight: 23.5,
),
TweenSequenceItem<double>(
tween: ConstantTween<double>(2.0),
weight: 47.0,
),
],
).animate(_anim1);
ScaleTransition(
scale: _animation1,
child: ClipRect(
clipBehavior: Clip.hardEdge,
child: OverflowBox(
maxHeight: 70,
maxWidth: 70,
child: Center(
child: Container(
decoration: BoxDecoration(
color: Colors.white38,
shape: BoxShape.circle,
),
),
),
),
),
)

How to animate height of the container relative to its children content?

I want to animate the height property of the container (AnimatedContainer in code below), but I don't want to set hard numbers like this (height: _visible2 ? 160 : 40.9) because height of this container is otherwise relative to its children content (mainly Text)
AnimatedContainer(
curve: Curves.ease,
height: _visible2 ? 160 : 40.9,
duration: Duration(milliseconds: 350),
child: AnimatedOpacity(
duration: Duration(milliseconds: 400),
opacity: _visible2 ? 1.0 : 0,
child: GestureDetector(
onTap: () {
_visible2
? setState(() {
_visible2 = !_visible2;
})
: null;
},
child: myWidget
),
),
)
Is there a way to animate from zero to relative height (for example height of this container can change in landscape mode due to more room for text to lay itself on the screen) ?

How to Create an Animated Determinate Circular Progress Indicator?

How to create something like that in Flutter:
I tried the following code, but it's drawn without being animated.
CircularProgressIndicator(
value: 0.50,
backgroundColor: Colors.grey,
valueColor: AlwaysStoppedAnimation(Colors.pink),
)
You can use an ImplicityAnimatedWidget such as TweenAnimationBuilder
Example:
This will animate over 3.5 seconds and the progress will start from 0% to 100%, you can tweak those in the begin:, end: parameters
TweenAnimationBuilder<double>(
tween: Tween<double>(begin: 0.0, end: 1),
duration: const Duration(milliseconds: 3500),
builder: (context, value, _) => CircularProgressIndicator(value: value),
)
You can also use AnimatedBuilder to take more control over the animation

Animate in and out a widget when some data changes in flutter

I have a widget that is displaying data, which changes over time, i'd like for that old data to fade out, then fade in new data when its available (passed in by the parent).
How can i achieve this?
I think this might help u:
AnimatedOpacity(
opacity: _visible ? 1.0 : 0.0,
duration: Duration(milliseconds: 500),
child: Text('Hello World!),
);