gpt4 book ai didi

flutter - 自定义形状上的动态数据

转载 作者:行者123 更新时间:2023-12-03 03:00:37 26 4
gpt4 key购买 nike

我想构建一个应用程序,在其中我的一个设计中要用动态数据填充形状。这些将是自定义形状,其中我有两种不同的形状,它们在另一种下面交替显示。所以我有一个左形状,然后下一个将是一个右形状,依此类推。是否可以在Flutter中创建它,我将如何做?
enter image description here

最佳答案

这是一种方法。我使用CustomPainter创建的自定义三角形形状简化了形状,因此您必须根据需要对其进行修改。

ListView(
children: <Widget>[
OverflowTitle(color: Colors.green),
OverflowTitle(color: Colors.blue),
OverflowTitle(color: Colors.red)
],
);

和自定义溢出标题
class OverflowTitle extends StatelessWidget {
final Color color;

const OverflowTitle({this.color});
@override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
height: 50,
child: OverflowBox(
alignment: Alignment.bottomCenter,
minHeight: 50,
maxHeight: 70,
child: Container(
height: 60,
child: CustomPaint(
painter: TrianglePainter(
strokeColor: color,
),
child: Text(
'NO1',
textAlign: TextAlign.center,
),
),
),
),
);
}
}

这是输出

enter image description here

让我知道您是否需要更多帮助...

更新
这是我自定义的三角画家
import 'package:flutter/material.dart';

enum Direction { Up, Down, Left, Right }

class TrianglePainter extends CustomPainter {
final Color strokeColor;
final Direction direction;

TrianglePainter({this.strokeColor = Colors.white, this.direction});

@override
void paint(Canvas canvas, Size size) {
Paint paint = Paint()
..color = strokeColor
..style = PaintingStyle.fill;

canvas.drawPath(getTrianglePath(size.width, size.height), paint);
}

Path getTrianglePath(double x, double y) {
if (direction == Direction.Right) {
return Path()
..moveTo(0, y)
..lineTo(x, y / 2)
..lineTo(0, 0)
..lineTo(0, y);
} else if (direction == Direction.Left) {
return Path()
..moveTo(x, 0)
..lineTo(0, y / 2)
..lineTo(x, y)
..lineTo(x, 0);
} else if (direction == Direction.Down) {
return Path()
..moveTo(0, 0)
..lineTo(x / 2, y)
..lineTo(x, 0)
..lineTo(0, 0);
} else {
return Path()
..moveTo(0, y)
..lineTo(x / 2, 0)
..lineTo(x, y)
..lineTo(0, y);
}
}

@override
bool shouldRepaint(TrianglePainter oldDelegate) {
return oldDelegate.strokeColor != strokeColor;
}
}

关于flutter - 自定义形状上的动态数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60804451/

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