How To Stop Or Cancle A Running Animation In Flutter

admin_img Posted By Bajarangi soft , Posted On 27-11-2020

This does not trigger any notifications. The animation stops in its current state.By default, the most recently returned TickerFuture is marked as having been canceled, meaning the future never completes and its TickerFuture.orCancel derivative future completes with a TickerCanceled error. By passing the canceled argument with the value false, this is reversed, and the futures complete successfully.

How To Stop Or Cancle A Running Animation In Flutter

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();
              }),
        ],
      ),
    );
  }
}

Related Post