dart - Flutter - 翻牌效果

标签 dart flutter

我正在尝试制作翻转卡,获得效果的最佳方法是什么

flip card

最佳答案

我会使用 AnimatedBuilderAnimatedWidgetTransform 小部件的值设置动画。 ScaleTransition几乎可以为您做到这一点,但它可以双向扩展,您只需要一个。

import 'package:flutter/material.dart';

void main() {
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePageState createState() => new MyHomePageState();
}

class MyCustomCard extends StatelessWidget {
  MyCustomCard({ this.colors });

  final MaterialColor colors;

  Widget build(BuildContext context) {
    return new Container(
      alignment: FractionalOffset.center,
      height: 144.0,
      width: 360.0,
      decoration: new BoxDecoration(
        color: colors.shade50,
        border: new Border.all(color: new Color(0xFF9E9E9E)),
      ),
      child: new FlutterLogo(size: 100.0, colors: colors),
    );
  }
}

class MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
  AnimationController _controller;
  Animation<double> _frontScale;
  Animation<double> _backScale;

  @override
  void initState() {
    super.initState();
    _controller = new AnimationController(
      vsync: this,
      duration: const Duration(seconds: 1),
    );
    _frontScale = new Tween(
      begin: 1.0,
      end: 0.0,
    ).animate(new CurvedAnimation(
      parent: _controller,
      curve: new Interval(0.0, 0.5, curve: Curves.easeIn),
    ));
    _backScale = new CurvedAnimation(
      parent: _controller,
      curve: new Interval(0.5, 1.0, curve: Curves.easeOut),
    );
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    ThemeData theme = Theme.of(context);

    return new Scaffold(
      appBar: new AppBar(),
      floatingActionButton: new FloatingActionButton(
        child: new Icon(Icons.flip_to_back),
        onPressed: () {
          setState(() {
            if (_controller.isCompleted || _controller.velocity > 0)
              _controller.reverse();
            else
              _controller.forward();
          });
        },
      ),
      body: new Center(
        child: new Stack(
          children: <Widget>[
            new AnimatedBuilder(
              child: new MyCustomCard(colors: Colors.orange),
              animation: _backScale,
              builder: (BuildContext context, Widget child) {
                final Matrix4 transform = new Matrix4.identity()
                  ..scale(1.0, _backScale.value, 1.0);
                return new Transform(
                  transform: transform,
                  alignment: FractionalOffset.center,
                  child: child,
                );
              },
            ),
            new AnimatedBuilder(
              child: new MyCustomCard(colors: Colors.blue),
              animation: _frontScale,
              builder: (BuildContext context, Widget child) {
                final Matrix4 transform = new Matrix4.identity()
                  ..scale(1.0, _frontScale.value, 1.0);
                return new Transform(
                  transform: transform,
                  alignment: FractionalOffset.center,
                  child: child,
                );
              },
            ),
          ],
        ),
      ),
    );
  }
}

关于dart - Flutter - 翻牌效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44990714/

相关文章:

java - Flutter 不是只创建 Java 类,而是 Kotlin

ios - Flutter(iOS) -"No CupertinoLocalizations found",如何解决?

angularjs - Dart web-webui vs angularjs

flutter - 如何在后台服务中获取经纬度?

flutter - 如何在 flutter 中按键隐藏或显示小部件?

flutter - 因为 sdk 中的 flutter_driver 的每个版本都依赖于 crypto 2.1.5 而 Cruise 依赖于 crypto 3.0.0,所以 sdk 中的 flutter_driver 是被禁止的

Flutter/Dart - 将逗号分隔的字符串拆分为 3 个变量?

flutter - 如何解决flutter中的权限处理程序错误?

flutter - 如何将小部件放置在可调整大小页面的特定位置? [ flutter ]

flutter - 什么是NoSuchMethod错误,如何解决?