| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- import 'package:flutter/cupertino.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter_screenutil/flutter_screenutil.dart';
- import '../resource/colors.gen.dart';
- /// 渐变色按钮
- class GradientBtn extends StatelessWidget {
- /// 纯色颜色
- final Color? color;
- /// 渐变颜色参数
- final List<Color> colors;
- /// 渐变方向
- final AlignmentGeometry begin;
- final AlignmentGeometry end;
- /// 按钮圆角半径
- final double radius;
- /// 子组件
- final Widget child;
- /// 点击回调
- final VoidCallback onPressed;
- const GradientBtn({
- super.key,
- required this.colors,
- this.color,
- this.begin = Alignment.centerLeft,
- this.end = Alignment.centerRight,
- this.radius = 50,
- required this.child,
- required this.onPressed,
- });
- @override
- Widget build(BuildContext context) {
- Decoration decoration;
- // 优先使用纯色,如果没有纯色,则使用渐变色
- if (color != null) {
- decoration = BoxDecoration(
- color: color,
- borderRadius: BorderRadius.all(Radius.circular(radius)),
- );
- } else {
- // 渐变色
- decoration = ShapeDecoration(
- gradient: LinearGradient(colors: colors, begin: begin, end: end),
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.circular(radius),
- ),
- );
- }
- return GestureDetector(
- onTap: () {
- onPressed();
- },
- child: Container(
- padding: EdgeInsets.symmetric(vertical: 14.h),
- width: double.maxFinite,
- // 渐变背景
- decoration: decoration,
- child: Center(child: child),
- ),
- );
- }
- }
- /// 渐变按钮,中间为文字
- class GradientTextBtn extends StatelessWidget {
- /// 按钮文字
- final String text;
- /// 纯色颜色
- final Color? color;
- /// 渐变颜色参数
- final List<Color> colors;
- /// 渐变方向
- final AlignmentGeometry begin;
- final AlignmentGeometry end;
- /// 按钮圆角半径
- final double radius;
- /// 点击回调
- final VoidCallback onPressed;
- const GradientTextBtn(
- this.text, {
- super.key,
- this.color,
- this.colors = const [ColorName.purpleGradient3, ColorName.purpleGradient4],
- this.begin = Alignment.centerLeft,
- this.end = Alignment.centerRight,
- this.radius = 50,
- required this.onPressed,
- });
- @override
- Widget build(BuildContext context) {
- return GradientBtn(
- color: color,
- colors: colors,
- radius: radius,
- onPressed: onPressed,
- child: Text(
- text,
- style: TextStyle(
- color: ColorName.white,
- fontSize: 16.sp,
- fontWeight: FontWeight.bold,
- ),
- ),
- );
- }
- }
|