AnimatedContainer ломает другую анимацию - PullRequest
1 голос
/ 13 февраля 2020

Я использую анимацию для поворота градиента одновременно, когда пользователь нажимает на контейнер, и я хочу увеличить размер контейнера.

, когда я использую контейнер вместо анимации градиента AnimatedContainer, работающей, как и ожидалось, но когда я замените контейнер на AnimatedContainer, градиентная анимация не работает должным образом (означает: после поворота от 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,
                )),
          ),
        ));
  }
}

1 Ответ

0 голосов
/ 13 февраля 2020

Если вы поменяете 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,
        )
      ),
    ),
  )
);
...