gpt4 book ai didi

flutter - 如何设置带有圆锥形边框的按钮的形状

转载 作者:IT王子 更新时间:2023-10-29 07:05:21 24 4
gpt4 key购买 nike

This kinda button我正在处理 flutter 中的按钮,并想在我的应用程序中更改按钮的形状。如何为我的按钮设置圆锥形边框?

最佳答案

您可以使用 CustomPaint 创建您自己的一个绘制按钮形状的小部件。

class MyButton extends StatelessWidget {
final double width;
final double height;
final Color color;
final Widget child;
final VoidCallback onPressed;

const MyButton({
Key key,
this.width = 150.0,
this.height = 75.0,
this.color,
@required this.child,
@required this.onPressed,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onPressed,
child: Container(
width: width,
height: height,
child: CustomPaint(
painter: MyButtonPainter(
color: color != null ? color : Theme.of(context).primaryColor),
child: Center(child: child),
),
),
);
}
}

class MyButtonPainter extends CustomPainter {
final Color color;

MyButtonPainter({this.color});
@override
void paint(Canvas canvas, Size size) {
final Paint paint = Paint()..color = color;
final double arrowDepth = size.height / 2;

final Path path = Path();

path.lineTo(size.width - arrowDepth, 0.0);
path.lineTo(size.width, size.height / 2);
path.lineTo(size.width - arrowDepth, size.height);
path.lineTo(0.0, size.height);
path.lineTo(arrowDepth, size.height / 2);
path.close();

canvas.drawPath(path, paint);
}

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

用法

MyButton(
width: 300.0,
child: Text(
'Time',
style:
Theme.of(context).textTheme.title.copyWith(color: Colors.white),
),
onPressed: () {},
),

您可以根据您的要求微调代码

关于flutter - 如何设置带有圆锥形边框的按钮的形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53929637/

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