| 1 | import 'package:cake_wallet/generated/i18n.dart'; |
| 2 | import 'package:cake_wallet/new-ui/widgets/currency_picker/currency_picker_args.dart'; |
| 3 | import 'package:cw_core/crypto_currency.dart'; |
| 4 | import 'package:flutter/material.dart'; |
| 5 | |
| 6 | class CurrencyPickerSearchField extends StatelessWidget { |
| 7 | const CurrencyPickerSearchField({ |
| 8 | super.key, |
| 9 | required this.controller, |
| 10 | required this.hintText, |
| 11 | }); |
| 12 | |
| 13 | final TextEditingController controller; |
| 14 | final String hintText; |
| 15 | |
| 16 | @override |
| 17 | Widget build(BuildContext context) { |
| 18 | final colors = Theme.of(context).colorScheme; |
| 19 | return Padding( |
| 20 | padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8), |
| 21 | child: Container( |
| 22 | height: 48, |
| 23 | padding: const EdgeInsets.symmetric(horizontal: 14), |
| 24 | decoration: BoxDecoration( |
| 25 | color: colors.surfaceContainer, |
| 26 | border: Border.all(color: colors.surfaceContainer, width: 1), |
| 27 | borderRadius: BorderRadius.circular(28), |
| 28 | ), |
| 29 | child: Row( |
| 30 | children: [ |
| 31 | ExcludeSemantics(child: Icon(Icons.search, size: 20, color: colors.primary)), |
| 32 | const SizedBox(width: 10), |
| 33 | Expanded( |
| 34 | // The hint names the field only while it is empty, so the label is |
| 35 | // supplied once there is text to keep exactly one announcement. |
| 36 | child: MergeSemantics( |
| 37 | child: Semantics( |
| 38 | label: controller.text.isEmpty ? null : hintText, |
| 39 | child: TextFormField( |
| 40 | controller: controller, |
| 41 | style: Theme.of(context).textTheme.bodyMedium, |
| 42 | decoration: InputDecoration( |
| 43 | isCollapsed: true, |
| 44 | border: InputBorder.none, |
| 45 | focusedBorder: InputBorder.none, |
| 46 | hintText: hintText, |
| 47 | hintStyle: Theme.of(context).textTheme.bodyMedium?.copyWith( |
| 48 | color: colors.onSurfaceVariant, |
| 49 | ), |
| 50 | ), |
| 51 | ), |
| 52 | ), |
| 53 | ), |
| 54 | ), |
| 55 | if (controller.text.isNotEmpty) |
| 56 | Semantics( |
| 57 | button: true, |
| 58 | label: S.of(context).clear, |
| 59 | child: InkWell( |
| 60 | onTap: controller.clear, |
| 61 | customBorder: const CircleBorder(), |
| 62 | child: Padding( |
| 63 | padding: const EdgeInsets.all(4), |
| 64 | child: Icon(Icons.close, size: 16, color: colors.onSurfaceVariant), |
| 65 | ), |
| 66 | ), |
| 67 | ), |
| 68 | ], |
| 69 | ), |
| 70 | ), |
| 71 | ); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | bool currencyMatchesQuery(CryptoCurrency c, String query) { |
| 76 | if (query.isEmpty) return true; |
| 77 | final q = query.toLowerCase(); |
| 78 | if (c.title.toLowerCase().contains(q)) return true; |
| 79 | if (c.name.toLowerCase().contains(q)) return true; |
| 80 | if (c.fullName?.toLowerCase().contains(q) ?? false) return true; |
| 81 | if (c.tag?.toLowerCase().contains(q) ?? false) return true; |
| 82 | if (chainNameForCurrency(c).toLowerCase().contains(q)) return true; |
| 83 | return false; |
| 84 | } |