【问题标题】:AnimatedContainer is breaking other animationAnimatedContainer 正在破坏其他动画
【发布时间】:2020-05-29 00:37:47
【问题描述】:

当用户按下容器时,我正在使用动画来旋转渐变,我想增加容器的大小。

当我使用容器而不是 AnimatedContainer 渐变动画按预期工作时,但是当我用 AnimatedContainer 替换 Container 时,渐变动画无法正常工作(意思是:在旋转 0 到 2*math.pi 后它再次重置然后开始动画,我希望它不断旋转。)

import 'package:flutter/material.dart';
import 'dart:math' as math;

class Delete extends StatefulWidget {
  Delete({Key key}) : super(key: key);

  @override
  _DeleteState createState() => _DeleteState();
}

class _DeleteState extends State<Delete> with SingleTickerProviderStateMixin {
  Animation<double> _animation;
  AnimationController _controller;
  double _playButtonSize = 170;

  @override
  void initState() {
    super.initState();
    _controller =
        AnimationController(vsync: this, duration: Duration(seconds: 10));
    _animation = Tween<double>(begin: 0, end: 2 * math.pi).animate(_controller)
      ..addListener(() {
        setState(() {});
      });
    _controller.repeat();
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(""),
        ),
        body: Container(
          child: GestureDetector(
            onLongPress: () {
              setState(() {
                _playButtonSize = 185;
              });
            },
            onLongPressEnd: (end) {
              setState(() {
                _playButtonSize = 170;
              });
            },
            child: AnimatedContainer(
                duration: Duration(milliseconds: 100),
                width: _playButtonSize,
                height: _playButtonSize,
                padding: EdgeInsets.all(17.0),
                decoration: BoxDecoration(
                  shape: BoxShape.circle,
                  gradient: SweepGradient(
                    colors: [
                      Colors.blue,
                      Colors.pink,
                      Colors.orange,
                      Colors.yellow,
                      Colors.blue
                    ],
                    center: Alignment(-0.50, -0.0),
                    endAngle: _animation.value + (2 * math.pi),
                    startAngle: _animation.value,
                    tileMode: TileMode.repeated,
                  ),
                  boxShadow: [
                    BoxShadow(
                      color: Colors.white,
                      blurRadius: 4.0,
                    ),
                  ],
                ),
                child: Icon(
                  Icons.email,
                  size: 50,
                )),
          ),
        ));
  }
}

【问题讨论】:

    标签: flutter flutter-layout flutter-animation


    【解决方案1】:

    如果您将 AnimatedContainer 与另一个父 Container 交换,您的两个动画都可以工作:

    return Scaffold(
      appBar: AppBar(
        title: Text(""),
      ),
      body: AnimatedContainer(
        duration: Duration(milliseconds: 100),
        width: _playButtonSize,
        height: _playButtonSize,
        child: GestureDetector(
          onLongPress: () {
            setState(() {
              _playButtonSize = 185;
            });
          },
          onLongPressEnd: (end) {
            setState(() {
              _playButtonSize = 170;
            });
          },
          child: Container(
            padding: EdgeInsets.all(17.0),
            decoration: BoxDecoration(
              shape: BoxShape.circle,
              gradient: SweepGradient(
                colors: [
                  Colors.blue,
                  Colors.pink,
                  Colors.orange,
                  Colors.yellow,
                  Colors.blue
                ],
                center: Alignment(-0.50, -0.0),
                endAngle: _animation.value + (2 * math.pi),
                startAngle: _animation.value,
                tileMode: TileMode.repeated,
              ),
              boxShadow: [
                BoxShadow(
                  color: Colors.white,
                  blurRadius: 4.0,
                ),
              ],
            ),
            child: Icon(
              Icons.email,
              size: 50,
            )
          ),
        ),
      )
    );
    

    【讨论】:

      猜你喜欢
      • 2012-11-09
      • 2018-01-09
      • 1970-01-01
      • 2016-12-02
      • 2016-11-16
      • 2018-03-19
      • 2016-12-29
      • 1970-01-01
      • 2016-02-18
      相关资源
      最近更新 更多