flutter - 在Flutter中实现卡片翻转动画

标签 flutter dart flutter-animation

This is the container flipping from top
我正在尝试卡片翻转动画,但不是从中心翻转。它从容器的顶部翻转。
我正在尝试卡片翻转动画,但不是从中心翻转。它从容器的顶部翻转。

class _Container1State extends State<Container1>
    with SingleTickerProviderStateMixin {
  Animation animation;
  AnimationController animationController;
  @override
  void initState() {
    animationController =
        AnimationController(duration: Duration(seconds: 1), vsync: this);
    animation = Tween(begin: 0.0, end: 1.0).animate(
      CurvedAnimation(curve: Curves.linear, parent: animationController),
    );
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    animationController.forward();
    return Scaffold(
        body: AnimatedBuilder(
            animation: animationController,
            builder: (BuildContext context, Widget child) {
              return Center(
                child: Container(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      GestureDetector(
                        onTap: () {
                          animationController.repeat();
                        },
                        child: Container(
                          transform: Matrix4.identity()
                            ..rotateX(2 * 3.14 * animation.value),
                          height: 150,
                          width: 150,
                          color: Colors.yellow,
                          child: Center(child: Text('Text 1')),
                        ),
                      ),
                    ],
                  ),
                ),
              );
            }));
  }
}

最佳答案

更新
enter image description here
这是我简单的FlipViewWidget

class FlipView extends StatefulWidget {
  final Widget front, back;

  FlipView({Key key, @required this.front, @required this.back}) : super(key: key);

  @override
  _FlipViewState createState() => _FlipViewState();
}

class _FlipViewState extends State<FlipView> with SingleTickerProviderStateMixin {
  Animation _animation;
  AnimationController _controller;

  @override
  void initState() {
    _controller = AnimationController(duration: Duration(milliseconds: 400), vsync: this);
    _animation = Tween(begin: 0.0, end: 1.0).animate(_controller);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: _controller,
      builder: (BuildContext context, Widget child) {
        bool isFront = _controller.value < .5;
        return InkWell(
          onTap: () {
            if (_animation.isDismissed) {
              _controller.forward();
            } else if (_animation.isCompleted) {
              _controller.reverse();
            }
          },
          child: Transform(
            transform: Matrix4.identity()
              ..setEntry(3, 2, 0.002)
              ..rotateX(pi * _animation.value + (isFront ? 0 : pi)),
            alignment: FractionalOffset.center,
            child: isFront ? widget.front : widget.back,
          ),
        );
      },
    );
  }
}
用法
class FlipViewTest extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Container(
          width: 200,
          height: 100,
          child: FlipView(
            front: Container(
              color: Colors.red,
              alignment: Alignment.center,
              child: Text('Front', style: TextStyle(fontSize: 25, color: Colors.white)),
            ),
            back: Container(
              color: Colors.green,
              alignment: Alignment.center,
              child: Text('Back', style: TextStyle(fontSize: 25, color: Colors.white)),
            ),
          ),
        ),
      ),
    );
  }
}
OLD
设置容器内的变换小部件。而且您可以轻松更改对齐方式。
  @override
  Widget build(BuildContext context) {
    animationController.forward();
    return Scaffold(
      body: AnimatedBuilder(
        animation: animationController,
        builder: (BuildContext context, Widget child) {
          return Center(
            child: Transform(
              transform: Matrix4.identity()
                ..rotateX(2 * 3.14 * animation.value),
              alignment: FractionalOffset.center,
              child: GestureDetector(
                onTap: () {
                  animationController.repeat();
                },
                child: Container(
                  height: 150,
                  width: 150,
                  color: Colors.yellow,
                  child: Center(child: Text('Text 1')),
                ),
              ),
            ),
          );
        },
      ),
    );
  }

关于flutter - 在Flutter中实现卡片翻转动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63296789/

相关文章:

flutter - 我们如何在 flutter 中实现这样的东西?

flutter - 如果我使用过多的 "const"关键字,会有什么不同吗?

android-studio - 如何修复Android Studio中的Gradle错误?

flutter - 将Map <String,dynamic>数据转换为类数据

image - Flutter CircleAvatar-将Image对象用作(背景)图像/将其类型转换为ImageProvider

flutter - 使用 Flutter 加载屏幕时如何淡入小部件?

flutter - 单击子小部件中存在的按钮时,如何更改父小部件的动画?

dart - 如何修复 future<int> 类型的值不能分配给 int 类型的变量

css - 如何调整输入的长度

flutter - Flutter:我可以在其中添加Text()的地方