You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
HMG_Patient_App_New/lib/widgets/buttons/custom_button.dart

148 lines
5.4 KiB
Dart

10 months ago
import 'package:flutter/material.dart';
import 'package:clarity_flutter/clarity_flutter.dart';
import 'package:hmg_patient_app_new/core/app_export.dart';
10 months ago
import 'package:hmg_patient_app_new/core/utils/utils.dart';
import 'package:hmg_patient_app_new/extensions/string_extensions.dart';
import 'package:hmg_patient_app_new/extensions/widget_extensions.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';
10 months ago
class CustomButton extends StatelessWidget {
final String text;
9 months ago
final String? icon;
3 months ago
final VoidCallback? onPressed;
10 months ago
final Color backgroundColor;
final Color borderColor;
final Color textColor;
9 months ago
final double? borderRadius;
10 months ago
final double borderWidth;
final EdgeInsetsGeometry padding;
9 months ago
final double? fontSize;
10 months ago
final String? fontFamily;
final FontWeight fontWeight;
final bool isDisabled;
10 months ago
final Color? iconColor;
9 months ago
final double? height;
final double? width;
9 months ago
final double? iconSize;
final TextOverflow? textOverflow;
final BorderSide? borderSide;
4 months ago
final bool applyThemeColor;
final bool isBold;
10 months ago
9 months ago
const CustomButton({
super.key,
required this.text,
required this.onPressed,
this.backgroundColor = const Color(0xFFED1C2B),
this.borderColor = const Color(0xFFED1C2B),
this.textColor = Colors.white,
this.borderRadius,
this.borderWidth = 2,
this.padding = const EdgeInsets.fromLTRB(8, 10, 8, 10),
this.fontSize,
this.fontFamily,
this.fontWeight = FontWeight.w700,
9 months ago
this.isDisabled = false,
this.isBold = false,
9 months ago
this.icon,
this.iconColor = Colors.white,
this.height,
this.width,
this.iconSize,
this.textOverflow,
this.borderSide,
4 months ago
this.applyThemeColor = true,
9 months ago
});
10 months ago
@override
Widget build(BuildContext context) {
9 months ago
final radius = borderRadius ?? (12.r);
final iconS = iconSize ?? (24.h);
final fontS = fontSize ?? (16.f);
3 months ago
final isButtonDisabled = isDisabled || onPressed == null;
return Semantics(
button: true,
enabled: !isButtonDisabled,
label: text,
hint: isButtonDisabled ? 'Button is disabled' : 'Tap to $text',
child: GestureDetector(
onTap: isButtonDisabled
? null
: () {
// Log button tap to Clarity before executing callback
try {
Clarity.setCustomTag('button_text', text.length > 100 ? text.substring(0, 100) : text);
Clarity.setCustomTag('button_action', 'tap');
Clarity.setCustomTag('button_disabled', 'false');
Clarity.sendCustomEvent('button_tapped');
} catch (e) {
// Silence Clarity errors to not interrupt user flow
}
// Execute the actual button callback
onPressed?.call();
},
child: Container(
height: height == null
? 56.h
: (isFoldable || isTablet)
? height! + 10.h
: height,
width: width,
padding: padding,
decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
color: isButtonDisabled ? AppColors.inputLabelTextColor.withValues(alpha: 0.3) : backgroundColor,
borderRadius: radius,
customBorder: BorderRadius.circular(radius),
side: borderSide ?? BorderSide(width: borderWidth.h, color: isButtonDisabled ? Colors.transparent : borderColor)),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (icon != null)
Padding(
padding: text.isNotEmpty ? EdgeInsets.only(right: 6.w, left: 6.w) : EdgeInsets.zero,
child: Utils.buildSvgWithAssets(
icon: icon!,
iconColor: isButtonDisabled ? AppColors.greyTextColor : iconColor,
isDisabled: isButtonDisabled,
width: iconS,
height: iconS,
applyThemeColor: applyThemeColor),
),
if (text.isNotEmpty && icon != null)
Flexible(
child: Text(
text,
overflow: textOverflow ?? TextOverflow.ellipsis,
maxLines: 1,
textAlign: TextAlign.center,
style: context.dynamicTextStyle(
fontSize: fontS,
color: isButtonDisabled ? AppColors.greyTextColor : textColor,
letterSpacing: 0,
fontWeight: isBold ? FontWeight.w700 : fontWeight,
),
),
),
if (text.isNotEmpty && icon == null)
Text(
3 months ago
text,
overflow: textOverflow ?? TextOverflow.ellipsis,
maxLines: 1,
textAlign: TextAlign.center,
style: context.dynamicTextStyle(
fontSize: fontS,
color: isButtonDisabled ? AppColors.greyTextColor : textColor,
letterSpacing: 0,
fontWeight: isBold ? FontWeight.w700 : fontWeight,
),
),
],
),
)),
);
10 months ago
}
}