| 1 | import 'package:cake_wallet/src/widgets/alert_with_one_action.dart'; |
| 2 | import 'package:cake_wallet/src/widgets/check_box_picker.dart'; |
| 3 | import 'package:cake_wallet/utils/show_pop_up.dart'; |
| 4 | import 'package:flutter/material.dart'; |
| 5 | import 'package:flutter_mobx/flutter_mobx.dart'; |
| 6 | import 'package:cake_wallet/generated/i18n.dart'; |
| 7 | import 'package:cake_wallet/view_model/exchange/exchange_view_model.dart'; |
| 8 | |
| 9 | class PresentProviderPicker extends StatelessWidget { |
| 10 | PresentProviderPicker({required this.exchangeViewModel}); |
| 11 | |
| 12 | final ExchangeViewModel exchangeViewModel; |
| 13 | |
| 14 | @override |
| 15 | Widget build(BuildContext context) { |
| 16 | final arrowBottom = Image.asset( |
| 17 | 'assets/images/arrow_bottom_purple_icon.png', |
| 18 | color: Theme.of(context).colorScheme.primary, |
| 19 | height: 6, |
| 20 | ); |
| 21 | |
| 22 | return Padding( |
| 23 | padding: EdgeInsets.only(left: 10), |
| 24 | child: TextButton( |
| 25 | onPressed: () => presentProviderPicker(context), |
| 26 | style: ButtonStyle( |
| 27 | padding: WidgetStateProperty.all(EdgeInsets.zero), |
| 28 | splashFactory: NoSplash.splashFactory, |
| 29 | foregroundColor: WidgetStateProperty.all(Colors.transparent), |
| 30 | overlayColor: WidgetStateProperty.all(Colors.transparent), |
| 31 | ), |
| 32 | child: Row( |
| 33 | mainAxisSize: MainAxisSize.min, |
| 34 | crossAxisAlignment: CrossAxisAlignment.start, |
| 35 | children: <Widget>[ |
| 36 | Column( |
| 37 | crossAxisAlignment: CrossAxisAlignment.center, |
| 38 | mainAxisSize: MainAxisSize.min, |
| 39 | children: <Widget>[ |
| 40 | Text( |
| 41 | S.of(context).swap, |
| 42 | style: Theme.of(context).textTheme.bodyMedium?.copyWith( |
| 43 | fontSize: 16.0, |
| 44 | fontWeight: FontWeight.w600, |
| 45 | ), |
| 46 | ), |
| 47 | Observer( |
| 48 | builder: (_) => Text( |
| 49 | exchangeViewModel.selectedProviders.isEmpty |
| 50 | ? S.of(context).choose_one |
| 51 | : exchangeViewModel.selectedProviders.length > 1 |
| 52 | ? S.of(context).automatic |
| 53 | : exchangeViewModel.selectedProviders.first.title, |
| 54 | style: Theme.of(context).textTheme.bodySmall?.copyWith( |
| 55 | fontSize: 10.0, |
| 56 | fontWeight: FontWeight.w500, |
| 57 | color: Theme.of(context).colorScheme.onSurfaceVariant, |
| 58 | ), |
| 59 | ), |
| 60 | ) |
| 61 | ], |
| 62 | ), |
| 63 | SizedBox(width: 5), |
| 64 | Padding( |
| 65 | padding: EdgeInsets.only(top: 12), |
| 66 | child: arrowBottom, |
| 67 | ) |
| 68 | ], |
| 69 | ), |
| 70 | ), |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | void presentProviderPicker(BuildContext context) async { |
| 75 | await showPopUp<void>( |
| 76 | builder: (BuildContext popUpContext) => CheckBoxPicker( |
| 77 | items: exchangeViewModel.providerList |
| 78 | .map( |
| 79 | (e) => CheckBoxItem( |
| 80 | e.title, |
| 81 | exchangeViewModel.selectedProviders.contains(e), |
| 82 | isDisabled: !exchangeViewModel.providerList.contains(e), |
| 83 | ), |
| 84 | ) |
| 85 | .toList(), |
| 86 | title: S.of(context).change_exchange_provider, |
| 87 | onChanged: (int index, bool value) { |
| 88 | if (!exchangeViewModel.providerList[index].isAvailable) { |
| 89 | showPopUp<void>( |
| 90 | builder: (BuildContext popUpContext) => AlertWithOneAction( |
| 91 | alertTitle: 'Error', |
| 92 | alertContent: 'The exchange is blocked in your region.', |
| 93 | buttonText: S.of(context).ok, |
| 94 | buttonAction: () => Navigator.of(context).pop()), |
| 95 | context: context); |
| 96 | return; |
| 97 | } |
| 98 | if (value) { |
| 99 | exchangeViewModel.addExchangeProvider(exchangeViewModel.providerList[index]); |
| 100 | } else { |
| 101 | exchangeViewModel.removeExchangeProvider(exchangeViewModel.providerList[index]); |
| 102 | } |
| 103 | }, |
| 104 | ), |
| 105 | context: context, |
| 106 | ); |
| 107 | |
| 108 | exchangeViewModel.saveSelectedProviders(); |
| 109 | } |
| 110 | } |