dev
dart 172 lines 5.98 KB
Raw
1 import 'package:cake_wallet/cake_pay/src/models/cake_pay_card.dart';
2 import 'package:cake_wallet/generated/i18n.dart';
3 import 'package:cake_wallet/src/widgets/number_text_fild_widget.dart';
4 import 'package:cake_wallet/typography.dart';
5 import 'package:cake_wallet/view_model/cake_pay/cake_pay_buy_card_view_model.dart';
6 import 'package:cake_wallet/view_model/dashboard/dropdown_filter_item_widget.dart';
7 import 'package:flutter/material.dart';
8 import 'package:flutter_mobx/flutter_mobx.dart';
9
10 class DenominationsAmountWidget extends StatefulWidget {
11 const DenominationsAmountWidget({
12 required this.fiatCurrency,
13 required this.denominations,
14 required this.amountFieldFocus,
15 required this.amountController,
16 required this.quantityFieldFocus,
17 required this.quantityController,
18 required this.cakePayBuyCardViewModel,
19 required this.onAmountChanged,
20 required this.onQuantityChanged,
21 });
22
23 final String fiatCurrency;
24 final List<Denomination> denominations;
25 final FocusNode amountFieldFocus;
26 final TextEditingController amountController;
27 final FocusNode quantityFieldFocus;
28 final TextEditingController quantityController;
29 final CakePayBuyCardViewModel cakePayBuyCardViewModel;
30 final Function(String) onAmountChanged;
31 final Function(int?) onQuantityChanged;
32
33 @override
34 State<DenominationsAmountWidget> createState() => _DenominationsAmountWidgetState();
35 }
36
37 class _DenominationsAmountWidgetState extends State<DenominationsAmountWidget> {
38 late (String, int?) _selected;
39
40 @override
41 void initState() {
42 super.initState();
43
44 final first = widget.denominations.first;
45 final amount = widget.amountController.text.isNotEmpty
46 ? widget.amountController.text
47 : first.value.toString();
48 _selected = (amount, first.cardId);
49 widget.cakePayBuyCardViewModel.selectedDenomination = _selected;
50
51 widget.amountController.text = _selected.$1;
52 widget.onAmountChanged(_selected.$1);
53 }
54
55 @override
56 Widget build(BuildContext context) {
57 return Container(
58 height: MediaQuery.of(context).size.height * 0.1,
59 child: Row(
60 crossAxisAlignment: CrossAxisAlignment.center,
61 children: [
62 Expanded(
63 flex: 8,
64 child: Column(
65 mainAxisSize: MainAxisSize.min,
66 children: [
67 DropdownFilterList(
68 items: widget.denominations.map((e) => e.value.toString()).toList(),
69 itemPrefix: widget.fiatCurrency,
70 selectedItem: _selected.$1,
71 onItemSelected: (value) {
72 setState(() => _selected = (
73 value,
74 widget.denominations.firstWhere((e) => e.value.toString() == value).cardId
75 ));
76 widget.amountController.text = value;
77 widget.onAmountChanged(value);
78 widget.cakePayBuyCardViewModel.selectedDenomination =
79 (_selected.$1, _selected.$2);
80 },
81 ),
82 const SizedBox(height: 4),
83 Container(
84 width: double.infinity,
85 decoration: BoxDecoration(
86 border: Border(
87 top: BorderSide(
88 width: 1.0,
89 color: Theme.of(context).colorScheme.onSurfaceVariant,
90 ),
91 ),
92 ),
93 child: Text(
94 S.of(context).value,
95 maxLines: 2,
96 style: textSmall(color: Theme.of(context).colorScheme.onSurfaceVariant),
97 ),
98 ),
99 ],
100 ),
101 ),
102 Spacer(),
103 Expanded(
104 flex: 5,
105 child: Column(
106 mainAxisSize: MainAxisSize.min,
107 children: [
108 NumberTextField(
109 controller: widget.quantityController,
110 focusNode: widget.quantityFieldFocus,
111 min: 1,
112 max: 99,
113 onChanged: (value) => widget.onQuantityChanged(value),
114 ),
115 const SizedBox(height: 4),
116 Container(
117 width: double.infinity,
118 decoration: BoxDecoration(
119 border: Border(
120 top: BorderSide(
121 width: 1.0,
122 color: Theme.of(context).colorScheme.onSurfaceVariant,
123 ),
124 ),
125 ),
126 child: Text(
127 S.of(context).quantity,
128 maxLines: 1,
129 style: textSmall(color: Theme.of(context).colorScheme.onSurfaceVariant),
130 ),
131 ),
132 ],
133 ),
134 ),
135 Spacer(),
136 Expanded(
137 flex: 8,
138 child: Column(
139 mainAxisSize: MainAxisSize.min,
140 children: [
141 Observer(
142 builder: (_) => Text(
143 '${widget.fiatCurrency} ${widget.cakePayBuyCardViewModel.totalAmount}',
144 maxLines: 1,
145 style: Theme.of(context).textTheme.titleMedium!,
146 ),
147 ),
148 const SizedBox(height: 4),
149 Container(
150 width: double.infinity,
151 decoration: BoxDecoration(
152 border: Border(
153 top: BorderSide(
154 width: 1.0,
155 color: Theme.of(context).colorScheme.onSurfaceVariant,
156 ),
157 ),
158 ),
159 child: Text(
160 S.of(context).total,
161 maxLines: 1,
162 style: textSmall(color: Theme.of(context).colorScheme.onSurfaceVariant),
163 ),
164 ),
165 ],
166 ),
167 ),
168 ],
169 ),
170 );
171 }
172 }