美文网首页
关于抖动动效

关于抖动动效

作者: 91阿生 | 来源:发表于2020-12-08 11:59 被阅读0次
1.先看动效:
shake_animation.gif
2.代码:
import 'package:flutter/material.dart';

class FCShakeScreen extends StatefulWidget {
  static const routeName = '/shake';

  @override
  _FCShakeScreenState createState() => _FCShakeScreenState();
}
class _FCShakeScreenState extends State<FCShakeScreen> with TickerProviderStateMixin {
  TextEditingController _controller;

  // 创建动效控制器
  AnimationController _shakeXAnimController;
  Animation _shakeXAnim;

  int _maxShakeCount = 3; // 最大抖动次数
  int _shakeNumber = 0; // 记录抖动次数 (forward() + reverse() 算一次动画完成)

  @override
  void initState() {
    super.initState();

    // 利用水平反复移动实现动效; xMin = 0.0 xMax = 10.0 
    _shakeXAnimController = AnimationController(vsync: this, duration: Duration(milliseconds: 150));
    _shakeXAnim = Tween(begin: 0.0, end: 10.0).animate(_shakeXAnimController);

    // 监听动画
    _shakeXAnimController.addListener(() {
      setState(() {});
    });
    // 监听动画状态
    _shakeXAnimController.addStatusListener((status) {
      if (status == AnimationStatus.completed) { // 一次forward() 完成
        _shakeNumber+=1;
        _shakeXAnimController.reverse();
      } else if (status == AnimationStatus.dismissed) { // forward() + reverse() 来回整个动画 完成
        _shakeXAnimController.reset(); // 重置

        if (_shakeNumber < _maxShakeCount) {
          _shakeXAnimController.forward();
        } else {
          _shakeNumber = 0;
        }
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Shake')
      ),
      body: Column(
        children: [
          SizedBox(height: 80),

          Transform.translate( // 这里包裹个水平移动
            offset: Offset(_shakeXAnim.value, 0), // 设置动画值 0.0 -- 10.0
            child: Container(
              margin: EdgeInsets.only(left: 30, right: 30),
              decoration: BoxDecoration(
                color: Colors.yellow[800], 
                borderRadius: BorderRadius.circular(30),
                border: Border.all(color: Color(0x80fafafa))
              ),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Padding(
                    padding: const EdgeInsets.only(left: 10, right: 10),
                    child: Icon(Icons.phone_android, size: 30, color: Colors.white),
                  ),
                  // 竖线
                  Container(width: 1.0, height: 26, color: Color(0xaafafafa)),
                  SizedBox(width: 10),

                  Expanded(
                    child: TextField(
                      controller: _controller,
                      decoration: InputDecoration(
                        hintText: "请输入11位手机号码",
                        hintStyle: TextStyle(color: Colors.white),
                        border: InputBorder.none,
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),

          SizedBox(height: 35),
          FlatButton(
            color: Colors.red[600],
            child: Text("开始"), 
            onPressed: () => _shakeXAnimController.forward()
          )
        ],
      ),
    );
  }
}

相关文章

  • 关于抖动动效

    1.先看动效: 2.代码:

  • 常用简单的动效

    两种加载动效 输入框抖动动效 核心动效 转场动画 进度条动画

  • Android列表滑动动效

    设计分析: 已经确定了要实现RecyclerView滑动监听,必然就得了解RecyclerView滑动监听实现的方...

  • Uber车辆移动动效实现

    项目中有需求需要实现车辆在地图上的行车轨迹,就像uber上小车移动的动效一样。刚接到需求时以为很简单,timer获...

  • UICollectionView 实现横向分页滑动动效

    一个简单的横向滑动功能,可以通过UIScrollView来实现,如果涉及到分页滑动效果或者更进一步的动画实现,就需...

  • 2017.03.30随笔-Android列表滑动动效

    前言:公司的一个产品需求,需要将列表的最后可见的一项做透明度以及缩放处理,并且在滑动时,最后可见的一项可以淡入淡出...

  • Android元素分散-飘移-聚合动效

    效果预览 功能说明 使用Canvas绘制元素移动动效,极致高效; 支持任意方向元素漂移(起点、终点任意); 支持修...

  • 风儿送它来, 风儿送它走。 一动不动动, 电闪雷鸣抖。

  • 大漠:手淘互动动效的探索

    内容来源:2017年6月18日,手淘前端技术专家大漠在“2017 iWeb峰会·第六届HTML5峰会 ”进行《手淘...

  • 功能动效设计的一些想法

    1.关于功能动效这个词的理解 UI动效、交互动效、转场动画、功能动效等等,“动效”这个词有的同学叫做“动画”,这些...

网友评论

      本文标题:关于抖动动效

      本文链接:https://www.haomeiwen.com/subject/cesfgktx.html