[Flutter动画笔记]-其他动画

3.其他动画

Hero动画

//第一个页面和第二个页面的Hero包裹的元素尽量是一样的,tag要有唯一性
//first page
Hero(
    tag: path,
    child:
    Img....
)
//second page
 Hero(
    tage: path,
    child:Img...
)
    
//动画变慢
//import 'package:flutter/scheduler.dart' show timeDilation;
//使用:在initState里增加timeDilation=5.0

雪人雪花

class _MyHomePageState extends State<MyHomePage>
    with SingleTickerProviderStateMixin {
  List<Snowflake> _snowFlakes = List.generate(200, (index) => Snowflake());
  AnimationController _controller;
  int _count = 0;
  _increment() {
    setState(() {
      this._count++;
    });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _controller = AnimationController(
      duration: Duration(seconds: 1),
      vsync: this,
    );
    _controller.repeat();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // ++++ 0000000 -------- 0

    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: Center(
          child: Container(
            height: double.infinity,
            width: double.infinity,
            decoration: BoxDecoration(
                gradient: LinearGradient(
                    begin: Alignment.topCenter,
                    end: Alignment.bottomCenter,
                    colors: [Colors.blue, Colors.lightBlue, Colors.white],
                    stops: [0.0, 0.7, 0.95])),
            child: AnimatedBuilder(
              animation: _controller,
              builder: (_, __) {
                _snowFlakes.forEach((snow) => snow.fall());
                return CustomPaint(painter: MyPainter(_snowFlakes));
              },
            ),
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            _controller.stop();
          },
          child: Icon(Icons.add),
        ));
  }
}

class MyPainter extends CustomPainter {
  List<Snowflake> _snowFlakes;
  MyPainter(this._snowFlakes);
  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()..color = Colors.white;
    print(size);
    canvas.drawCircle(size.center(Offset(0, 110)), 60.0, paint);
    canvas.drawOval(
        Rect.fromCenter(
          center: size.center(Offset(0, 280)),
          width: 200,
          height: 250,
        ),
        paint);
    _snowFlakes.forEach((snowflake) {
      canvas.drawCircle(
          Offset(snowflake.x, snowflake.y), snowflake.radius, paint);
    });
  }

  @override
  bool shouldRepaint(MyPainter oldDelegate) => true;
}

class Snowflake {
  double x = Random().nextDouble() * 400, y = Random().nextDouble() * 800;
  double radius = Random().nextDouble() * 2 + 2;
  double velocity = Random().nextDouble() * 4 + 2;

  fall() {
    y += velocity;
    if (y > 800) {
      y = 0;
      x = Random().nextDouble() * 400;
      radius = Random().nextDouble() * 2 + 2;
      velocity = Random().nextDouble() * 4 + 2;
    }
  }
}

Flare/Rive动画引用

//import 'package:flare_flutter/flare_actor.dart';
class _MyHomePageState extends State<MyHomePage> {
  String _currentName = "day_idle";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: Center(
            child: AnimatedContainer(
          duration: Duration(seconds: 2),
          color: this._currentName == "switch_day" || _currentName == "day_idle"
              ? Colors.white
              : Colors.black,
          child: GestureDetector(
            onTap: () {
              setState(() {
                if (_currentName == "day_idle") {
                  _currentName = "switch_night";
                } else if (_currentName == "night_idle") {
                  _currentName = "switch_day";
                }
              });
            },
            child: FlareActor("assets/switch day to night.flr",
                alignment: Alignment.center,
                fit: BoxFit.contain,
                animation: _currentName, callback: (switchName) {
              if (switchName == "switch_day") {
                setState(() {
                  _currentName = "day_idle";
                });
              } else if (switchName == "switch_night") {
                setState(() {
                  _currentName = "night_idle";
                });
              }
            }),
          ),
        )),
        floatingActionButton: FloatingActionButton(
          onPressed: () {},
          child: Icon(Icons.add),
        ));
  }
}
posted @   漫游者杰特  阅读(56)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示