title: Change the value of an animation and restart it
body: <p>I created a counter animation that increments for x seconds.
  When the user arrives at the widget, the animation starts correctly via an initState.
  I also created textfield begins and end to allow the user to update the start and end value and restart the animation.
  Problem when the user pressed the button to restart the animation, it does not work.</p>
  
  <p>PS: You can test the code if you want by copying the whole into a new project in main.dart</p>
  
  <pre class="lang-dart prettyprint-override"><code>import 'package:flutter/material.dart';
  
  void main() {
    runApp(MyApp());
  }
  
  class MyApp extends StatelessWidget {
    // This widget is the root of your application.
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
          visualDensity: VisualDensity.adaptivePlatformDensity,
        ),
        home: ShowCounter(),
      );
    }
  }
  
  //
  // The animation
  //
  
  class AnimatedCounter extends StatefulWidget {
    // Variables
    int startValue;
    int endValue;
    Duration timeAnim;
    Color color;
    double factor;
  
    // Constructor
    AnimatedCounter({
      int startValue,
      int endValue,
      Duration timeAnim: const Duration(seconds: 3),
      color: Colors.black,
      factor: 5.4,
    }) {
      this.startValue = startValue;
      this.endValue = endValue;
      this.timeAnim = timeAnim;
      this.color = color;
      this.factor = factor;
    }
  
    @override
    _AnimatedCounterState createState() =&gt; _AnimatedCounterState();
  }
  
  class _AnimatedCounterState extends State&lt;AnimatedCounter&gt;
      with SingleTickerProviderStateMixin {
    Animation&lt;double&gt; animCounter;
    AnimationController _controller;
    String i;
  
    @override
    void initState() {
      super.initState();
  
      // AnimationController
      _controller = AnimationController(duration: widget.timeAnim, vsync: this);
  
      // Animation
      animCounter = Tween&lt;double&gt;(
              begin: widget.startValue.toDouble(),
              end: widget.endValue.toDouble())
          .animate(CurvedAnimation(
        parent: _controller,
        curve: Curves.easeOutCirc,
      ))
            ..addListener(() {
              setState(() {
                i = animCounter.value.toStringAsFixed(0);
              });
            });
      _controller.forward().orCancel;
    }
  
    @override
    void dispose() {
      _controller.dispose();
      super.dispose();
    }
  
    // Layout
    @override
    Widget build(BuildContext context) {
      return Text(
        '$i',
        textScaleFactor: widget.factor,
        style: TextStyle(color: widget.color),
      );
    }
  }
  
  //
  // Display and layout
  //
  
  class ShowCounter extends StatefulWidget {
    @override
    _ShowCounterState createState() =&gt; _ShowCounterState();
  }
  
  class _ShowCounterState extends State&lt;ShowCounter&gt; {
    // Variable
    AnimatedCounter counter;
    int startvalue;
    int endValue;
  
    @override
    void initState() {
      super.initState();
      startvalue = 0;
      endValue = 42;
      counter = AnimatedCounter(startValue: startvalue, endValue: endValue);
    }
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: Text("Counter animated"),
        ),
        body: Scaffold(
          backgroundColor: Colors.blue[100],
          body: Center(
            child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: &lt;Widget&gt;[
                  counter,
                  Card(
                      margin: EdgeInsets.only(left: 20.0, right: 20.0),
                      child: Container(
                        padding: EdgeInsets.all(10),
                        child: Column(children: &lt;Widget&gt;[
                          TextField(
                            keyboardType: TextInputType.number,
                            decoration:
                                InputDecoration(labelText: "Starting value"),
                            onChanged: (String string) {
                              setState(() {
                                startvalue = int.tryParse(string);
                              });
                            },
                          ),
                          TextField(
                            keyboardType: TextInputType.number,
                            decoration: InputDecoration(labelText: "End value"),
                            onChanged: (String string) {
                              setState(() {
                                endValue = int.tryParse(string);
                              });
                            },
                          ),
                          Container(
                            height: 10,
                          ),
                          SizedBox(
                            width: double.infinity,
                            child: RaisedButton(
                              child: Text("Run"),
                              onPressed: () {
                                setState(() {
                                  // Desperate attempt to restart animation with new start/end value (not working)
                                  counter = AnimatedCounter(
                                      startValue: startvalue, endValue: endValue);
                                });
                              },
                            ),
                          ),
                        ]),
                      ))
                ]),
          ),
        ),
      );
    }
  }
  </code></pre>