button_for_block.dart 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_screenutil/flutter_screenutil.dart';
  3. class ButtonForBlock extends StatefulWidget {
  4. final String title;
  5. final double? width;
  6. final double? height;
  7. final void Function()? onTap;
  8. final double? fontSize;
  9. final Color? bgColor;
  10. final Color? activeBgColor;
  11. final BoxBorder? border;
  12. final double? radius;
  13. const ButtonForBlock({
  14. Key? key,
  15. required this.title,
  16. this.onTap,
  17. this.width,
  18. this.height,
  19. this.fontSize,
  20. this.bgColor=const Color(0xFF00aaFF),
  21. this.activeBgColor=const Color(0xff007ab6),
  22. this.border,
  23. this.radius,
  24. }) : super(key: key);
  25. @override
  26. State<ButtonForBlock> createState() => _ButtonForBlockState();
  27. }
  28. class _ButtonForBlockState extends State<ButtonForBlock> {
  29. late bool isOn = false;
  30. @override
  31. Widget build(BuildContext context) {
  32. return GestureDetector(
  33. onTap: widget.onTap,
  34. onTapDown: (e)=>{setState(()=>{isOn=true})},
  35. onTapUp: (e)=>{setState(()=>{isOn=false})},
  36. onTapCancel: ()=>{setState(()=>{isOn=false})},
  37. child: Container(
  38. height: widget.height ?? 35.h,
  39. alignment: Alignment.center,
  40. width: widget.width ?? 280.w,
  41. decoration: BoxDecoration(
  42. color: isOn? widget.activeBgColor: widget.bgColor,
  43. borderRadius: BorderRadius.circular(widget.radius ?? (widget.height != null? widget.height!/2: 35.h/2)),
  44. border: widget.border
  45. ),
  46. child: Text(widget.title, style: TextStyle(color: Colors.white, fontSize: widget.fontSize ?? 15.sp)),
  47. ),
  48. );
  49. }
  50. }