颤振:重建/热重装时,FadeTransition停止

时间:2019-03-21 11:32:48

标签: animation dart flutter transition fade

关于颤动中的动画,我有些无法理解。当我运行以下代码时,按照预期,我会看到红色屏幕反复淡入。但是,如果我在过渡过程中进行热重装,有时似乎会停止,有时不会。我尝试了几次,最终将停止。为什么会这样?

import "package:flutter/material.dart";

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: FadingLoop(),
    );
  }
}

class FadingLoop extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return FadingLoopState();
  }
}

class FadingLoopState extends State<FadingLoop> with SingleTickerProviderStateMixin {
  AnimationController _controller;
  Animation _animation;

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

    _controller = AnimationController(
      vsync: this,
      duration: Duration(milliseconds: 1000),
    );
    _animation = Tween(begin: 0.0, end: 1.0).animate(_controller);

    _controller.repeat();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: FadeTransition(
        opacity: _animation,
        child: Container(
          color: Colors.red,
        ),
      ),
    );
  }

  @override
  void dispose() {
    _controller.dispose();

    super.dispose();
  }
}

0 个答案:

没有答案