| 1 | import 'package:cake_wallet/entities/new_ui_entities/list_item/list_item_text_field.dart'; |
| 2 | import 'package:cake_wallet/generated/i18n.dart'; |
| 3 | import 'package:cake_wallet/new-ui/widgets/new_primary_button.dart'; |
| 4 | import 'package:cake_wallet/new-ui/widgets/receive_page/receive_top_bar.dart'; |
| 5 | import 'package:cake_wallet/src/widgets/new_list_row/new_list_section.dart'; |
| 6 | import 'package:cake_wallet/view_model/wallet_address_list/wallet_address_edit_or_create_view_model.dart'; |
| 7 | import 'package:cw_core/utils/print_verbose.dart'; |
| 8 | import 'package:flutter/material.dart'; |
| 9 | import 'package:flutter_mobx/flutter_mobx.dart'; |
| 10 | import 'package:mobx/mobx.dart'; |
| 11 | |
| 12 | class ReceiveLabelModal extends StatefulWidget { |
| 13 | const ReceiveLabelModal({super.key, required this.walletAddressEditOrCreateViewModel}); |
| 14 | |
| 15 | final WalletAddressEditOrCreateViewModel walletAddressEditOrCreateViewModel; |
| 16 | |
| 17 | @override |
| 18 | State<ReceiveLabelModal> createState() => _ReceiveLabelModalState(); |
| 19 | } |
| 20 | |
| 21 | class _ReceiveLabelModalState extends State<ReceiveLabelModal> { |
| 22 | late final TextEditingController _controller; |
| 23 | |
| 24 | @override |
| 25 | void initState() { |
| 26 | super.initState(); |
| 27 | _controller = TextEditingController(); |
| 28 | WidgetsBinding.instance.addPostFrameCallback((_) { |
| 29 | _controller.text = widget.walletAddressEditOrCreateViewModel.label; |
| 30 | }); |
| 31 | reaction((_) => widget.walletAddressEditOrCreateViewModel.state, |
| 32 | (AddressEditOrCreateState state) { |
| 33 | printV(state); |
| 34 | if (state is AddressSavedSuccessfully) { |
| 35 | WidgetsBinding.instance.addPostFrameCallback((_) { |
| 36 | if (mounted && Navigator.canPop(context)) { |
| 37 | Navigator.of(context).pop(widget.walletAddressEditOrCreateViewModel.label); |
| 38 | } |
| 39 | }); |
| 40 | } |
| 41 | }); |
| 42 | } |
| 43 | |
| 44 | @override |
| 45 | Widget build(BuildContext context) { |
| 46 | List<String> defaultLabels = [ |
| 47 | S.of(context).donation, |
| 48 | S.of(context).savings, |
| 49 | S.of(context).business, |
| 50 | S.of(context).mining, |
| 51 | S.of(context).salary |
| 52 | ]; |
| 53 | |
| 54 | return Padding( |
| 55 | padding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom), |
| 56 | child: Container( |
| 57 | decoration: BoxDecoration( |
| 58 | color: Theme.of(context).colorScheme.surface, |
| 59 | borderRadius: BorderRadius.vertical(top: Radius.circular(16))), |
| 60 | child: SafeArea( |
| 61 | top: false, |
| 62 | child: Column( |
| 63 | spacing: 24, |
| 64 | mainAxisSize: MainAxisSize.min, |
| 65 | children: [ |
| 66 | ModalTopBar( |
| 67 | title: S.of(context).label_address, |
| 68 | leadingIcon: Icon(Icons.close), |
| 69 | leadingSemanticLabel: S.of(context).close, |
| 70 | onLeadingPressed: Navigator.of(context).pop, |
| 71 | onTrailingPressed: () {}), |
| 72 | Padding( |
| 73 | padding: const EdgeInsets.symmetric(horizontal: 18.0), |
| 74 | child: Column(spacing: 24, children: [ |
| 75 | Text( |
| 76 | S.of(context).address_label_explainer, |
| 77 | textAlign: TextAlign.center, |
| 78 | style: TextStyle( |
| 79 | fontSize: 12, color: Theme.of(context).colorScheme.onSurfaceVariant), |
| 80 | ), |
| 81 | NewListSections(sections: { |
| 82 | "": [ListItemTextField(keyValue: "label", label: S.of(context).label)] |
| 83 | }, controllers: { |
| 84 | "label": _controller |
| 85 | }), |
| 86 | SizedBox( |
| 87 | height: 36, |
| 88 | child: ListView.separated( |
| 89 | scrollDirection: Axis.horizontal, |
| 90 | itemCount: defaultLabels.length, |
| 91 | separatorBuilder: (context, index) { |
| 92 | return SizedBox(width: 8); |
| 93 | }, |
| 94 | itemBuilder: (context, index) { |
| 95 | return MergeSemantics( |
| 96 | child: Semantics( |
| 97 | button: true, |
| 98 | child: Container( |
| 99 | height: 36, |
| 100 | decoration: BoxDecoration( |
| 101 | color: Theme.of(context).colorScheme.surfaceContainerHigh, |
| 102 | borderRadius: BorderRadius.circular(999)), |
| 103 | child: Material( |
| 104 | color: Colors.transparent, |
| 105 | borderRadius: BorderRadius.circular(999), |
| 106 | child: InkWell( |
| 107 | borderRadius: BorderRadius.circular(999), |
| 108 | onTap: () { |
| 109 | _controller.text = defaultLabels[index]; |
| 110 | }, |
| 111 | child: Padding( |
| 112 | padding: const EdgeInsets.symmetric(horizontal: 10.0), |
| 113 | child: Center( |
| 114 | child: Text( |
| 115 | defaultLabels[index], |
| 116 | style: TextStyle( |
| 117 | fontWeight: FontWeight.w400, |
| 118 | color: Theme.of(context).colorScheme.primary), |
| 119 | ), |
| 120 | ), |
| 121 | ), |
| 122 | ), |
| 123 | ), |
| 124 | ), |
| 125 | ), |
| 126 | ); |
| 127 | }), |
| 128 | ), |
| 129 | Observer( |
| 130 | builder: (_) { |
| 131 | final isSaving = |
| 132 | (widget.walletAddressEditOrCreateViewModel.state is AddressIsSaving); |
| 133 | |
| 134 | return NewPrimaryButton( |
| 135 | isLoading: isSaving, |
| 136 | onPressed: () { |
| 137 | if (isSaving) return; |
| 138 | widget.walletAddressEditOrCreateViewModel.label = _controller.text; |
| 139 | widget.walletAddressEditOrCreateViewModel.save(); |
| 140 | }, |
| 141 | text: S.of(context).continue_text, |
| 142 | color: Theme.of(context).colorScheme.primary, |
| 143 | textColor: Theme.of(context).colorScheme.onPrimary); |
| 144 | }, |
| 145 | ) |
| 146 | ]), |
| 147 | ), |
| 148 | SizedBox() |
| 149 | ], |
| 150 | ), |
| 151 | )), |
| 152 | ); |
| 153 | } |
| 154 | } |