CakeWallet/lib/src/screens/exchange/widgets/exchange_card.dart

512 lines
21 KiB
Dart
Raw Normal View History

import 'package:cake_wallet/entities/contact_base.dart';
import 'package:cake_wallet/routes.dart';
import 'package:cake_wallet/src/widgets/alert_with_two_actions.dart';
2020-09-30 21:23:15 +03:00
import 'package:cake_wallet/utils/show_bar.dart';
2020-09-25 18:32:44 +03:00
import 'package:cake_wallet/utils/show_pop_up.dart';
import 'package:cake_wallet/utils/payment_request.dart';
2020-01-04 21:31:52 +02:00
import 'package:flutter/services.dart';
import 'package:flutter/material.dart';
import 'package:cake_wallet/generated/i18n.dart';
2021-12-24 14:37:24 +02:00
import 'package:cw_core/crypto_currency.dart';
2020-01-04 21:31:52 +02:00
import 'package:cake_wallet/src/widgets/address_text_field.dart';
import 'package:cake_wallet/src/widgets/base_text_form_field.dart';
import 'package:cake_wallet/src/screens/exchange/widgets/currency_picker.dart';
2020-01-04 21:31:52 +02:00
class ExchangeCard extends StatefulWidget {
ExchangeCard(
{Key key,
this.title = '',
2020-01-04 21:31:52 +02:00
this.initialCurrency,
this.initialAddress,
this.initialWalletName,
this.initialIsAmountEditable,
this.initialIsAddressEditable,
this.isAmountEstimated,
this.hasRefundAddress = false,
this.isMoneroWallet = false,
2020-01-04 21:31:52 +02:00
this.currencies,
this.onCurrencySelected,
this.imageArrow,
this.currencyButtonColor = Colors.transparent,
this.addressButtonsColor = Colors.transparent,
this.borderColor = Colors.transparent,
2020-01-04 21:31:52 +02:00
this.currencyValueValidator,
2020-09-29 20:56:11 +03:00
this.addressTextFieldValidator,
2021-01-05 20:31:03 +02:00
this.amountFocusNode,
this.addressFocusNode,
2021-01-05 20:31:03 +02:00
this.hasAllAmount = false,
this.allAmount,
this.onPushPasteButton,
this.onPushAddressBookButton})
2020-01-04 21:31:52 +02:00
: super(key: key);
2020-01-08 14:26:34 +02:00
final List<CryptoCurrency> currencies;
final Function(CryptoCurrency) onCurrencySelected;
final String title;
2020-01-08 14:26:34 +02:00
final CryptoCurrency initialCurrency;
final String initialWalletName;
final String initialAddress;
final bool initialIsAmountEditable;
final bool initialIsAddressEditable;
final bool isAmountEstimated;
final bool hasRefundAddress;
final bool isMoneroWallet;
2020-01-08 14:26:34 +02:00
final Image imageArrow;
final Color currencyButtonColor;
final Color addressButtonsColor;
final Color borderColor;
2020-01-08 14:26:34 +02:00
final FormFieldValidator<String> currencyValueValidator;
final FormFieldValidator<String> addressTextFieldValidator;
2020-09-29 20:56:11 +03:00
final FocusNode amountFocusNode;
final FocusNode addressFocusNode;
2021-01-05 20:31:03 +02:00
final bool hasAllAmount;
final Function allAmount;
final Function(BuildContext context) onPushPasteButton;
final Function(BuildContext context) onPushAddressBookButton;
2020-01-08 14:26:34 +02:00
2020-01-04 21:31:52 +02:00
@override
2020-01-08 14:26:34 +02:00
ExchangeCardState createState() => ExchangeCardState();
2020-01-04 21:31:52 +02:00
}
class ExchangeCardState extends State<ExchangeCard> {
final addressController = TextEditingController();
final amountController = TextEditingController();
String _title;
2020-01-04 21:31:52 +02:00
String _min;
String _max;
CryptoCurrency _selectedCurrency;
String _walletName;
bool _isAmountEditable;
bool _isAddressEditable;
bool _isAmountEstimated;
bool _isMoneroWallet;
2020-01-04 21:31:52 +02:00
@override
void initState() {
_title = widget.title;
2020-01-04 21:31:52 +02:00
_isAmountEditable = widget.initialIsAmountEditable;
_isAddressEditable = widget.initialIsAddressEditable;
_walletName = widget.initialWalletName;
_selectedCurrency = widget.initialCurrency;
_isAmountEstimated = widget.isAmountEstimated;
_isMoneroWallet = widget.isMoneroWallet;
2020-01-04 21:31:52 +02:00
addressController.text = widget.initialAddress;
super.initState();
}
void changeLimits({String min, String max}) {
setState(() {
_min = min;
_max = max;
});
}
void changeSelectedCurrency(CryptoCurrency currency) {
setState(() => _selectedCurrency = currency);
}
void changeWalletName(String walletName) {
setState(() => _walletName = walletName);
}
void changeIsAction(bool isActive) {
setState(() => _isAmountEditable = isActive);
}
void isAmountEditable({bool isEditable = true}) {
setState(() => _isAmountEditable = isEditable);
}
void isAddressEditable({bool isEditable = true}) {
setState(() => _isAddressEditable = isEditable);
}
void changeAddress({String address}) {
setState(() => addressController.text = address);
}
void changeAmount({String amount}) {
setState(() => amountController.text = amount);
}
void changeIsAmountEstimated(bool isEstimated) {
setState(() => _isAmountEstimated = isEstimated);
}
@override
Widget build(BuildContext context) {
final copyImage = Image.asset('assets/images/copy_content.png',
2020-09-29 20:56:11 +03:00
height: 16,
width: 16,
color: Theme.of(context).primaryTextTheme.display2.color);
2020-01-04 21:31:52 +02:00
return Container(
width: double.infinity,
color: Colors.transparent,
2020-09-29 20:56:11 +03:00
child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: <
Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Text(
_title,
style: TextStyle(
2020-09-29 20:56:11 +03:00
fontSize: 18,
fontWeight: FontWeight.w600,
color: Theme.of(context).textTheme.headline.color),
)
],
),
Padding(
2020-09-29 20:56:11 +03:00
padding: EdgeInsets.only(top: 20),
child: Row(
children: [
Container(
padding: EdgeInsets.only(right: 8),
height: 32,
color: widget.currencyButtonColor,
child: InkWell(
onTap: () => _presentPicker(context),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Padding(
padding: EdgeInsets.only(right: 5),
child: widget.imageArrow,
),
Text(_selectedCurrency.toString(),
style: TextStyle(
fontWeight: FontWeight.w600,
fontSize: 16,
color: Colors.white))
]),
2020-01-04 21:31:52 +02:00
),
2021-01-05 20:31:03 +02:00
),
_selectedCurrency.tag != null ? Padding(
padding: const EdgeInsets.only(right:3.0),
child: Container(
height: 32,
decoration: BoxDecoration(
color: widget.addressButtonsColor ?? Theme.of(context)
.primaryTextTheme
.display1
.color,
borderRadius:
BorderRadius.all(Radius.circular(6))),
child: Center(
child: Padding(
padding: const EdgeInsets.all(6.0),
child: Text(_selectedCurrency.tag,
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.bold,
color: Theme.of(context)
.primaryTextTheme
.display1
.decorationColor)),
),
),
),
) : Container(),
Padding(
padding: const EdgeInsets.only(right: 4.0),
child: Text(':',
style: TextStyle(
fontWeight: FontWeight.w600,
fontSize: 16,
color: Colors.white)),
),
Expanded(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Flexible(
child: BaseTextFormField(
focusNode: widget.amountFocusNode,
controller: amountController,
enabled: _isAmountEditable,
textAlign: TextAlign.left,
keyboardType: TextInputType.numberWithOptions(
signed: false, decimal: true),
inputFormatters: [
FilteringTextInputFormatter.deny(
RegExp('[\\-|\\ ]'))
],
hintText: '0.0000',
borderColor: Colors.transparent,
//widget.borderColor,
textStyle: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
color: Colors.white),
placeholderTextStyle: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
color: Theme.of(context)
.accentTextTheme
.display4
.decorationColor),
validator: _isAmountEditable
? widget.currencyValueValidator
: null),
),
if (widget.hasAllAmount)
Container(
height: 32,
width: 32,
decoration: BoxDecoration(
color: Theme.of(context)
.primaryTextTheme
.display1
.color,
borderRadius:
2022-02-07 00:17:39 +02:00
BorderRadius.all(Radius.circular(6))),
child: InkWell(
onTap: () => widget.allAmount?.call(),
child: Center(
child: Text(S.of(context).all,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.bold,
color: Theme.of(context)
.primaryTextTheme
.display1
.decorationColor)),
),
2021-01-05 20:31:03 +02:00
),
)
],
),
),
2020-09-29 20:56:11 +03:00
],
)),
2022-02-09 13:43:22 +02:00
Divider(height: 1,color: Theme.of(context)
.primaryTextTheme
.headline
.decorationColor),
Padding(
padding: EdgeInsets.only(top: 5),
2021-01-11 19:15:27 +02:00
child: Container(
height: 15,
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
_min != null
? Text(
S
.of(context)
.min_value(_min, _selectedCurrency.toString()),
style: TextStyle(
fontSize: 10,
height: 1.2,
color: Theme.of(context)
.accentTextTheme
.display4
.decorationColor),
)
: Offstage(),
_min != null ? SizedBox(width: 10) : Offstage(),
_max != null
? Text(
S
.of(context)
.max_value(_max, _selectedCurrency.toString()),
style: TextStyle(
fontSize: 10,
height: 1.2,
color: Theme.of(context)
.accentTextTheme
.display4
.decorationColor))
: Offstage(),
])),
),
!_isAddressEditable && widget.hasRefundAddress
? Padding(
2021-01-05 20:31:03 +02:00
padding: EdgeInsets.only(top: 20),
child: Text(
S.of(context).refund_address,
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w500,
color: Theme.of(context)
.accentTextTheme
.display4
.decorationColor),
))
: Offstage(),
_isAddressEditable
2020-09-29 20:56:11 +03:00
? Padding(
padding: EdgeInsets.only(top: 20),
child: AddressTextField(
focusNode: widget.addressFocusNode,
controller: addressController,
onURIScanned: (uri) {
final paymentRequest = PaymentRequest.fromUri(uri);
addressController.text = paymentRequest.address;
if (amountController.text.isNotEmpty) {
_showAmountPopup(context, paymentRequest);
2022-07-07 18:02:38 +03:00
return;
}
2022-07-07 18:02:38 +03:00
widget.amountFocusNode.requestFocus();
amountController.text = paymentRequest.amount;
},
placeholder: widget.hasRefundAddress
? S.of(context).refund_address
: null,
options: [
AddressTextFieldOption.paste,
AddressTextFieldOption.qrCode,
AddressTextFieldOption.addressBook,
],
isBorderExist: false,
textStyle: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
color: Colors.white),
hintStyle: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
color: Theme.of(context)
.accentTextTheme
.display4
.decorationColor),
buttonColor: widget.addressButtonsColor,
validator: widget.addressTextFieldValidator,
onPushPasteButton: widget.onPushPasteButton,
onPushAddressBookButton: widget.onPushAddressBookButton
),
)
2020-09-29 20:56:11 +03:00
: Padding(
padding: EdgeInsets.only(top: 10),
child: Builder(
builder: (context) => Stack(children: <Widget>[
BaseTextFormField(
controller: addressController,
readOnly: true,
borderColor: Colors.transparent,
suffixIcon:
SizedBox(width: _isMoneroWallet ? 80 : 36),
textStyle: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
color: Colors.white),
validator: widget.addressTextFieldValidator),
Positioned(
top: 2,
right: 0,
child: SizedBox(
width: _isMoneroWallet ? 80 : 36,
child: Row(children: <Widget>[
if (_isMoneroWallet)
Padding(
padding: EdgeInsets.only(left: 10),
child: Container(
width: 34,
height: 34,
padding: EdgeInsets.only(top: 0),
child: InkWell(
onTap: () async {
final contact =
await Navigator.of(context,
rootNavigator: true)
.pushNamed(Routes
.pickerAddressBook);
if (contact is ContactBase &&
contact.address != null) {
setState(() =>
addressController.text =
contact.address);
widget.onPushAddressBookButton
?.call(context);
}
},
child: Container(
padding: EdgeInsets.all(8),
decoration: BoxDecoration(
color: widget
.addressButtonsColor,
borderRadius:
BorderRadius.all(
Radius.circular(
6))),
child: Image.asset(
'assets/images/open_book.png',
color: Theme.of(context)
.primaryTextTheme
.display1
.decorationColor,
)),
)),
),
Padding(
padding: EdgeInsets.only(left: 2),
child: Container(
width: 34,
height: 34,
padding: EdgeInsets.only(top: 0),
child: InkWell(
onTap: () {
Clipboard.setData(ClipboardData(
text: addressController
.text));
showBar<void>(
context,
S
.of(context)
.copied_to_clipboard);
},
child: Container(
padding: EdgeInsets.fromLTRB(
8, 8, 0, 8),
color: Colors.transparent,
child: copyImage),
)))
])))
])),
2020-09-29 20:56:11 +03:00
),
2020-01-04 21:31:52 +02:00
]),
);
}
void _presentPicker(BuildContext context) {
2020-09-25 18:32:44 +03:00
showPopUp<void>(
builder: (_) => CurrencyPicker(
2020-01-04 21:31:52 +02:00
selectedAtIndex: widget.currencies.indexOf(_selectedCurrency),
items: widget.currencies,
hintText: S.of(context).search_currency,
isMoneroWallet: _isMoneroWallet,
isConvertFrom: widget.hasRefundAddress,
2020-01-08 14:26:34 +02:00
onItemSelected: (CryptoCurrency item) =>
2020-09-29 20:56:11 +03:00
widget.onCurrencySelected != null
? widget.onCurrencySelected(item)
: null),
2020-01-04 21:31:52 +02:00
context: context);
}
void _showAmountPopup(BuildContext context, PaymentRequest paymentRequest) {
showPopUp<void>(
context: context,
builder: (dialogContext) {
return AlertWithTwoActions(
alertTitle: S.of(context).overwrite_amount,
alertContent: S.of(context).qr_payment_amount,
rightButtonText: S.of(context).ok,
leftButtonText: S.of(context).cancel,
actionRightButton: () {
2022-07-07 18:02:38 +03:00
widget.amountFocusNode.requestFocus();
amountController.text = paymentRequest.amount;
Navigator.of(context).pop();
},
actionLeftButton: () => Navigator.of(dialogContext).pop());
}
);
}
2020-01-04 21:31:52 +02:00
}