I'm animating a widget using AnimationController (The widget is the red wave shown in the image attached). The widget starts with visibility = false
and turns true for periods of 10 seconds after the user hits the red button to speak. The problem I'm facing is that when hitting the red button for a second time I get the error:
AnimationController.stop() called after AnimationController.dispose().
And the widget never shows again. Since I'm not disposing the widget just hiding it, I can't understand what is going on. I have tried so far:
- Create the
_controller
outside/inside widget build. - Check if the widget is mounted before calling it
- Change the
AnimationController
state to false anytime the widget is hidden.
None has worked. Any idea what is wrong in my code:
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with TickerProviderStateMixin {
var _controller;
var spinkitWave;
stt.SpeechToText speech = stt.SpeechToText();
@override
void initState() {
super.initState();
_controller = AnimationController(vsync:this, duration: Duration(seconds:1), lowerBound:0, upperBound:0.1)
..addStatusListener((status) {
if (status == AnimationStatus.completed) {
if (mounted) {
_controller.reverse();
}
}});
@override
dispose() {
_controller.dispose(); // you need this
super.dispose();
}
void startListening() {
_controller = AnimationController(vsync:this, duration: Duration(seconds:1), lowerBound:0, upperBound:0.1);
speech.listen(onResult: resultListener,
onSoundLevelChange: soundLevelListener,
cancelOnError: true,);
setState(() {});
}
@override
Widget build(BuildContext context) {
spinkitWave = SpinKitWave(
color: Colors.redAccent,
type: SpinKitWaveType.center,
controller: _controller,
);
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.red,
),
home: Builder(
builder: (context) => Scaffold(
appBar: AppBar(
title: Text("Leurebeng"),
),
body: Center(
Positioned(
bottom: 10,
child: Stack(
alignment: AlignmentDirectional.bottomCenter,
children: <Widget>[
SizedBox
width: 110.0,
height: 110.0,
child: Visibility(
visible: !speech.isListening,
child: FloatingActionButton(
onPressed:
_available ? startListening : initSpeechState,
tooltip: 'Increment',
child: Icon(Icons.mic),
),
),
),
Visibility(
visible: speech.isListening, //Turns true or false after red button pressed
child:
spinkitWave
),
]),
),
),
),
);
}
}
}