0
I have a problem with a numerical transition animation.
I wanted the animation to always continue from the last generated number, but is always repeating the first value transition made
I’ve looked for someone who had similar problems, but I couldn’t find
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> with SingleTickerProviderStateMixin {
Animation numberTransitionAnimation;
AnimationController animationController;
@override
void initState() {
super.initState();
animationController = AnimationController(duration: Duration(seconds: 1), vsync: this);
numberTransitionAnimation = IntTween(begin: currentNumber, end: newNumber).animate(CurvedAnimation(parent: animationController, curve: Curves.linear));
}
int selectedNumber;
int currentNumber = 0;
int newNumber = 0;
int total = 2000;
void selectNumber(selectedNumber){
newNumber = selectedNumber + currentNumber;
currentNumber = newNumber;
animationController.reset();
animationController.forward();
}
@override
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: animationController,
builder: (BuildContext context, Widget child){
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(numberTransitionAnimation.value.toString() + " / $total"),
// \/ Test to see if the method is working
Text(currentNumber.toString()),
RaisedButton(onPressed: () => selectNumber(100), child: Text('100')),
RaisedButton(onPressed: ()=> selectNumber(200), child: Text('200')),
RaisedButton(onPressed: ()=> selectNumber(300), child: Text('300')),
],
)
),
);
}
);
}
}
I’ve tried using the setState (){}
, but it didn’t work. Funny that you are updating test values even without setState.
I’ve tried to use animationController.forward(from: currentNumber.toDouble());
, but it didn’t help either.
I even imagined that the problem was that maybe the variables were at the same value at the time of initialization, but it is not. I gave an initial value of 0 to currentNumber
and 200 to newNumber
, animation is done, but the value of currentNumber
has already changed and the animation does not update, continues animating from 0 to 200.
I’ve tried to use it too animationController.reverse()
, unsuccessful.
Thank you in advance for being willing to read my problem.
Thanks for your help. I’m a beginner in the world of programming and there are things I think can only be done the way I learned in tutorials. I didn’t know you could add
IntTween
inside the method. I suspected the part that the animation would not be re-generated with changing variables, but did not know how to solve. AboutcurrentNumber = newNumber;
, was a command line for the new number generated innewNumber
were stored incurrentNumber
, since it was going to be the new current number and was ready for when the button calling the method was clicked again.– Victor