Files
flutterApp/packages/cc_ui_kit/lib/src/cc_primary_button.dart

54 lines
1.6 KiB
Dart
Raw Normal View History

2026-01-11 19:42:22 +08:00
import 'package:flutter/material.dart';
class CCPrimaryButton extends StatelessWidget {
final String text;
2026-01-18 20:21:14 +08:00
final VoidCallback? onPressed;
final double? width;
final double height;
final double fontSize;
final Color backgroundColor; // 新增:背景颜色
final Color textColor; // 新增:字体颜色
2026-01-11 19:42:22 +08:00
const CCPrimaryButton({
super.key,
required this.text,
2026-01-18 20:21:14 +08:00
this.onPressed,
this.width = double.infinity,
this.height = 54.0,
this.fontSize = 16.0,
this.backgroundColor = Colors.black, // 默认黑色背景
this.textColor = Colors.white, // 默认白色文字
2026-01-11 19:42:22 +08:00
});
@override
Widget build(BuildContext context) {
2026-01-18 20:21:14 +08:00
// 💡 核心改动:如果父组件是 Row 且没有指定 width,
// 直接用 double.infinity 会崩溃。
// 使用这种方式可以更加安全。
return SizedBox(
width: width,
height: height,
child: ElevatedButton(
onPressed: onPressed,
style: ElevatedButton.styleFrom(
backgroundColor: backgroundColor, // 使用参数
foregroundColor: textColor, // 使用参数
disabledBackgroundColor: Colors.grey.shade300,
disabledForegroundColor: Colors.white,
elevation: 0,
padding: const EdgeInsets.symmetric(horizontal: 16),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
textStyle: TextStyle(
fontSize: fontSize,
fontWeight: FontWeight.w600,
letterSpacing: 1.2,
),
),
child: Text(text),
),
);
2026-01-11 19:42:22 +08:00
}
}