how to use pause animation in flutter

admin_img Posted By Bajarangi soft , Posted On 30-10-2020

I wasn't aware of that code, here is how I did. All you need is Controller.reset() to stop the animation and Controller.repeat() to start it.However if you need to start the animation just once, use Controller.forward() and Controller.reverse().

How To Use Pause Animation Using Flutter Android App

Pause Animation
Complete Code For In  Pause Animation 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,
      home: MyHomePage()
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage>
    with SingleTickerProviderStateMixin {
  AnimationController animationController;
  Animation<double> animation;
  bool _isPlaying = true;

  @override
  void initState() {
    super.initState();
    animationController = AnimationController(
      vsync: this,
      duration: Duration(seconds: 5),
    )
      ..addListener(() => setState(() {}))
      ..repeat();
    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(
      backgroundColor: Colors.black,
      appBar: AppBar(
        backgroundColor: Colors.red[700],
          title: Text("Pause Animation")),
      body: Column(
        children: <Widget>[
          Expanded(
            child: Center(
              child: Container(
                height: animation.value,
                width: animation.value,
                child: FlutterLogo(),
              ),
            ),
          ),
          RaisedButton(
              child: Text(_isPlaying ? "Pause Animation" : "Play Animation",style: TextStyle(color: Colors.white),),
              color: Colors.red[700],
              onPressed: () {
                _isPlaying
                    ? animationController.reset()
                    : animationController.repeat();
              }),
        ],
      ),
    );
  }
}

Related Post