Files
flutterApp/packages/cc_ui_kit/lib/src/cc_primary_button.dart
2026-01-18 20:21:14 +08:00

54 lines
1.6 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:flutter/material.dart';
class CCPrimaryButton extends StatelessWidget {
final String text;
final VoidCallback? onPressed;
final double? width;
final double height;
final double fontSize;
final Color backgroundColor; // 新增:背景颜色
final Color textColor; // 新增:字体颜色
const CCPrimaryButton({
super.key,
required this.text,
this.onPressed,
this.width = double.infinity,
this.height = 54.0,
this.fontSize = 16.0,
this.backgroundColor = Colors.black, // 默认黑色背景
this.textColor = Colors.white, // 默认白色文字
});
@override
Widget build(BuildContext context) {
// 💡 核心改动:如果父组件是 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),
),
);
}
}