• 赚钱入口【需求资源】限时招募流量主、渠道主,站长合作;【合作模式】CPS长期分成,一次推广永久有收益。主动打款,不扣量;

Flutter 翻转效果

Flutter cps12345 5个月前 (12-02) 109次浏览 0个评论
问题:

Flutter 如何实现翻转效果?

解决方法:

使用AnimationController+Transform 指定旋转来实现

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),
              ),
            );
          },
        ),
      ),
    );
  }
}

效果如下:

Flutter 翻转效果

喜欢 (0)

您必须 登录 才能发表评论!