gpt4 book ai didi

dart - Flutter - 我正在寻找一种制作脉冲动画的方法

转载 作者:IT老高 更新时间:2023-10-28 12:35:01 36 4
gpt4 key购买 nike

我试图在 flutter 中实现这样的事情 enter image description here

最佳答案

一种方法是使用 CustomPainter 和动画。另请查看 SpriteWidget .

import 'dart:math';

import 'package:flutter/material.dart';

class SpritePainter extends CustomPainter {
final Animation<double> _animation;

SpritePainter(this._animation) : super(repaint: _animation);

void circle(Canvas canvas, Rect rect, double value) {
double opacity = (1.0 - (value / 4.0)).clamp(0.0, 1.0);
Color color = Color.fromRGBO(0, 117, 194, opacity);

double size = rect.width / 2;
double area = size * size;
double radius = sqrt(area * value / 4);

final Paint paint = Paint()..color = color;
canvas.drawCircle(rect.center, radius, paint);
}

@override
void paint(Canvas canvas, Size size) {
Rect rect = Rect.fromLTRB(0.0, 0.0, size.width, size.height);

for (int wave = 3; wave >= 0; wave--) {
circle(canvas, rect, wave + _animation.value);
}
}

@override
bool shouldRepaint(SpritePainter oldDelegate) {
return true;
}
}

class SpriteDemo extends StatefulWidget {
@override
SpriteDemoState createState() => SpriteDemoState();
}

class SpriteDemoState extends State<SpriteDemo>
with SingleTickerProviderStateMixin {
late final AnimationController _controller;

@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
);
//_startAnimation();
}

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

void _startAnimation() {
_controller
..stop()
..reset()
..repeat(period: const Duration(seconds: 1));
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Pulse')),
body: CustomPaint(
painter: SpritePainter(_controller),
child: SizedBox(
width: 200.0,
height: 200.0,
),
),
floatingActionButton: FloatingActionButton(
onPressed: _startAnimation,
child: new Icon(Icons.play_arrow),
),
);
}
}

void main() {
runApp(
MaterialApp(
home: SpriteDemo(),
),
);
}

关于dart - Flutter - 我正在寻找一种制作脉冲动画的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50739048/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com