簡體   English   中英

無法重新啟動 `TweenAnimationBuilder` animation

[英]Cannot restart a `TweenAnimationBuilder` animation

盡管有setState() ,但無法重新啟動我的 animation 。

我的代碼如下:


class SocOptimiserProgressIndicator extends StatefulWidget {
  @override
  State<SocOptimiserProgressIndicator> createState() =>
      _SocOptimiserProgressIndicatorState();
}

class _SocOptimiserProgressIndicatorState
    extends State<SocOptimiserProgressIndicator> {
  double _start = 0;

  @override
  Widget build(BuildContext context) {
    final size = 40.0;
    return TweenAnimationBuilder<double>(
        tween: Tween(begin: _start, end: 1.0),
        duration: Duration(seconds: 4),
        onEnd: () => setState(() {
              _start = 1 - _start;
            }),
        builder: (context, value, child) {
          // percentage to show in Center Text
          int percentage = (value * 100).ceil();
         
          // blah blah

您可以這樣做(也請注意密鑰,這可能是您的小部件未重建的原因):

class MyWidget extends StatefulWidget {
  const MyWidget({Key? key}) : super(key: key);

  @override
  State<MyWidget> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  double _start = 0;
  double _end = 1;

  @override
  Widget build(BuildContext context) {
    return TweenAnimationBuilder<double>(
      tween: Tween(begin: _start, end: _end),
      duration: const Duration(milliseconds: 2000),
      curve: Curves.linear,
      builder: (ctx, value, _) {
        print('value: $value');
        return FlutterLogo(
          key: ValueKey(value),
          size: 100 * value,
        );
      },
      onEnd: () {
        // restart the animation
        setState(() {
          var tmp = _start;
          _start = _end;
          _end = tmp;
        });
      },
    );
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM