Flutter中的多个补间动画问题-包含视频

时间:2018-11-18 19:51:21

标签: dart flutter flutter-layout flutter-animation

我要创建的某些动画出现了奇怪的抖动问题。

我正在尝试在屏幕上制作图像动画。 我希望它在x轴上移动,也希望它逐渐淡入。

所以我想出了-PositionedOpacity,并用补间动画它们的值。

PositionedOpacity小部件都可以很好地发挥作用,但是当我将两者结合时-我得到了一个怪异的动画,它只会在一段时间(约3秒)后才开始绘制。

我尝试打印animation.value,但似乎很好,从0.0慢慢爬到1.0-但是只有三秒钟后云才突然出现。

我试图将它们分离到不同的控制器,以为这可能是罪魁祸首,但是没有。

TLDR视频:

Opacity Animation Only

Positioned Animation Only

Both Animations Combined - NOT GOOD

这是小部件的代码:

import 'package:app/weather/widgets/CloudProperties.dart';
import 'package:flutter/widgets.dart';

class WeatherCloudWidget extends StatefulWidget {
  final double sunSize;
  final CloudProperties properties;

  WeatherCloudWidget({Key key, this.properties, this.sunSize})
      : super(key: key);

  @override
  State<StatefulWidget> createState() => _WeatherCloudWidget();
}

class _WeatherCloudWidget extends State<WeatherCloudWidget>
    with TickerProviderStateMixin {
  AnimationController controller;
  AnimationController controller2;

  Animation<double> position;
  Animation<double> opacity;

  @override
  initState() {
    super.initState();
    _startAnimation();
  }

  @override
  Widget build(BuildContext context) {
    // screen width and height
    final screenWidth = MediaQuery.of(context).size.width;
    final screenHeight = MediaQuery.of(context).size.height;

    final properties = widget.properties;

    var vertical =
        screenHeight * 0.5 + (widget.sunSize * properties.verticalOffset * -1);

    var horizontal = (screenWidth * 0.5) + (widget.sunSize * position.value);

    print(opacity.value);

    // both Positioned & Opacity widgets

    return Positioned(
        left: horizontal,
        top: vertical,
        child: Opacity(
          opacity: opacity.value,
          child: Image.asset(
            properties.path,
            width: properties.getScaledWidth(widget.sunSize),
            height: properties.getScaledHeight(widget.sunSize),
          ),
        ));

    // Positioned only

    return Positioned(
        left: horizontal,
        top: vertical,
        child: Image.asset(
          properties.path,
          width: properties.getScaledWidth(widget.sunSize),
          height: properties.getScaledHeight(widget.sunSize),
        ));

    // Opacity only

    return Positioned(
        left: (screenWidth * 0.5) + (widget.sunSize * properties.tween[1]),
        top: vertical,
        child: Opacity(
          opacity: opacity.value,
          child: Image.asset(
            properties.path,
            width: properties.getScaledWidth(widget.sunSize),
            height: properties.getScaledHeight(widget.sunSize),
          ),
        ));
  }

  @override
  dispose() {
    controller.dispose();
    controller2.dispose();
    super.dispose();
  }

  void _startAnimation() {
    controller = AnimationController(
        duration: const Duration(milliseconds: 5000), vsync: this);

    controller2 = AnimationController(
        duration: const Duration(milliseconds: 5000), vsync: this);

    position = Tween(
            begin: widget.properties.tween[0], end: widget.properties.tween[1])
        .animate(
            new CurvedAnimation(parent: controller, curve: Curves.decelerate))
          ..addListener(() => setState(() {}));

    opacity = Tween(begin: 0.0, end: 1.0).animate(controller2)
      ..addListener(() => setState(() {}));

    controller.forward();
    controller2.forward();
  }
}

1 个答案:

答案 0 :(得分:1)

好的。我设法使用SlideTransitionFadeTransition对此进行了排序。 我猜我们应该只将Transition小部件用于...转换吗?而PositionedOpacity之类的东西是用于更多静态小部件的?不确定...

看起来像什么:https://youtu.be/hj7PkjXrgfg

无论如何,如果有人在寻找参考,这是替换代码:

class WeatherCloudWidget extends StatefulWidget {
  final double sunSize;
  final CloudProperties properties;

  WeatherCloudWidget({Key key, this.properties, this.sunSize})
      : super(key: key);

  @override
  State<StatefulWidget> createState() => _WeatherCloudWidget();
}

class _WeatherCloudWidget extends State<WeatherCloudWidget>
    with TickerProviderStateMixin {
  AnimationController controller;
  Animation<Offset> position;
  Animation<double> opacity;

  final alphaTween = new Tween(begin: 0.0, end: 1.0);

  @override
  initState() {
    super.initState();
    _startAnimation();
  }

  @override
  Widget build(BuildContext context) {
    // screen width and height
    final screenWidth = MediaQuery.of(context).size.width;
    final screenHeight = MediaQuery.of(context).size.height;

    final properties = widget.properties;

    var vertical = (screenHeight * 0.5) +
        (widget.sunSize * properties.verticalOffset * -1);

    var horizontal =
        (screenWidth * 0.5) + (widget.sunSize * properties.tweenEnd);

    return Positioned(
      left: horizontal,
      top: vertical,
      child: SlideTransition(
          position: position,
          child: FadeTransition(
            opacity: opacity,
            child: Image.asset(
              properties.path,
              width: properties.getScaledWidth(widget.sunSize),
              height: properties.getScaledHeight(widget.sunSize),
            ),
          )),
    );
  }

  @override
  dispose() {
    controller.dispose();
    super.dispose();
  }

  void _startAnimation() {
    controller = AnimationController(
        duration: const Duration(milliseconds: 2000), vsync: this);

    position = new Tween<Offset>(
      begin: new Offset(widget.properties.tweenStart, 0.0),
      end: new Offset(0.0, 0.0),
    ).animate(new CurvedAnimation(parent: controller, curve: Curves.decelerate));

    opacity = alphaTween.animate(controller);

    controller.forward();
  }
}