gradient_button.dart 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import 'package:flutter/material.dart';
  2. class GradientButton extends StatelessWidget {
  3. final List<Color> linearGradientColors;
  4. final Function? onTap;
  5. // text is ignored if child is specified
  6. final String text;
  7. // nullable
  8. final IconData? iconData;
  9. // padding between the text and icon
  10. final double paddingValue;
  11. // used when two icons are in row
  12. final bool reversedGradient;
  13. const GradientButton({
  14. super.key,
  15. this.linearGradientColors = const [
  16. Color.fromARGB(255, 133, 44, 210),
  17. Color.fromARGB(255, 187, 26, 93),
  18. ],
  19. this.reversedGradient = false,
  20. this.onTap,
  21. this.text = '',
  22. this.iconData,
  23. this.paddingValue = 0.0,
  24. });
  25. @override
  26. Widget build(BuildContext context) {
  27. Widget buttonContent;
  28. if (iconData == null) {
  29. buttonContent = Text(
  30. text,
  31. style: const TextStyle(
  32. color: Colors.white,
  33. fontWeight: FontWeight.w600,
  34. fontFamily: 'Inter-SemiBold',
  35. fontSize: 18,
  36. ),
  37. );
  38. } else {
  39. buttonContent = Row(
  40. mainAxisAlignment: MainAxisAlignment.center,
  41. crossAxisAlignment: CrossAxisAlignment.center,
  42. children: [
  43. Icon(
  44. iconData,
  45. size: 20,
  46. color: Colors.white,
  47. ),
  48. const Padding(padding: EdgeInsets.symmetric(horizontal: 6)),
  49. Text(
  50. text,
  51. style: const TextStyle(
  52. color: Colors.white,
  53. fontWeight: FontWeight.w600,
  54. fontFamily: 'Inter-SemiBold',
  55. fontSize: 18,
  56. ),
  57. ),
  58. ],
  59. );
  60. }
  61. return InkWell(
  62. onTap: onTap as void Function()?,
  63. child: Container(
  64. height: 56,
  65. decoration: BoxDecoration(
  66. gradient: LinearGradient(
  67. begin: const Alignment(0.1, -0.9),
  68. end: const Alignment(-0.6, 0.9),
  69. colors: reversedGradient
  70. ? linearGradientColors.reversed.toList()
  71. : linearGradientColors,
  72. ),
  73. borderRadius: BorderRadius.circular(8),
  74. ),
  75. child: Center(child: buttonContent),
  76. ),
  77. );
  78. }
  79. }