Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animated Widget only animates once

Tags:

flutter

I have a widget that has an animation AnimatedText and after the first animation, it doesn't seem to rebuild with setState.

Widget build(BuildContext context) {
   return createPage(texts[currentIndex]);
}

  Widget createPage(Excerpt excerpt) {
    return new Material(child: new Stack(
        children: <Widget>[
          createBackgroundImage(excerpt),
          new Column(mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                new Flexible(flex: 6, child: new SizedBox.expand()),
                new Flexible(flex: 1,
                    child: createHeader(excerpt)),
                new Flexible(flex: 4,
                    child: createTextBox(excerpt))
              ]),
        ]
    ));
  }
   Widget createTextBox(Excerpt excerpt) {
       return new SizedBox.expand(child: new FlatButton(
            color: Colors.black,
            child: new Container(
                padding: new EdgeInsets.only(top: 10.0),
                constraints: new BoxConstraints.expand(),
                child: new AnimatedText(excerpt.text)),
            onPressed: () {
              setState(() {//update index});
            }

The AnimatedTest class:

class AnimatedText extends StatefulWidget {
  String text;

  AnimatedText(this.text);

  @override
  AnimatedTextState createState() => new AnimatedTextState(text);
}

class AnimatedTextState extends State<AnimatedText>
    with SingleTickerProviderStateMixin {
  String text;
  String currentText = "";
  AnimationController controller;
  Animation animation;
  AnimatedTextState(this.text){
    print(text);
  }

  @override
  void initState() {
    super.initState();
    controller = new AnimationController(
        upperBound: text.length.toDouble(),
        duration: new Duration(seconds: 3),
        vsync: this);
    controller.addListener((){
      setState((){
        currentText = text.substring(0,controller.value.round());
      });
    });
    controller.forward();
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) =>
      new Text(currentText, style: speakerStyle());
}

The header widget rebuilds everytime but the part with the AnimatedText does not. Any ideas on how to resolve this will be helpful

like image 988
Mark Avatar asked Oct 24 '25 17:10

Mark


1 Answers

The field on the Widget should be final. The State constructor shouldn't take an argument. You should implement didUpdateConfig to grab the data from the new Widget to update your State's text field.

like image 108
Ian Hickson Avatar answered Oct 26 '25 07:10

Ian Hickson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!