Flutter中的尺度过渡-Loader动画

HaS*_*Tai 5 animation dart flutter flutter-layout flutter-animation

我制作了一个具有比例过渡的容器,它从 0 高度和宽度增长到 90 高度和宽度。

现在,我想做的是它应该随着它的增长而慢慢淡出。我需要为不透明度创建另一个动画控制器吗?最好的方法是什么?有人可以帮忙吗?

我的代码看起来像这样

import 'package:flutter/animation.dart';
import 'package:flutter/material.dart';

void main() => runApp(new MyAnimationApp());

class MyAnimationApp extends StatefulWidget {
  @override
  _MyAnimationAppState createState() => _MyAnimationAppState();
}

class _MyAnimationAppState extends State<MyAnimationApp>
    with TickerProviderStateMixin {
  Animation<double> animation;
  AnimationController _controller;

  @override
  void initState() {
    super.initState();

    _controller =
        new AnimationController(vsync: this, duration: Duration(seconds: 3))
          ..repeat();
    animation = new CurvedAnimation(parent: _controller, curve: Curves.linear);
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        body: new Container(
          child: new Center(
            child: new ScaleTransition(
              scale: animation,
              child: new Container(
                decoration: new BoxDecoration(
                    color: Color(0XFFEC3457), shape: BoxShape.circle),
                height: 90.0,
                width: 90.0,
              ),
            ),
          ),
        ),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

SS来了

在此输入图像描述 在此输入图像描述

谢谢 !!!希望得到答复......


Sin*_*afi 5

您需要在代码中添加一个双精度值:

double _opacity = 1;
Run Code Online (Sandbox Code Playgroud)

以及 initState 末尾动画控制器的侦听器

_controller.addListener(() {
  setState(() {
    _opacity = 1 - _controller.value;
  });
});
Run Code Online (Sandbox Code Playgroud)

在您的小部件树上,您可以添加以下内容:

ScaleTransition(
  scale: animation,
  child: Opacity(
    opacity: _opacity,
    child: Container(
  
Run Code Online (Sandbox Code Playgroud)