gpt4 book ai didi

flutter - 如何在 Flutter 中绘制自定义形状卡片

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

我只想制作这样的卡片

enter image description here

最佳答案

代码如下,我使用CustomPaint Widget来绘制自定义形状,然后使用Card Widget内部的stack来正确放置widget。

我没有把图片改成粉红色来显示图片:


这是 Card Widget 的代码,然后是 CustomPainter 类:

   Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(50.0)),
elevation: 10.0,
child: Container(
width: 300.0,
height: 400.0,
child: Stack(
alignment: Alignment.bottomCenter,
children: [
// This will hold the Image in the back ground:
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(50.0),
color: Colors.pink[100]),
),
// This is the Custom Shape Container
Positioned(
bottom: 0.0,
left: 0.0,
child: Container(
color: Colors.red,
child: CustomPaint(
painter: CustomContainerShapeBorder(
height: 100.0,
width: 300.0,
radius: 50.0,
),
),
),
),
// This Holds the Widgets Inside the the custom Container;
Positioned(
bottom: 10.0,
child: Container(
height: 80.0,
width: 260.0,
color: Colors.grey.withOpacity(0.6),
child: null,
),
),
],
),
),
),

自定义画家类:

/// The {CustomContainerShapeBorder} should be reactibe with different sizes,
/// If it isn't then chamge the offset values.
class CustomContainerShapeBorder extends CustomPainter {
final double height;
final double width;
final Color fillColor;
final double radius;

CustomContainerShapeBorder({
this.height: 400.0,
this.width: 300.0,
this.fillColor: Colors.white,
this.radius: 50.0,
});
@override
void paint(Canvas canvas, Size size) {
Path path = new Path();
path.moveTo(0.0, -radius);
path.lineTo(0.0, -(height - radius));
path.conicTo(0.0, -height, radius, -height, 1);
path.lineTo(width - radius, -height);
path.conicTo(width, -height, width, -(height + radius), 1);
path.lineTo(width, -(height - radius));
path.lineTo(width, -radius);

path.conicTo(width, 0.0, width - radius, 0.0, 1);
path.lineTo(radius, 0.0);
path.conicTo(0.0, 0.0, 0.0, -radius, 1);
path.close();
canvas.drawPath(path, Paint()..color = fillColor);
}

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

输出:灰色的容器是用来描绘Custom Shape里面的内容

Output Image

整个代码:

import 'package:flutter/material.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Custom Card Design',
theme: ThemeData(
primarySwatch: Colors.amber,
),
home: MyHomePage(),
);
}
}

class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Container(
color: Colors.amber,
child: Center(
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(50.0)),
elevation: 10.0,
child: Container(
width: 300.0,
height: 400.0,
child: Stack(
alignment: Alignment.bottomCenter,
children: [
// This will hold the Image in the back ground:
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(50.0),
color: Colors.pink[100]),
),
// This is the Custom Shape Container
Positioned(
bottom: 0.0,
left: 0.0,
child: Container(
color: Colors.red,
child: CustomPaint(
painter: CustomContainerShapeBorder(
height: 100.0,
width: 300.0,
radius: 50.0,
),
),
),
),
// This Holds the Widgets Inside the the custom Container;
Positioned(
bottom: 10.0,
child: Container(
height: 80.0,
width: 260.0,
color: Colors.grey.withOpacity(0.6),
child: null,
),
),
],
),
),
),
));
}
}

/// The {CustomContainerShapeBorder} should be reactibe with different sizes,
/// If it isn't then chamge the offset values.
class CustomContainerShapeBorder extends CustomPainter {
final double height;
final double width;
final Color fillColor;
final double radius;

CustomContainerShapeBorder({
this.height: 400.0,
this.width: 300.0,
this.fillColor: Colors.white,
this.radius: 50.0,
});
@override
void paint(Canvas canvas, Size size) {
Path path = new Path();
path.moveTo(0.0, -radius);
path.lineTo(0.0, -(height - radius));
path.conicTo(0.0, -height, radius, -height, 1);
path.lineTo(width - radius, -height);
path.conicTo(width, -height, width, -(height + radius), 1);
path.lineTo(width, -(height - radius));
path.lineTo(width, -radius);

path.conicTo(width, 0.0, width - radius, 0.0, 1);
path.lineTo(radius, 0.0);
path.conicTo(0.0, 0.0, 0.0, -radius, 1);
path.close();
canvas.drawPath(path, Paint()..color = fillColor);
}

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

关于flutter - 如何在 Flutter 中绘制自定义形状卡片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63751539/

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