custome button

This commit is contained in:
2025-05-29 12:55:37 +02:00
parent 362def856a
commit 15c4143821
2 changed files with 46 additions and 22 deletions

View File

@@ -130,6 +130,7 @@ class _PackageToolOneState extends State<PackageToolOne> {
},
buttonColor:
MzanziInnovationHub.of(context)!.theme.secondaryColor(),
elevation: 10,
width: 300,
child: Text(
"Click Me",
@@ -141,7 +142,7 @@ class _PackageToolOneState extends State<PackageToolOne> {
),
),
),
const SizedBox(height: 10),
const SizedBox(height: 40),
MihButton(
onPressed: () {
print("Button Pressed");
@@ -150,6 +151,7 @@ class _PackageToolOneState extends State<PackageToolOne> {
MzanziInnovationHub.of(context)!.theme.successColor(),
width: 300,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.delete,

View File

@@ -1,42 +1,64 @@
import 'package:animated_button/animated_button.dart';
import 'package:flutter/material.dart';
class MihButton extends StatefulWidget {
final void Function() onPressed;
class MihButton extends StatelessWidget {
final void Function()? onPressed;
final Color buttonColor;
final double width;
final double? width;
final double? height;
final double? borderRadius;
final double? elevation; // 0 = flat, higher = more shadow
final Widget child;
const MihButton({
super.key,
required this.onPressed,
required this.buttonColor,
required this.width,
this.width,
this.height,
this.borderRadius,
this.elevation,
required this.child,
});
Color _darkerColor(Color color, [double amount = .1]) {
final hsl = HSLColor.fromColor(color);
final hslDark = hsl.withLightness((hsl.lightness - amount).clamp(0.0, 1.0));
return hslDark.toColor();
}
@override
State<MihButton> createState() => _MihButtonState();
}
class _MihButtonState extends State<MihButton> {
@override
Widget build(BuildContext context) {
return FittedBox(
fit: BoxFit.contain,
child: AnimatedButton(
width: widget.width,
height: widget.height ?? 50.0,
borderRadius: widget.borderRadius ?? 25.0,
color: widget.buttonColor,
shadowDegree: ShadowDegree.light,
duration: 30,
onPressed: widget.onPressed,
child: FittedBox(child: widget.child),
final Color effectiveButtonColor = onPressed == null
? buttonColor.withValues(alpha: 0.6) // Example disabled color
: buttonColor;
final Color rippleColor = _darkerColor(effectiveButtonColor, 0.1);
final double radius = borderRadius ?? 25.0;
final double effectiveElevation =
onPressed == null ? 0.0 : (elevation ?? 4.0);
return MouseRegion(
cursor: onPressed == null
? SystemMouseCursors.basic
: SystemMouseCursors.click,
child: Material(
color: effectiveButtonColor,
borderRadius: BorderRadius.circular(radius),
elevation: effectiveElevation,
shadowColor: Colors.black,
child: InkWell(
borderRadius: BorderRadius.circular(radius),
splashColor: rippleColor,
highlightColor: rippleColor.withValues(alpha: 0.2),
hoverColor: rippleColor.withValues(alpha: 0.15),
onTap: onPressed,
child: Container(
width: width,
height: height,
padding: (width == null || height == null)
? const EdgeInsets.symmetric(horizontal: 24, vertical: 12)
: null,
alignment: Alignment.center,
child: child,
),
),
),
);
}