老孟导读:此文讲解3个酷炫的3D动画效果。

下面是要实现的效果:

Flutter 中3D效果是通过 Transform 组件实现的,没有变换效果的实现:

  1. class TransformDemo extends StatelessWidget {
  2. @override
  3. Widget build(BuildContext context) {
  4. return Scaffold(
  5. appBar: AppBar(
  6. title: Text('3D 变换Demo'),
  7. ),
  8. body: Container(
  9. alignment: Alignment.center,
  10. color: Colors.white,
  11. child: Text('3D 变换Demo'),
  12. ),
  13. );
  14. }
  15. }

通过 GestureDetector 组件添加滑动事件监听:

  1. @override
  2. Widget build(BuildContext context) {
  3. return Scaffold(
  4. appBar: AppBar(
  5. title: Text('3D 变换Demo'),
  6. ),
  7. body: GestureDetector(
  8. onPanUpdate: (details) {
  9. print('$details');
  10. },
  11. child: Container(
  12. alignment: Alignment.center,
  13. color: Colors.white,
  14. child: Text('3D 变换Demo'),
  15. ),
  16. ),
  17. );
  18. }

添加 Transform 对组件进入旋转:

  1. @override
  2. Widget build(BuildContext context) {
  3. return Transform(
  4. transform: Matrix4.identity()
  5. ..setEntry(3, 2, 0.001)
  6. ..rotateX(pi/6)
  7. ..rotateY(pi/6),
  8. alignment: Alignment.center,
  9. child: Scaffold(
  10. appBar: AppBar(
  11. title: Text('3D 变换Demo'),
  12. ),
  13. body: GestureDetector(
  14. onPanUpdate: (details) {
  15. },
  16. child: Container(
  17. alignment: Alignment.center,
  18. color: Colors.white,
  19. child: Text('3D 变换Demo'),
  20. ),
  21. ),
  22. ));
  23. }

将滑动的偏移和旋转进行关联:

  1. class TransformDemo extends StatefulWidget {
  2. @override
  3. _TransformDemoState createState() => _TransformDemoState();
  4. }
  5. class _TransformDemoState extends State<TransformDemo> {
  6. double _rotateX = .0;
  7. double _rotateY = .0;
  8. @override
  9. Widget build(BuildContext context) {
  10. return Transform(
  11. transform: Matrix4.identity()
  12. ..rotateX(_rotateX)
  13. ..rotateY(_rotateY),
  14. alignment: Alignment.center,
  15. child: Scaffold(
  16. appBar: AppBar(
  17. title: Text('3D 变换Demo'),
  18. ),
  19. body: GestureDetector(
  20. onPanUpdate: (details) {
  21. setState(() {
  22. _rotateX += details.delta.dy * .01;
  23. _rotateY += details.delta.dx * -.01;
  24. });
  25. },
  26. child: Container(
  27. alignment: Alignment.center,
  28. color: Colors.white,
  29. child: Text('3D 变换Demo'),
  30. ),
  31. ),
  32. ));
  33. }
  34. }

基本已经实现了3D效果,但效果比较生硬,尤其垂直方向旋转的时候远点和近点在屏幕上的宽度是一样,

