弹跳按钮颤动

Han*_*aum 3 animation android ios flutter

如何创建按下按钮的动画。例如,反射应用程序中的弹跳动画。

到目前为止我做了什么:

  AnimationController _animationController;
  Animation<double> _animation;

  @override
  void initState() {
    _animationController = AnimationController(
      vsync: this,
      duration: Duration(seconds: 2000),
    );

    _animation =
        CurvedAnimation(parent: _animationController, curve: Curves.bounceInOut);

    super.initState();
  }
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Kis*_*nga 7

如果您想让它禁用动画,也可以使用此小部件类,然后将其设置为 onPress null 并传递子项。

import 'package:flutter/material.dart';

class Bouncing extends StatefulWidget {
  final Widget child;
  final VoidCallback onPress;

  Bouncing({@required this.child, Key key, this.onPress})
      : assert(child != null),
        super(key: key);

  @override
  _BouncingState createState() => _BouncingState();
}

class _BouncingState extends State<Bouncing>
    with SingleTickerProviderStateMixin {
  double _scale;
  AnimationController _controller;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      vsync: this,
      duration: Duration(milliseconds: 100),
      lowerBound: 0.0,
      upperBound: 0.1,
    );
    _controller.addListener(() {
      setState(() {});
    });
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    _scale = 1 - _controller.value;
    return Listener(
      onPointerDown: (PointerDownEvent event) {
        if (widget.onPress != null) {
          _controller.forward();
        }
      },
      onPointerUp: (PointerUpEvent event) {
        if (widget.onPress != null) {
          _controller.reverse();
          widget.onPress();
        }
      },
      child: Transform.scale(
        scale: _scale,
        child: widget.child,
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

用法:

Bouncing(
  onPress: (){},
  child: Container()
);
Run Code Online (Sandbox Code Playgroud)

输出: