Stop Or Cancle A Running Animation
Complete Code For Stop Or Cancle A Running Animation In Flutter
main.dart
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.red,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage>
with SingleTickerProviderStateMixin {
AnimationController animationController;
Animation<double> animation;
@override
void initState() {
super.initState();
animationController = AnimationController(
vsync: this,
duration: Duration(seconds: 5),
)..addListener(() => setState(() {}));
animation = Tween<double>(
begin: 50.0,
end: 120.0,
).animate(animationController);
animationController.forward();
}
@override
void dispose() {
animationController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.lightGreen[800],
title: Text("Stop Animation")),
body: Column(
children: <Widget>[
Expanded(
child: Center(
child: Container(
height: animation.value,
width: animation.value,
child: FlutterLogo(),
),
),
),
RaisedButton(
child: Text("Stop Animation"),
textColor: Colors.white,
color: Colors.lightGreen[800],
onPressed: () {
animationController.stop();
}),
],
),
);
}
}