下面给你一套“开箱即用”的代码与思路:只要把 main.dart 替换成下面这段代码(或复制到自己的项目里),flutter run 就能看到卡片在点击时“翻转 180°”的动画效果。🌟
import 'dart:math';
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: const FlipCardDemo(),
);
}
}
class FlipCardDemo extends StatelessWidget {
const FlipCardDemo({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Flip Card Demo')),
body: const Center(
child: FlipCard(
width: 220,
height: 300,
front: CardFace(
color: Colors.blue,
label: 'Front',
icon: Icons.face,
),
back: CardFace(
color: Colors.orange,
label: 'Back',
icon: Icons.tag_faces,
),
),
),
);
}
}
/// 可复用的“翻转卡片”组件
class FlipCard extends StatefulWidget {
const FlipCard({
super.key,
required this.front,
required this.back,
this.duration = const Duration(milliseconds: 600),
this.width = 200,
this.height = 300,
});
final Widget front;
final Widget back;
final Duration duration;
final double width;
final double height;
@override
State<FlipCard> createState() => _FlipCardState();
}
class _FlipCardState extends State<FlipCard> with SingleTickerProviderStateMixin {
late final AnimationController _controller;
late final Animation<double> _anim; // 0 ~ 1
bool _isFront = true; // 方便判定当前面
@override
void initState() {
super.initState();
_controller = AnimationController(vsync: this, duration: widget.duration);
_anim = Tween<double>(begin: 0, end: 1).animate(
CurvedAnimation(parent: _controller, curve: Curves.easeInOut),
);
// 动画结束时翻转面(正/反)
_controller.addStatusListener((status) {
if (status == AnimationStatus.completed ||
status == AnimationStatus.dismissed) {
setState(() => _isFront = !_isFront);
}
});
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
void _flip() {
// 正面 -> 反面:正转;反面 -> 正面:反转
if (_isFront) {
_controller.forward(from: 0);
} else {
_controller.reverse(from: 1);
}
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: _flip,
child: SizedBox(
width: widget.width,
height: widget.height,
child: AnimatedBuilder(
animation: _anim,
builder: (context, child) {
// 0~0.5 显示 front,0.5~1 显示 back
final angle = _anim.value * pi; // 0 -> 180°
final isUnder = angle > pi / 2;
final display = isUnder ? widget.back : widget.front;
// 让反面对齐正常(否则是倒着的 ↓)
final transform = Matrix4.identity()
..setEntry(3, 2, 0.001) // 透视
..rotateY(angle);
if (isUnder) {
// 反面要额外旋转 180°,让文字不倒
transform.rotateY(pi);
}
return Transform(
transform: transform,
alignment: Alignment.center,
child: display,
);
},
),
),
);
}
}
/// 漂亮点的卡片“面子”
class CardFace extends StatelessWidget {
const CardFace({
super.key,
required this.color,
required this.label,
required this.icon,
});
final Color color;
final String label;
final IconData icon;
@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(16),
color: color,
boxShadow: const [
BoxShadow(color: Colors.black26, blurRadius: 6, offset: Offset(0, 3)),
],
),
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Icon(icon, size: 80, color: Colors.white),
const SizedBox(height: 12),
Text(
label,
style: const TextStyle(
fontSize: 28,
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
],
),
),
);
}
}
核心思路拆解
- AnimationController + Tween:
- 用
_controller 控制 0~1 的补间值 _anim;进度 × π(180°)就是旋转角度。
- Matrix4 与 3D 透视:
setEntry(3, 2, 0.001) 打开“3D 透视”,否则看起来像 2D 翻转。
rotateY(angle) 让卡片沿 Y 轴旋转(正面 → 反面)。
- 正反面切换:
- 在 0
0.5 区间显示正面;0.51 显示反面。
- 反面对齐角度后要再
rotateY(pi) 让文字方向正常。
- 加一个手势
onTap 调 forward / reverse,每次点击就正反互换。
Tips:
• 想要两张不同大小或不同圆角的卡片?直接换掉 CardFace,或者自己写两个完全不同的 Widget 传进去都行。
• 如果想让多张卡片在列表里独立翻转,只需在 ListView.builder 内部给每个 FlipCard 分配一个 key 即可。
• 动画速度可改 duration,曲线多试试 Curves.elasticInOut、Curves.fastOutSlowIn … 玩出不同感觉。
Enjoy Coding & Happy Flipping! 🪄