添加近大远小的效果:

  1. Transform(
  2. transform: Matrix4.identity()
  3. ..setEntry(3, 2, 0.001)
  4. ..rotateX(_rotateX)
  5. ..rotateY(_rotateY),
  6. ...

翻书效果

上面的效果类似于翻书的效果。

实现的原理:

将图片左右切割为两部分,两张图片共分割为4个新的组件,如下图,分别为1、2、3、4

代码实现:

  1. _child1 = ClipRect(
  2. child: Align(
  3. alignment: Alignment.centerLeft,
  4. widthFactor: 0.5,
  5. child: child1,
  6. ),
  7. );
  8. _child2 = ClipRect(
  9. child: Align(
  10. alignment: Alignment.centerRight,
  11. widthFactor: 0.5,
  12. child: child1,
  13. ),
  14. );
  15. _child3 = ClipRect(
  16. child: Align(
  17. alignment: Alignment.centerLeft,
  18. widthFactor: 0.5,
  19. child: child2,
  20. ),
  21. );
  22. _child4 = ClipRect(
  23. child: Align(
  24. alignment: Alignment.centerRight,
  25. widthFactor: 0.5,
  26. child: child2,
  27. ),
  28. );

将第一张图片放在第二种图片的上面,先旋转 组件2 从 0度到 90度,然后再旋转 组件3 从 -90度到0度,代码实现:

  1. Row(
  2. mainAxisAlignment: MainAxisAlignment.center,
  3. children: <Widget>[
  4. Stack(
  5. children: [
  6. _child1,
  7. Transform(
  8. alignment: Alignment.centerRight,
  9. transform: Matrix4.identity()
  10. ..setEntry(3, 2, 0.001)
  11. ..rotateY(_animation1.value),
  12. child: _child3,
  13. ),
  14. ],
  15. ),
  16. Container(
  17. width: 3,
  18. color: Colors.white,
  19. ),
  20. Stack(
  21. children: [
  22. _child4,
  23. Transform(
  24. alignment: Alignment.centerLeft,
  25. transform: Matrix4.identity()
  26. ..setEntry(3, 2, 0.001)
  27. ..rotateY(_animation.value),
  28. child: _child2,
  29. )
  30. ],
  31. )
  32. ],
  33. )

动画控制器设置:

  1. @override
  2. void initState() {
  3. init();
  4. _controller =
  5. AnimationController(vsync: this, duration: Duration(seconds: 5))
  6. ..addListener(() {
  7. setState(() {});
  8. });
  9. _animation = Tween(begin: .0, end: pi / 2)
  10. .animate(CurvedAnimation(parent: _controller, curve: Interval(.0, .5)));
  11. _animation1 = Tween(begin: -pi / 2, end: 0.0).animate(
  12. CurvedAnimation(parent: _controller, curve: Interval(.5, 1.0)));
  13. _controller.forward();
  14. super.initState();
  15. }

其中 child1, child2为两种图片,代码如下:

  1. _FlipUpDemoState(
  2. Container(
  3. width: 300,
  4. height: 400,
  5. child: Image.asset(
  6. 'assets/images/b.jpg',
  7. fit: BoxFit.cover,
  8. ),
  9. ),
  10. Container(
  11. width: 300,
  12. height: 400,
  13. child: Image.asset(
  14. 'assets/images/c.jpeg',
  15. fit: BoxFit.cover,
  16. ),
  17. ))

最后生成的效果就是开始的翻书效果。

上面是左右翻页效果,同理换成上下翻页效果:

  1. @override
  2. Widget build(BuildContext context) {
  3. return Scaffold(
  4. appBar: AppBar(),
  5. body: Column(
  6. mainAxisAlignment: MainAxisAlignment.center,
  7. children: <Widget>[
  8. Stack(
  9. children: [
  10. _upperChild1,
  11. Transform(
  12. alignment: Alignment.bottomCenter,
  13. transform: Matrix4.identity()
  14. ..setEntry(3, 2, 0.003)
  15. ..rotateX(_animation1.value),
  16. child: _upperChild2,
  17. ),
  18. ],
  19. ),
  20. SizedBox(
  21. height: 2,
  22. ),
  23. Stack(
  24. children: [
  25. _lowerChild2,
  26. Transform(
  27. alignment: Alignment.topCenter,
  28. transform: Matrix4.identity()
  29. ..setEntry(3, 2, 0.003)
  30. ..rotateX(_animation.value),
  31. child: _lowerChild1,
  32. )
  33. ],
  34. )
  35. ],
  36. ),
  37. );
  38. }

交流

老孟Flutter博客地址(330个控件用法):http://laomengit.com

欢迎加入Flutter交流群(微信:laomengit)、关注公众号【老孟Flutter】:

Flutter 实现酷炫的3D效果的更多相关文章

  1. 使用Three.js网页引擎创建酷炫的3D效果的标签墙

    使用Three.js引擎(这是开源的webgl三维引擎,gitgub)进行一个简单应用. 做一个酷炫的3d效果的标签墙(已经放在我的博客首页,大屏幕可见), 去我的博客首页看看实际效果 www.son ...

  2. 【CSS进阶】试试酷炫的 3D 视角

    写这篇文章的缘由是因为看到了这个页面: 戳我看看(移动端页面,使用模拟器观看) 运用 CSS3 完成的 3D 视角,虽然有一些晕3D,但是使人置身于其中的交互体验感觉非常棒,运用在移动端制作一些 H5 ...

  3. 15个来自 CodePen 的酷炫 CSS 动画效果【下篇】

    CodePen 是一个在线的前端代码编辑和展示网站,能够编写代码并即时预览效果.你在上面可以在线分享自己的 Web 作品,也可以欣赏到世界各地的优秀开发者在网页中实现的各种令人惊奇的效果. 今天这篇文 ...

  4. 赞!15个来自 CodePen 的酷炫 CSS 动画效果

    CodePen 是一个在线的前端代码编辑和展示网站,能够编写代码并即时预览效果.你在上面可以在线分享自己的 Web 作品,也可以欣赏到世界各地的优秀开发者在网页中实现的各种令人惊奇的效果. 今天这篇文 ...

  5. 酷炫的3D照片墙

    今天给大家分享的案例是酷炫的3D照片墙 这个案例主要是通过 CSS3 和原生的 JS 来实现的,接下来我给大家分享一下这个效果实现的过程.博客上不知道怎么放本地视频,所以只能放两张效果截图了. 1.实 ...

  6. 一分钟搞定触手app主页酷炫滑动切换效果

    代码地址如下:http://www.demodashi.com/demo/12826.html 前言: 前几天在看手机直播的时候,自己就用上了触手app.一进到主页就看上了里面页面切换的效果,自己想这 ...

  7. css3实现酷炫的3D盒子翻转效果

    简介 运用css3先在平面空间组成立方体盒子,再让整个盒子翻转起来,先来张效果图: 步骤 1.先用css将6张图片摆成下图的样子: 下面就是通过css3的3D变换将每个面进行翻转,使之成为一个立体的盒 ...

  8. 【CSS3进阶】酷炫的3D旋转透视

    之前学习 react+webpack ,偶然路过 webpack 官网 ,看到顶部的 LOGO ,就很感兴趣. 最近觉得自己 CSS3 过于薄弱,想着深入学习一番,遂以这个 LOGO 为切入口,好好研 ...

  9. photoshop打造超酷炫火焰人像效果

    效果图看上去非常的酷.制作方法跟火焰字过程差不多.唯一不同的是前期的处理,需要用滤镜把人物轮廓路径找出来,去色后再用制作火焰的过程制作.最后把最好的火焰叠加到人物上面,适当用蒙版控制区域即可.原图 最 ...

随机推荐

  1. Java面试必备Springioc上

    配置文件中 Proprety name值必须和 类中的成员变量private IUsedao  userDao一一对应 工程项目的代码为:

  2. 重学 Java 设计模式:实战迭代器模式「模拟公司组织架构树结构关系,深度迭代遍历人员信息输出场景」

    作者:小傅哥 博客:https://bugstack.cn - 原创系列专题文章 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 相信相信的力量! 从懵懂的少年,到拿起键盘,可以写一个Hell ...

  3. 语言模型 N-gram 与其平滑方法推导

    N-gram N-gram 作为一个名词表示的是一个给定文本/音频样本中有n项(音素,音节,字母,单词)的一个连续序列. 数学表达 N-gram 模型表示的是当前这个 word \(w_i\) 依赖于 ...

  4. Net链接Sql Server语法

    1.登录名.密码链接 </system.web> <appSettings> <!--<add key="MSSqlConnectionString&qu ...

  5. Spring Boot 整合 Shiro-登录认证和权限管理

    这篇文章我们来学习如何使用 Spring Boot 集成 Apache Shiro .安全应该是互联网公司的一道生命线,几乎任何的公司都会涉及到这方面的需求.在 Java 领域一般有 Spring S ...

  6. python使用selenium和requests.session登录抓取

    # Author:song from selenium import webdriver from selenium.webdriver.common.keys import Keys from re ...

  7. Redundant Paths 分离的路径【边双连通分量】

    Redundant Paths 分离的路径 题目描述 In order to get from one of the F (1 <= F <= 5,000) grazing fields ...

  8. 工作那么久,才知道的 SOLID 设计原则

    认识 SOLID 原则 无论是软件系统设计,还是代码实现,遵循有效和明确的设计原则,都利于系统软件灵活可靠,安全快速的落地,更重要的是能灵活地应对需求,简化系统扩展和维护,避免无效的加班.本文主要讨论 ...

  9. JVM 专题十六:StringTable

    1. String的基本特性 String:字符串,使用一对""引起来表示. String声明为final的,不可被继承. String实现了Serializable接口:表示字符 ...

  10. 数据可视化之DAX篇(一)Power BI时间智能函数如何处理2月29日的?

    https://zhuanlan.zhihu.com/p/109964336 ​今年是闰年,有星友问我,在Power BI中,2月29日的上年同期是怎么计算的? 这是个好问题,正好梳理一下,Power ...