Reputation: 12346
I want to pass in a value to set the starting animation value to 1.0 instead of starting from 0, but I don't know how this is achieved.
class AnimatedIconButton extends StatefulWidget {
AnimatedIconButton(this.img, this.start);
final String img;
final double start;
@override
_AnimatedIconButtonState createState() => _AnimatedIconButtonState();
}
class _AnimatedIconButtonState extends State<AnimatedIconButton>
with TickerProviderStateMixin {
AnimationController _controller;
Animation _animation;
void animateButton() {
if (_animation.value == 0)
_controller.forward();
else
_controller.reverse();
}
@override
void initState() {
super.initState();
_controller = AnimationController(
duration: Duration(milliseconds: 300),
vsync: this,
);
_animation =
CurvedAnimation(parent: _controller, curve: Curves.easeOutQuad);
_controller.addListener(() {
setState(() {});
});
_animation.value = widget.start;// does not work
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialButton(
splashColor: Colors.white,
highlightColor: Colors.white,
child: Image.asset("images/${widget.img}.png",
width: 33 + (16 * _animation.value)),
onPressed: () {
animateButton();
},
);
}
}
Upvotes: 1
Views: 10259
Reputation: 304
If you want to use an animation where forward()
will drive the value in reverse, i.e. 1.0 → 0.0
, the simplest solution is to use ReverseAnimation:
@override
void initState() {
super.initState();
_animationController =
AnimationController(vsync: this, duration: Duration(milliseconds: 500));
_hideAnimation = ReverseAnimation(
CurvedAnimation(
parent: _animationController,
curve: Curves.fastOutSlowIn,
),
),
}
Upvotes: 2
Reputation: 1745
as i understand you want to make animation starts from 1.0 instead of starting from 0.0 so here we are ..
Make global variables like:-
Animation<double> animation;
AnimationController controller;
Tween<double> tween = Tween(begin: 0, end: 100);
Override initState method:-
@override
void initState() {
super.initState();
controller=AnimationController(vsync: this, duration: Duration(seconds: 2));
animation = tween.animate(controller);
}
finally you can change start & end animation values by this :-
void accessToStartOfAnimation() {
print(tween.begin);
tween.begin = 1;
print(tween.begin);
}
Upvotes: 0
Reputation: 29468
You can use:
_controller.forward(from: 0.0);
or
_controller.reverse(from: 1.0);
And set value you need
Or for setting initial value you can use
_controller.value = widget.start;
Upvotes: 9
Reputation: 10161
Inside your code:
add _controller.forward(1.0);
.
Here you can pass the value you want.
Like below:
void animateButton() {
if (_animation.value == 0)
_controller.forward(from: 1.0);
else
_controller.reverse(from: 1.0);
}
Upvotes: 1