How to Create an Animated Determinate Circular Progress Indicator? - flutter

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

Related

Different in and out curve for animated opacity widget 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"),
),

Flutter current screen out, new in Animation

I need to animate current screen A out, then screen B in. PageRouteBuilder, transition:
#override
Widget buildTransitions(
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
Widget child,
) {
var animCurrentWidget = Tween<Offset>(begin: const Offset(0, 0), end: const Offset(0, 1))
.chain(CurveTween(curve: const Interval(0, 0.5, curve: Curves.linear)))
.animate(animation);
var animNewWidget = Tween<Offset>(begin: const Offset(0, 1), end: const Offset(0, 0))
.chain(CurveTween(curve: const Interval(0.5, 1, curve: Curves.linear)))
.animate(animation);
return Stack(
children: <Widget>[
SlideTransition(position: animCurrentWidget, child: parent, transformHitTests: false),
SlideTransition(position: animNewWidget, child: child),
],
);
}
Pretty simple, current screen leaves with up->down, new one goes in with sliding down->up after first one left.
But the tricky part is that you need to have access to current Widget on screen to do that, which removes use of
onGenerateRoute: MyCustomRouting.generateRoute
And forces me to work with routes in local scope of every possible screen app can have.
What are the best practices for tasks like that?
Is there a way to get current widget inside MyCustomRouting.generateRoute without going full static?
Is there a way to get current 'on top' widget inside other widget in the tree?

How to animate color transitions in flutter?

I'm creating a custom checkbox-like widget that uses Card. I'm able to get the colors changing if the values are the same onTap. However, I want it to transition the color instead of it suddenly changing.
Here's my code:
return Card(
color: selected ? AppColors.primary : AppColors.primaryLight,
I'm relatively new to animations in Flutter, but coming from a strong Frontend/CSS understanding, the way I could achieve something like this is to add transition on a CSS property -- is there something similar to this in Flutter? If not, could someone point me in the right direction?
What I would do is instead of using the Card widget, I would use an AnimatedContainer and have the same condition as you for the color parameter. You will get a smooth transition when the selected value changes. However, you may need to also add boxShadow to have the same elevation effect as Card
AnimatedContainer(
duration: const Duration(milliseconds: 700),
curve: Curves.easeInOut,
decoration:BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.black,
blurRadius: 5,
spreadRadius: 5,
offset: Offset(0, 2),
),
],
color: selected ? AppColors.primary : AppColors.primaryLight,
),
)
One way to do this would be an AnimatedContainer
return Card(
child: AnimatedContainer(
duration: const Duration(milliseconds: 1000),
height: 200,
width: 200,
color: selected ? Colors.red : Colors.blue,
child: FooWidget(),
),
)

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 really use SizeTransition in Flutter?

I want to make the same effect as this: https://api.flutter.dev/flutter/widgets/SizeTransition-class.html but starting from the beginning of an image. I tried everything and this does not work!
I tried to put Align in it, to put a container with the width modified by the value of the animation.. nothing works!
I have an animation before, and then it will call forward() on this one when it finishes (there is a function called complete that calls the forward() function).
initState() {
_sizeController =
AnimationController(duration: Duration(seconds: 1), vsync: this)
..addListener(() {
setState(() {});
});
_sizeAnimation = Tween(begin: 0.0, end: 27.0).animate(CurvedAnimation(
parent: _sizeController, curve: Curves.fastLinearToSlowEaseIn));
}
AnimatedContainer(
width: _width,
height: _height,
duration: Duration(milliseconds: 200),
decoration: BoxDecoration(
color: _color,
border: Border.all(
color: Color(0xFF707070), width: 2, style: BorderStyle.solid),
borderRadius: BorderRadius.circular(14.0),
),
child: SizeTransition(
sizeFactor: _sizeAnimation,
axis: Axis.horizontal,
axisAlignment: -1,
child:
Center(child: Image.asset('assets/images/check.png')),
),
Or it does nothing or it animates by scaling only.. How can i achieve the same effect as the example video on the widget page? I want that effect to happen showing a checkmark image inside a circle container. And before this the animatedcontainer will be a linear container that turns into the circle, and then the checkmark appears.
you need to call _sizeController.forward() to run the animation