gpt4 book ai didi

flutter - 如何创建这个进度指示器?特别是我不知道如何制作Flow动画

转载 作者:行者123 更新时间:2023-12-05 04:40:56 26 4
gpt4 key购买 nike

enter image description here

我想知道如何制作流动动画。有人知道吗?

最佳答案

我只尝试了我没有成功的小部件。所以我选择定制油漆。看看我的代码。

import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class MyAnimatedLoading extends StatefulWidget {
final Offset offsetSpeed;
final List<MaterialColor> colors;
final double width;
final double height;

const MyAnimatedLoading(
{Key? key,
required this.offsetSpeed,
required this.colors,
required this.width,
required this.height})
: super(key: key);

@override
State<MyAnimatedLoading> createState() => _MyAnimatedLoadingState();
}

class _MyAnimatedLoadingState extends State<MyAnimatedLoading> {
late List<Node> nodes;
late double width;

@override
void initState() {
super.initState();
width = widget.width / (widget.colors.length);

nodes = List.generate(widget.colors.length, (index) {
return Node(
rect: Rect.fromCenter(
center: Offset(index * width + width / 2, widget.height / 2),
width: width,
height: widget.height),
color: widget.colors[index],
);
});

List<Node> tempNodes = <Node>[];
for (int i = -widget.colors.length; i <= -1; i++) {
tempNodes.add(Node(
rect: Rect.fromCenter(
center: Offset(i * width + width / 2, widget.height / 2),
width: width,
height: widget.height),
color: widget.colors.first,
));
}

for (int i = 0; i < tempNodes.length; i++) {
tempNodes[i].color = widget.colors[i];
}

nodes.addAll(tempNodes);

Timer.periodic(const Duration(milliseconds: 20), (timer) {
if (mounted) {
setState(() {});
}
});
}

@override
Widget build(BuildContext context) {
_calculateNewPositions();
return ClipRRect(
clipBehavior: Clip.hardEdge,
borderRadius: const BorderRadius.all(Radius.circular(25)),
child: CustomPaint(
size: Size(widget.width, widget.height),
painter: MyCustomPaint(nodes: nodes),
),
);
}

void _calculateNewPositions() {
for (final node in nodes) {
final offset = node.rect.center;

if (offset.dx - width / 2 >= widget.width) {
node.rect = Rect.fromCenter(
center: Offset(
(-width / 2) * (widget.colors.length * 2) + width / 2,
widget.height / 2) +
widget.offsetSpeed,
width: width,
height: widget.height);
} else {
node.rect = Rect.fromCenter(
center: offset + widget.offsetSpeed,
width: width,
height: widget.height);
}
}
}
}

class Node {
Rect rect;
Color color;

Node({required this.rect, required this.color});

@override
String toString() {
return 'Node{rect: $rect, color: $color}\n';
}
}

class MyCustomPaint extends CustomPainter {
List<Node> nodes;

MyCustomPaint({required this.nodes});

@override
void paint(Canvas canvas, Size size) {
for (int i = 0; i < nodes.length; i++) {
canvas.drawRect(nodes[i].rect, Paint()..color = nodes[i].color);
}
}

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


这不是完全相同的动画,但它是一个很好的起点

const MyAnimatedLoading(
offsetSpeed: Offset(1, 0),
width: 220,
height: 20,
colors: [
Colors.yellow,
Colors.deepOrange,
Colors.red,
Colors.blue,
Colors.green,
],
)

关于flutter - 如何创建这个进度指示器?特别是我不知道如何制作Flow动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70174219/

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