|
|
|
@ -6,7 +6,7 @@ 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/extensions/widget_extensions.dart';
|
|
|
|
import 'package:hmg_patient_app_new/theme/colors.dart';
|
|
|
|
import 'package:hmg_patient_app_new/theme/colors.dart';
|
|
|
|
|
|
|
|
|
|
|
|
class CustomButton extends StatelessWidget {
|
|
|
|
class CustomButton extends StatefulWidget {
|
|
|
|
final String text;
|
|
|
|
final String text;
|
|
|
|
final String? icon;
|
|
|
|
final String? icon;
|
|
|
|
final VoidCallback? onPressed;
|
|
|
|
final VoidCallback? onPressed;
|
|
|
|
@ -54,25 +54,61 @@ class CustomButton extends StatelessWidget {
|
|
|
|
this.applyThemeColor = true,
|
|
|
|
this.applyThemeColor = true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
|
|
|
State<CustomButton> createState() => _CustomButtonState();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class _CustomButtonState extends State<CustomButton> with SingleTickerProviderStateMixin {
|
|
|
|
|
|
|
|
late AnimationController _animationController;
|
|
|
|
|
|
|
|
late Animation<double> _scaleAnimation;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
|
|
|
void initState() {
|
|
|
|
|
|
|
|
super.initState();
|
|
|
|
|
|
|
|
_animationController = AnimationController(
|
|
|
|
|
|
|
|
duration: const Duration(milliseconds: 80),
|
|
|
|
|
|
|
|
vsync: this,
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
_scaleAnimation = Tween<double>(begin: 1.0, end: 0.95).animate(
|
|
|
|
|
|
|
|
CurvedAnimation(parent: _animationController, curve: Curves.easeInOut),
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
|
|
|
void dispose() {
|
|
|
|
|
|
|
|
_animationController.dispose();
|
|
|
|
|
|
|
|
super.dispose();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Future<void> _playTapAnimation() async {
|
|
|
|
|
|
|
|
if (!widget.isDisabled && widget.onPressed != null) {
|
|
|
|
|
|
|
|
await _animationController.forward();
|
|
|
|
|
|
|
|
_animationController.reverse();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final radius = borderRadius ?? (12.r);
|
|
|
|
final radius = widget.borderRadius ?? (12.r);
|
|
|
|
final iconS = iconSize ?? (24.h);
|
|
|
|
final iconS = widget.iconSize ?? (24.h);
|
|
|
|
final fontS = fontSize ?? (16.f);
|
|
|
|
final fontS = widget.fontSize ?? (16.f);
|
|
|
|
final isButtonDisabled = isDisabled || onPressed == null;
|
|
|
|
final isButtonDisabled = widget.isDisabled || widget.onPressed == null;
|
|
|
|
|
|
|
|
|
|
|
|
return Semantics(
|
|
|
|
return Semantics(
|
|
|
|
button: true,
|
|
|
|
button: true,
|
|
|
|
enabled: !isButtonDisabled,
|
|
|
|
enabled: !isButtonDisabled,
|
|
|
|
label: text,
|
|
|
|
label: widget.text,
|
|
|
|
hint: isButtonDisabled ? 'Button is disabled' : 'Tap to $text',
|
|
|
|
hint: isButtonDisabled ? 'Button is disabled' : 'Tap to ${widget.text}',
|
|
|
|
child: GestureDetector(
|
|
|
|
child: GestureDetector(
|
|
|
|
onTap: isButtonDisabled
|
|
|
|
onTap: isButtonDisabled
|
|
|
|
? null
|
|
|
|
? null
|
|
|
|
: () {
|
|
|
|
: () async {
|
|
|
|
|
|
|
|
// Play tap animation and wait for it to complete
|
|
|
|
|
|
|
|
await _playTapAnimation();
|
|
|
|
|
|
|
|
|
|
|
|
// Log button tap to Clarity before executing callback
|
|
|
|
// Log button tap to Clarity before executing callback
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
Clarity.setCustomTag('button_text', text.length > 100 ? text.substring(0, 100) : text);
|
|
|
|
Clarity.setCustomTag('button_text', widget.text.length > 100 ? widget.text.substring(0, 100) : widget.text);
|
|
|
|
Clarity.setCustomTag('button_action', 'tap');
|
|
|
|
Clarity.setCustomTag('button_action', 'tap');
|
|
|
|
Clarity.setCustomTag('button_disabled', 'false');
|
|
|
|
Clarity.setCustomTag('button_disabled', 'false');
|
|
|
|
Clarity.sendCustomEvent('button_tapped');
|
|
|
|
Clarity.sendCustomEvent('button_tapped');
|
|
|
|
@ -81,67 +117,73 @@ class CustomButton extends StatelessWidget {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Execute the actual button callback
|
|
|
|
// Execute the actual button callback
|
|
|
|
onPressed?.call();
|
|
|
|
widget.onPressed?.call();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
child: Container(
|
|
|
|
child: AnimatedBuilder(
|
|
|
|
height: height == null
|
|
|
|
animation: _scaleAnimation,
|
|
|
|
? 56.h
|
|
|
|
builder: (context, child) => Transform.scale(
|
|
|
|
|
|
|
|
scale: _scaleAnimation.value,
|
|
|
|
|
|
|
|
child: child,
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
child: Container(
|
|
|
|
|
|
|
|
height: widget.height == null
|
|
|
|
|
|
|
|
? 56.h
|
|
|
|
: (isFoldable || isTablet)
|
|
|
|
: (isFoldable || isTablet)
|
|
|
|
? height! + 10.h
|
|
|
|
? widget.height! + 10.h
|
|
|
|
: height,
|
|
|
|
: widget.height,
|
|
|
|
width: width,
|
|
|
|
width: widget.width,
|
|
|
|
padding: padding,
|
|
|
|
padding: widget.padding,
|
|
|
|
decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
|
|
|
|
decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
|
|
|
|
color: isButtonDisabled ? AppColors.inputLabelTextColor.withValues(alpha: 0.3) : backgroundColor,
|
|
|
|
color: isButtonDisabled ? AppColors.inputLabelTextColor.withValues(alpha: 0.3) : widget.backgroundColor,
|
|
|
|
borderRadius: radius,
|
|
|
|
borderRadius: radius,
|
|
|
|
customBorder: BorderRadius.circular(radius),
|
|
|
|
customBorder: BorderRadius.circular(radius),
|
|
|
|
side: borderSide ?? BorderSide(width: borderWidth.h, color: isButtonDisabled ? Colors.transparent : borderColor)),
|
|
|
|
side: widget.borderSide ?? BorderSide(width: widget.borderWidth.h, color: isButtonDisabled ? Colors.transparent : widget.borderColor)),
|
|
|
|
child: Row(
|
|
|
|
child: Row(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
children: [
|
|
|
|
if (icon != null)
|
|
|
|
if (widget.icon != null)
|
|
|
|
Padding(
|
|
|
|
Padding(
|
|
|
|
padding: text.isNotEmpty ? EdgeInsets.only(right: 6.w, left: 6.w) : EdgeInsets.zero,
|
|
|
|
padding: widget.text.isNotEmpty ? EdgeInsets.only(right: 6.w, left: 6.w) : EdgeInsets.zero,
|
|
|
|
child: Utils.buildSvgWithAssets(
|
|
|
|
child: Utils.buildSvgWithAssets(
|
|
|
|
icon: icon!,
|
|
|
|
icon: widget.icon!,
|
|
|
|
iconColor: isButtonDisabled ? AppColors.greyTextColor : iconColor,
|
|
|
|
iconColor: isButtonDisabled ? AppColors.greyTextColor : widget.iconColor,
|
|
|
|
isDisabled: isButtonDisabled,
|
|
|
|
isDisabled: isButtonDisabled,
|
|
|
|
width: iconS,
|
|
|
|
width: iconS,
|
|
|
|
height: iconS,
|
|
|
|
height: iconS,
|
|
|
|
applyThemeColor: applyThemeColor),
|
|
|
|
applyThemeColor: widget.applyThemeColor),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
if (text.isNotEmpty && icon != null)
|
|
|
|
if (widget.text.isNotEmpty && widget.icon != null)
|
|
|
|
Flexible(
|
|
|
|
Flexible(
|
|
|
|
child: Text(
|
|
|
|
child: Text(
|
|
|
|
text,
|
|
|
|
widget.text,
|
|
|
|
overflow: textOverflow ?? TextOverflow.ellipsis,
|
|
|
|
overflow: widget.textOverflow ?? TextOverflow.ellipsis,
|
|
|
|
maxLines: 1,
|
|
|
|
maxLines: 1,
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: context.dynamicTextStyle(
|
|
|
|
style: context.dynamicTextStyle(
|
|
|
|
fontSize: fontS,
|
|
|
|
fontSize: fontS,
|
|
|
|
color: isButtonDisabled ? AppColors.greyTextColor : textColor,
|
|
|
|
color: isButtonDisabled ? AppColors.greyTextColor : widget.textColor,
|
|
|
|
letterSpacing: 0,
|
|
|
|
letterSpacing: 0,
|
|
|
|
fontWeight: isBold ? FontWeight.w700 : fontWeight,
|
|
|
|
fontWeight: widget.isBold ? FontWeight.w700 : widget.fontWeight,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
if (text.isNotEmpty && icon == null)
|
|
|
|
if (widget.text.isNotEmpty && widget.icon == null)
|
|
|
|
Text(
|
|
|
|
Text(
|
|
|
|
text,
|
|
|
|
widget.text,
|
|
|
|
overflow: textOverflow ?? TextOverflow.ellipsis,
|
|
|
|
overflow: widget.textOverflow ?? TextOverflow.ellipsis,
|
|
|
|
maxLines: 1,
|
|
|
|
maxLines: 1,
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: context.dynamicTextStyle(
|
|
|
|
style: context.dynamicTextStyle(
|
|
|
|
fontSize: fontS,
|
|
|
|
fontSize: fontS,
|
|
|
|
color: isButtonDisabled ? AppColors.greyTextColor : textColor,
|
|
|
|
color: isButtonDisabled ? AppColors.greyTextColor : widget.textColor,
|
|
|
|
letterSpacing: 0,
|
|
|
|
letterSpacing: 0,
|
|
|
|
fontWeight: isBold ? FontWeight.w700 : fontWeight,
|
|
|
|
fontWeight: widget.isBold ? FontWeight.w700 : widget.fontWeight,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)),
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|