Flutter组件封装:自定义较强的渐变色按钮(适合大量不同颜色的渐变色按钮,命名格式有点怪可自行修改)

该代码段展示了一个在Flutter中实现的自定义Yhh_Button组件,它是一个无状态Widget,支持设置宽度、高度、圆角、字体、字体大小、颜色渐变等属性。按钮使用线性渐变背景,并在按下时透明度有变化,文本样式可定制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 渐变色按钮组件
class Yhh_Button extends StatelessWidget {
  final double width;
  final double height;
  final double radius;
  final String font;
  final double fontsize;
  final FontWeight? fontWeight;
  final Color? fontColor;
  final Alignment begin;
  final Alignment end;
  final void Function()? event;
  final List<Color>? colors;

  const Yhh_Button(
      {super.key,
      required this.width,
      required this.height,
      required this.radius,
      required this.font,
      required this.fontsize,
      this.fontWeight,
      this.fontColor,
      required this.begin,
      required this.end,
      this.event,
      this.colors});

  @override
  Widget build(BuildContext context) {
    final haveWeight = fontWeight ?? 13;
    final havaColors = colors ??
        const [
          Color.fromRGBO(7, 236, 254, 0.69),
          Color.fromRGBO(21, 224, 254, 0.69),
          Color.fromRGBO(105, 197, 251, 0.69),
        ];
    return Container(
      width: width,
      height: height,
      decoration: BoxDecoration(
          borderRadius: BorderRadius.all(Radius.circular(radius)),
          gradient: LinearGradient(begin: begin, end: end, colors: havaColors)),
      child: ElevatedButton(
        style: ButtonStyle(
            elevation: MaterialStateProperty.all(0),
            backgroundColor: MaterialStateProperty.all(Colors.transparent),
            shape: MaterialStateProperty.all(RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(radius)))),
        onPressed: event,
        child: Text(
          font,
          style: TextStyle(
              fontSize: fontsize, fontWeight: haveWeight, color: fontColor),
        ),
      ),
    );
  }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值