Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dart 如何使用动画容器进行动画变换(例如缩放)_Dart_Flutter - Fatal编程技术网

Dart 如何使用动画容器进行动画变换(例如缩放)

Dart 如何使用动画容器进行动画变换(例如缩放),dart,flutter,Dart,Flutter,我希望使用容器的属性设置容器比例的动画;但是,刻度没有转换,而是直接从起点跳到终点 代码段: var container = new AnimatedContainer( duration: const Duration(milliseconds: 200), width: 50.0, height: 50.0, // selected is a bool that will be toggled transform: selected ? new Matrix4.ident

我希望使用容器的属性设置容器比例的动画;但是,刻度没有转换,而是直接从起点跳到终点

代码段:

var container = new AnimatedContainer(
  duration: const Duration(milliseconds: 200),
  width: 50.0,
  height: 50.0,
  // selected is a bool that will be toggled
  transform: selected ? new Matrix4.identity() : new Matrix4.identity().scaled(0.2,0.2),
  decoration: new BoxDecoration(
    shape: BoxShape.circle,
    backgroundColor: Colors.blue[500],
  ),
  child: new Center(
    child: new Icon(
      Icons.check,
      color: Colors.white,
    ),
  )
);

对正在发生的事情有什么见解吗?

我恐怕
转换
是我们不设置动画的属性之一(
子属性是另一个属性)。如果要设置比例动画,可以使用ScaleTransition

缩放转换:


矩阵lerp的错误:

您可以使用动画生成器制作动画,下面的代码将在4秒内缩放字体大小为20-35的文本让我将其分解为多个步骤,以便您更好地理解

1.您需要从TickerProviderStateMixin实现类

2.您需要一个动画控制器和一个动画变量

3.将小部件包装在AnimatedBuilder中(注意AnimatedBuilder必须至少返回一个容器),并将控制器添加到动画中

animation: _controller,
和返回动画小部件的生成器

4.在init方法中,使用vsync和动画持续时间初始化控制器。 带有Tweenit的动画采用开始值和结束值,这两个值定义了要设置动画的小部件的初始值和最终值

对我来说,在本例中,它是文本小部件,因此开始值和结束值将与fontSize.com相对应,fontSize.com作为
animation.value

5.最后,您希望动画效果如何将由接受控制器和曲线效果的动画指定

下面是一个工作示例

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<SplashScreen>
    with TickerProviderStateMixin {

  AnimationController _controller;
  Animation _animation;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Color(0xFF005CA9),
        body: Center(
          child: AnimatedBuilder(
            animation: _controller,
            builder: (BuildContext context, Widget child) {
              return Container(
                child: Text(
                  'Hello World',
                  style: TextStyle(
                      color: Colors.white,
                      fontSize: _animation.value,
                      ),
                ),
              );
            },
          ),
        ));
  }

  void initState() {
    super.initState();
    _controller =
        AnimationController(vsync: this, duration: Duration(seconds: 4));
    _animation = Tween<double>(
      begin: 20,
      end: 35,
    ).animate(
      CurvedAnimation(
        parent: _controller,
        curve: Curves.ease,
      ),
    );
    _controller.forward();
  }
}
类MyApp扩展StatefulWidget{
@凌驾
_MyAppState createState()=>\u MyAppState();
}
类MyAppState扩展了状态
使用TickerProviderStateMixin{
动画控制器_控制器;
动画(动画),;
@凌驾
小部件构建(构建上下文){
返回脚手架(
背景颜色:颜色(0xFF005CA9),
正文:中(
子对象:动画生成器(
动画:_控制器,
生成器:(BuildContext上下文,小部件子项){
返回容器(
子:文本(
“你好,世界”,
样式:TextStyle(
颜色:颜色,白色,
fontSize:_animation.value,
),
),
);
},
),
));
}
void initState(){
super.initState();
_控制器=
AnimationController(vsync:this,duration:duration(秒数:4));
_动画=吐温(
开始:20,
完:35,,
).制作动画(
曲线化(
父节点:_控制器,
曲线:曲线,
),
);
_controller.forward();
}
}
上面的代码生成以下输出


AnimatedContainer支持动画化其变换值,如下所示:

/// scale to 95%, centerred
final width = 200.0;
final height = 300.0;
bool shouldScaleDown = true;// change value when needed
AnimatedContainer(
  color: Colors.blueAccent,
  width: width,
  height: height,
  duration: Duration(milliseconds: 100),
  transform: (shouldScaleDown
      ? (Matrix4.identity()
        ..translate(0.025 * width, 0.025 * height)// translate towards right and down
        ..scale(0.95, 0.95))// scale with to 95% anchorred at topleft of the AnimatedContainer
      : Matrix4.identity()),
  child: Container(),
);

我假设您现在确实支持使用动画容器进行转换,因为它在您的每周小部件视频中这样说:不确定是如何完成的。啊,显然我们支持它,但只支持翻译,因此出现了上述问题(使用缩放)。