在Flutter中制作翻转卡片动画的完整实例代码
作者:Breeze_Luckly 时间:2023-06-23 23:31:21
前言
本文将带您了解在 Flutter 中制作翻转卡片动画的两个完整示例。第一个示例从头开始实现,第二个示例使用第三方包。闲话少说,让我们动手吧。
使用自写代码
本示例使用变换小部件创建翻转卡片效果。
预览
我们将要构建的演示应用程序显示了两张隐藏一些秘密的卡片。您可以通过按“揭示秘密” 按钮来揭开面具背后的东西。最上面的卡片展示了一个水平翻转动画,底部一张展示了一个垂直翻转动画。
完整代码
// main.dart
import 'package:flutter/material.dart';
import 'dart:math';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
// Remove the debug banner
debugShowCheckedModeBanner: false,
title: 'Kindacode.com',
theme: ThemeData(
primarySwatch: Colors.amber,
),
home: HomePage());
}
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State with SingleTickerProviderStateMixin {
late AnimationController _controller;
late Animation _animation;
AnimationStatus _status = AnimationStatus.dismissed;
@override
void initState() {
super.initState();
_controller =
AnimationController(vsync: this, duration: Duration(seconds: 1));
_animation = Tween(end: 1.0, begin: 0.0).animate(_controller)
..addListener(() {
setState(() {});
})
..addStatusListener((status) {
_status = status;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Kindacode.com'),
),
body: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
SizedBox(
height: 30,
),
// Horizontal Flipping
Transform(
alignment: FractionalOffset.center,
transform: Matrix4.identity()
..setEntry(3, 2, 0.0015)
..rotateY(pi * _animation.value),
child: Card(
child: _animation.value <= 0.5
? Container(
color: Colors.deepOrange,
width: 240,
height: 300,
child: Center(
child: Text(
'?',
style: TextStyle(fontSize: 100, color: Colors.white),
)))
: Container(
width: 240,
height: 300,
color: Colors.grey,
child: Image.network(
'https://www.kindacode.com/wp-content/uploads/2021/09/girl.jpeg',
fit: BoxFit.cover,
)),
),
),
// Vertical Flipping
SizedBox(
height: 30,
),
Transform(
alignment: FractionalOffset.center,
transform: Matrix4.identity()
..setEntry(3, 2, 0.0015)
..rotateX(pi * _animation.value),
child: Card(
child: _animation.value <= 0.5
? Container(
color: Colors.deepPurple,
width: 240,
height: 300,
child: Center(
child: Text(
'?',
style: TextStyle(fontSize: 100, color: Colors.white),
)))
: Container(
width: 240,
height: 300,
color: Colors.grey,
child: RotatedBox(
quarterTurns: 2,
child: Image.network(
'https://www.kindacode.com/wp-content/uploads/2021/09/flower.jpeg',
fit: BoxFit.cover,
),
)),
),
),
ElevatedButton(
onPressed: () {
if (_status == AnimationStatus.dismissed) {
_controller.forward();
} else {
_controller.reverse();
}
},
child: Text('Reveal The Secrets'))
],
),
),
);
}
}
使用第三个插件
从头开始编写代码可能既麻烦又耗时。如果您想快速而整洁地完成工作,那么使用插件中的预制小部件是一个不错的选择。下面的示例使用了一个名为flip_card的很棒的包。
编码
1.将插件添加到您的项目中:
flutter pub add flip_card
您可能需要运行:
flutter pub get
安装插件。
2.实现插件提供的FlipCard小部件:
Center(
child: FlipCard(
direction: FlipDirection.HORIZONTAL,
front: Container(
width: 300,
height: 400,
color: Colors.red,
),
back: Container(
width: 300,
height: 400,
color: Colors.blue,
),
),
),
结论
我们已经通过几个在应用程序中实现翻转效果的示例。
来源:https://juejin.cn/post/7017586962194726926
标签:flutter,翻转,卡片
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java中的Struts2框架拦截器之实例代码
2023-06-21 19:04:03
![](https://img.aspxhome.com/file/2023/8/78238_0s.jpg)
Flutter 通过Clipper实现各种自定义形状的示例代码
2023-06-19 14:25:11
![](https://img.aspxhome.com/file/2023/7/59397_0s.jpg)
C#创建临时文件的方法
2023-06-16 14:32:36
C#中使用split分割字符串的几种方法小结
2023-06-18 17:23:21
formfile文件上传使用示例
2023-06-23 03:41:58
C#实现简单的JSON序列化功能代码实例
2023-06-21 09:03:52
C#启动进程的几种常用方法
2023-06-18 04:13:48
Flutter路由传递参数及解析实现
2023-06-22 11:48:45
![](https://img.aspxhome.com/file/2023/5/90155_0s.gif)
JSON.toJSONString()空字段不忽略修改的问题
2023-06-16 03:12:37
flutter ExpansionTile 层级菜单的实现
2023-06-15 16:04:01
![](https://img.aspxhome.com/file/2023/7/91007_0s.png)
Android 微信摇一摇功能实现详细介绍
2023-06-21 21:00:09
C++实现LeetCode(205.同构字符串)
2023-06-21 04:06:54
OpenCV实现直线拟合
2023-06-22 15:22:37
![](https://img.aspxhome.com/file/2023/4/92494_0s.jpg)
iOS新浪微博、腾讯微博分享功能实例
2023-06-16 09:15:53
![](https://img.aspxhome.com/file/2023/0/101960_0s.png)
Android RecyclerBarChart绘制使用教程
2023-06-19 12:18:36
![](https://img.aspxhome.com/file/2023/1/83521_0s.png)
iOS WebView中使用webp格式图片的方法
2023-06-17 22:06:03
iOS实现微信朋友圈与摇一摇功能
2023-06-16 13:59:48
![](https://img.aspxhome.com/file/2023/6/78286_0s.gif)
Android Studio中通过CMake使用NDK并编译自定义库和添加预编译库
2023-06-16 10:31:35
![](https://img.aspxhome.com/file/2023/6/82386_0s.png)
Java自定义实现链队列详解
2023-06-22 12:47:31
![](https://img.aspxhome.com/file/2023/6/57426_0s.png)
spring boot整合log4j2及MQ消费处理系统日志示例
2023-06-17 17:47:54