import 'package:flutter/material.dart'; import 'package:google_fonts/google_fonts.dart'; import '../theme/app_colors.dart'; import '../theme/product_theme.dart'; class GradientButton extends StatelessWidget { final String text; final VoidCallback? onPressed; final double width; final double height; final bool isLoading; final Gradient? gradient; final List? shadows; const GradientButton({ super.key, required this.text, this.onPressed, this.width = double.infinity, this.height = 48.0, // 统一规范高度 this.isLoading = false, this.gradient, this.shadows, }); /// 从 ProductThemeData 创建,自动匹配渐变和阴影 factory GradientButton.fromTheme({ Key? key, required String text, required ProductThemeData theme, VoidCallback? onPressed, double width = double.infinity, double height = 48.0, bool isLoading = false, }) { return GradientButton( key: key, text: text, onPressed: onPressed, width: width, height: height, isLoading: isLoading, gradient: theme.buttonGradient, shadows: theme.buttonShadows, ); } List get _boxShadows { if (shadows != null) return shadows!; // 根据渐变颜色自动推断阴影 if (gradient is LinearGradient) { final lg = gradient as LinearGradient; if (lg.colors.length >= 2) { final shadowColor = lg.colors[lg.colors.length ~/ 2]; return [ BoxShadow( color: shadowColor.withOpacity(0.4), offset: const Offset(0, 4), blurRadius: 20, ), BoxShadow( color: lg.colors.last.withOpacity(0.2), offset: Offset.zero, blurRadius: 40, ), ]; } } return AppColors.shadowPrimaryButton; } @override Widget build(BuildContext context) { final bool isDisabled = onPressed == null || isLoading; return Opacity( opacity: isDisabled ? 0.7 : 1.0, child: Container( width: width, height: height, decoration: BoxDecoration( borderRadius: BorderRadius.circular(height / 2), gradient: gradient ?? AppColors.btnPrimaryGradient, boxShadow: _boxShadows, ), child: Stack( children: [ // Shine overlay (top half gradient) Positioned.fill( child: ClipRRect( borderRadius: BorderRadius.circular(height / 2), child: DecoratedBox( decoration: BoxDecoration( gradient: LinearGradient( begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [ Colors.white.withOpacity(0.15), Colors.transparent, ], stops: const [0.0, 0.5], ), ), ), ), ), // Button content Material( color: Colors.transparent, elevation: 0, child: InkWell( onTap: isDisabled ? null : onPressed, borderRadius: BorderRadius.circular(height / 2), splashColor: Colors.white.withOpacity(0.2), highlightColor: Colors.white.withOpacity(0.1), child: Center( child: isLoading ? const SizedBox( width: 24, height: 24, child: CircularProgressIndicator( color: Colors.white, strokeWidth: 2.5, ), ) : Text( text, style: GoogleFonts.dmSans( fontSize: 17, fontWeight: FontWeight.w600, color: Colors.white, ), ), ), ), ), ], ), ), ); } }