使用AnimatedContainer小部件的transorm属性

问题描述 投票:1回答:1

我正在尝试使用动画容器的'transform'属性,但是,我找不到使AnimatedContainer围绕中心X轴旋转的方法,如何设置Matrix4使其旋转翻转的方法中心?

child: AnimatedContainer(
        color: widget.activated ? widget.color : Colors.transparent,
        duration: Duration(seconds: 1),
        curve: Curves.linear,
        transform: _createRotation(widget.activated),
      ),
flutter flutter-animation
1个回答
1
投票

您可以使用软件包https://pub.dev/packages/matrix4_transform

代码段

  AnimatedContainer(
          duration: Duration(seconds: 1),
          curve: Curves.linear,
          color: clicked ? Colors.red : Colors.blue,
          width: width,
          height: height,
          transform: Matrix4Transform()
              .rotateDegrees(clicked ? 90 : 180, origin: Offset(width / 2, height / 2))
              .matrix4,
        )

工作演示

enter image description here

完整代码

    import 'package:flutter/material.dart';
import 'package:matrix4_transform/matrix4_transform.dart';
import 'dart:math';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  double height = 100.0;
  double width = 100.0;
  Color color = Colors.red;
  BorderRadiusGeometry borderRadius = BorderRadius.circular(8);
  bool clicked = false;

  void _incrementCounter() {
    setState(() {
      clicked = !clicked;
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            AnimatedContainer(
              duration: Duration(seconds: 1),
              curve: Curves.linear,
              color: clicked ? Colors.red : Colors.blue,
              width: width,
              height: height,
              transform: Matrix4Transform()
                  .rotateDegrees(clicked ? 90 : 180, origin: Offset(width / 2, height / 2))
                  .matrix4,
            ),
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed:  _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.