Flutter - Effect of flip card

Viewed 10981

I'm trying to make a flip card, what would be the best way to get the effect

flip card

3 Answers

enter image description here

I used simple approach, rotated it on X axis. Here is the full code.

void main() => runApp(MaterialApp(home: HomePage()));

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with SingleTickerProviderStateMixin {
  AnimationController _controller;
  bool _flag = true;
  Color _color = Colors.blue;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(vsync: this, duration: Duration(seconds: 1), value: 1);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.crop_rotate),
        onPressed: () async {
          if (_flag) {
            await _controller.reverse();
            setState(() {
              _color = Colors.orange;
            });
            await _controller.forward();
          } else {
            await _controller.reverse();
            setState(() {
              _color = Colors.blue;
            });
            await _controller.forward();
          }

          _flag = !_flag;
        },
      ),
      body: Center(
        child: AnimatedBuilder(
          animation: _controller,
          builder: (context, child) {
            return Transform(
              transform: Matrix4.rotationX((1 - _controller.value) * math.pi / 2),
              alignment: Alignment.center,
              child: Container(
                height: 100,
                margin: EdgeInsets.symmetric(horizontal: 20),
                padding: EdgeInsets.symmetric(vertical: 12),
                alignment: Alignment.center,
                decoration: BoxDecoration(color: _color.withOpacity(0.2), border: Border.all(color: Colors.grey)),
                child: FlutterLogo(colors: _color, size: double.maxFinite),
              ),
            );
          },
        ),
      ),
    );
  }
}

You can use the flip_card Flutter package. It lets you define a front and back widget and can be flipped horizontally or vertically.

Related