【问题标题】:My rectangle Animation does not enlarge from the center but from the first vertex expanding to the right我的矩形动画不是从中心放大而是从第一个顶点向右扩展
【发布时间】:2020-10-01 21:34:49
【问题描述】:

我正在尝试从从中心打开的屏幕中心创建一个方形动画(从非常小的开始并从正方形的中心开始放大,不幸的是,我的代码发生的事情是给出动画作为CustomPaint中矩形的参数作为Size(transitionTween.value, transitionTween.value)动画没有增长from中心向外但首先从左/上顶点开始,将矩形放大到右侧而不是从中心。我怎样才能获得使我的动画从中心开始和放大的效果

import 'package:flutter/material.dart';

class PointToCircle extends StatefulWidget {
  @override
  _PointToCircleState createState() => _PointToCircleState();
}

class _PointToCircleState extends State<PointToCircle>
    with TickerProviderStateMixin {
  AnimationController _controller;
  Animation<double> transitionTween;
  // Animation<BorderRadius> borderRadius;

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

    _controller = AnimationController(
        duration: const Duration(milliseconds: 2000), vsync: this);
    
    transitionTween = Tween<double>(
      begin: 1.0,
      end: 200.0,
    ).animate(
      CurvedAnimation(
        parent: _controller,
        curve: Curves.easeIn,
      ),
    );

    _controller.forward();
  }

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

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: _controller,
      builder: (BuildContext context, Widget child) {
        return Scaffold(
            body: new Center(
                child: CustomPaint(
          painter: OpenPainter(transitionTween),
        )));
      },
    );
  }
}

class OpenPainter extends CustomPainter {
  Animation<double> transitionTween;
  OpenPainter(this.transitionTween);

  @override
  void paint(Canvas canvas, Size size) {
    var paint1 = Paint()
      ..color = Colors.blue
      ..style = PaintingStyle.stroke;

    canvas.drawRect(
        Offset(-50, -100) & Size(transitionTween.value, transitionTween.value),
        paint1);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) => true;
}

【问题讨论】:

    标签: flutter flutter-layout flutter-animation custom-painting


    【解决方案1】:

    发现解决方案是在自定义绘制中添加size:Size(transition.value,transition.value);,同时在矩形大小内保留动态补间

    但我不明白现在工作背后的原因

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-22
      • 1970-01-01
      • 1970-01-01
      • 2015-09-01
      • 2021-03-25
      • 2018-09-18
      • 1970-01-01
      相关资源
      最近更新 更多