gpt4 book ai didi

flutter - Flutter中如何实现自定义形状的容器

转载 作者:行者123 更新时间:2023-12-03 19:45:03 28 4
gpt4 key购买 nike

我想实现一个自定义形状的容器,如下图所示。有没有办法在 Flutter 中构建自定义形状的容器?

enter image description here

最佳答案

卡罗尔的回答有点含糊。我决定用一些实际的代码来扩展它。

我想这就是你要找的。最后一节课对这个问题特别有趣:

class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: CustomPaint(
painter: Chevron(),
child: Container(
width: 100.0,
height: 120.0,
child: Padding(
padding: EdgeInsets.only(top: 30.0),
child: Align(
alignment: Alignment.topCenter,
child: Text("1", style: TextStyle(fontSize: 24.0)),
),
),
),
),
),
);
}
}

class Chevron extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final Gradient gradient = new LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [Colors.orangeAccent, Colors.yellow],
tileMode: TileMode.clamp,
);

final Rect colorBounds = Rect.fromLTRB(0, 0, size.width, size.height);
final Paint paint = new Paint()
..shader = gradient.createShader(colorBounds);

Path path = Path();
path.moveTo(0, 0);
path.lineTo(0, size.height);
path.lineTo(size.width / 2, size.height - size.height / 3);
path.lineTo(size.width, size.height);
path.lineTo(size.width, 0);
path.close();

canvas.drawPath(path, paint);
}

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

关于flutter - Flutter中如何实现自定义形状的容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56170150/

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