| 1 | import 'package:cw_core/hardware/device_connection_type.dart'; |
| 2 | import 'package:flutter/material.dart'; |
| 3 | |
| 4 | class SelectButton extends StatelessWidget { |
| 5 | SelectButton({ |
| 6 | required this.text, |
| 7 | required this.onTap, |
| 8 | this.image, |
| 9 | this.isSelected = false, |
| 10 | this.showTrailingIcon = true, |
| 11 | this.height = 60, |
| 12 | this.textSize = 18, |
| 13 | this.color, |
| 14 | this.textColor, |
| 15 | this.arrowColor, |
| 16 | this.borderColor, |
| 17 | this.deviceConnectionTypes, |
| 18 | this.borderRadius, |
| 19 | this.padding, |
| 20 | super.key, |
| 21 | this.center = false, |
| 22 | }); |
| 23 | |
| 24 | final Widget? image; |
| 25 | final String text; |
| 26 | final double textSize; |
| 27 | final bool isSelected; |
| 28 | final VoidCallback onTap; |
| 29 | final bool showTrailingIcon; |
| 30 | final List<DeviceConnectionType>? deviceConnectionTypes; |
| 31 | final double height; |
| 32 | final Color? color; |
| 33 | final Color? textColor; |
| 34 | final Color? arrowColor; |
| 35 | final Color? borderColor; |
| 36 | final BorderRadius? borderRadius; |
| 37 | final EdgeInsets? padding; |
| 38 | final bool center; |
| 39 | |
| 40 | @override |
| 41 | Widget build(BuildContext context) { |
| 42 | final backgroundColor = color ?? |
| 43 | (isSelected |
| 44 | ? Theme.of(context).colorScheme.primary |
| 45 | : Theme.of(context).colorScheme.surfaceContainer); |
| 46 | final effectiveTextColor = textColor ?? |
| 47 | (isSelected |
| 48 | ? Theme.of(context).colorScheme.onPrimary |
| 49 | : Theme.of(context).colorScheme.onSurface); |
| 50 | final effectiveArrowColor = arrowColor ?? |
| 51 | (isSelected |
| 52 | ? Theme.of(context).colorScheme.onPrimary |
| 53 | : Theme.of(context).colorScheme.onSurface); |
| 54 | |
| 55 | final trailingIcons = <Image>[]; |
| 56 | final selectArrowImage = |
| 57 | Image.asset('assets/images/select_arrow.png', color: effectiveArrowColor); |
| 58 | |
| 59 | deviceConnectionTypes?.forEach((element) => trailingIcons.add(Image.asset( |
| 60 | element.iconString, |
| 61 | color: effectiveArrowColor, |
| 62 | height: 24, |
| 63 | ))); |
| 64 | |
| 65 | if (showTrailingIcon) trailingIcons.add(selectArrowImage); |
| 66 | |
| 67 | return GestureDetector( |
| 68 | onTap: onTap, |
| 69 | child: Container( |
| 70 | width: double.infinity, |
| 71 | height: height, |
| 72 | alignment: Alignment.center, |
| 73 | decoration: BoxDecoration( |
| 74 | borderRadius: borderRadius ?? BorderRadius.all(Radius.circular(18)), |
| 75 | color: backgroundColor, |
| 76 | border: borderColor != null ? Border.all(color: borderColor!, width: 2) : null, |
| 77 | ), |
| 78 | child: Row( |
| 79 | mainAxisSize: MainAxisSize.max, |
| 80 | mainAxisAlignment: MainAxisAlignment.spaceBetween, |
| 81 | crossAxisAlignment: CrossAxisAlignment.center, |
| 82 | children: <Widget>[ |
| 83 | Expanded( |
| 84 | child: Row( |
| 85 | mainAxisAlignment: center ? MainAxisAlignment.center : MainAxisAlignment.start, |
| 86 | crossAxisAlignment: CrossAxisAlignment.center, |
| 87 | children: <Widget>[ |
| 88 | if (!center) SizedBox(width: 16), |
| 89 | image ?? Offstage(), |
| 90 | Padding( |
| 91 | padding: image != null ? EdgeInsets.only(left: 14) : EdgeInsets.only(left: 0), |
| 92 | child: Text( |
| 93 | text, |
| 94 | style: Theme.of(context).textTheme.bodyMedium!.copyWith( |
| 95 | fontSize: textSize, |
| 96 | fontWeight: FontWeight.w500, |
| 97 | color: effectiveTextColor, |
| 98 | ), |
| 99 | ), |
| 100 | ) |
| 101 | ], |
| 102 | ), |
| 103 | ), |
| 104 | ...trailingIcons, |
| 105 | if (!center) SizedBox(width: 16), |
| 106 | ], |
| 107 | ), |
| 108 | ), |
| 109 | ); |
| 110 | } |
| 111 | } |