julian 2022-09-24 11:34:14 -06:00
parent 37f59f11bf
commit 750f36ca7b

View file

@ -16,6 +16,7 @@ import 'package:stackwallet/utilities/constants.dart';
import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/utilities/text_styles.dart';
import 'package:stackwallet/utilities/theme/stack_colors.dart'; import 'package:stackwallet/utilities/theme/stack_colors.dart';
import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart'; import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart';
import 'package:stackwallet/widgets/custom_buttons/blue_text_button.dart';
import 'package:stackwallet/widgets/emoji_select_sheet.dart'; import 'package:stackwallet/widgets/emoji_select_sheet.dart';
import 'package:stackwallet/widgets/icon_widgets/x_icon.dart'; import 'package:stackwallet/widgets/icon_widgets/x_icon.dart';
import 'package:stackwallet/widgets/stack_text_field.dart'; import 'package:stackwallet/widgets/stack_text_field.dart';
@ -73,10 +74,23 @@ class _AddAddressBookEntryViewState
} }
List<NewContactAddressEntryForm> forms = []; List<NewContactAddressEntryForm> forms = [];
int _formCount = 0; final Set<int> _formIds = {};
void _removeForm(int id) {
forms.retainWhere((e) => e.id != id);
_formIds.remove(id);
ref.refresh(addressEntryDataProvider(id));
setState(() {});
}
void _addForm() { void _addForm() {
int id = ++_formCount; int id = 0;
// ensure unique form id while allowing reuse of removed form ids
while (_formIds.contains(id)) {
id++;
}
_formIds.add(id);
forms.add( forms.add(
NewContactAddressEntryForm( NewContactAddressEntryForm(
key: Key("contactAddressEntryForm_$id"), key: Key("contactAddressEntryForm_$id"),
@ -303,22 +317,34 @@ class _AddAddressBookEntryViewState
}, },
), ),
), ),
if (_formCount <= 1) if (forms.length <= 1)
const SizedBox( const SizedBox(
height: 8, height: 8,
), ),
if (_formCount <= 1) forms[0], if (forms.length <= 1) forms[0],
if (_formCount > 1) if (forms.length > 1)
for (int i = 0; i < _formCount; i++) for (int i = 0; i < forms.length; i++)
Column( Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
const SizedBox( const SizedBox(
height: 12, height: 12,
), ),
Text( Row(
"Address ${i + 1}", mainAxisAlignment:
style: STextStyles.smallMed12(context), MainAxisAlignment.spaceBetween,
children: [
Text(
"Address ${i + 1}",
style: STextStyles.smallMed12(context),
),
BlueTextButton(
onTap: () {
_removeForm(forms[i].id);
},
text: "Remove",
),
],
), ),
const SizedBox( const SizedBox(
height: 8, height: 8,
@ -329,7 +355,7 @@ class _AddAddressBookEntryViewState
const SizedBox( const SizedBox(
height: 16, height: 16,
), ),
GestureDetector( BlueTextButton(
onTap: () { onTap: () {
_addForm(); _addForm();
scrollController.animateTo( scrollController.animateTo(
@ -338,11 +364,15 @@ class _AddAddressBookEntryViewState
curve: Curves.easeInOut, curve: Curves.easeInOut,
); );
}, },
child: Text( text: "+ Add another address",
"+ Add another address",
style: STextStyles.largeMedium14(context),
),
), ),
// GestureDetector(
//
// child: Text(
// "+ Add another address",
// style: STextStyles.largeMedium14(context),
// ),
// ),
const SizedBox( const SizedBox(
height: 16, height: 16,
), ),
@ -411,10 +441,12 @@ class _AddAddressBookEntryViewState
} }
List<ContactAddressEntry> entries = List<ContactAddressEntry> entries =
[]; [];
for (int i = 0; i < _formCount; i++) { for (int i = 0;
i < forms.length;
i++) {
entries.add(ref entries.add(ref
.read(addressEntryDataProvider( .read(addressEntryDataProvider(
i + 1)) forms[i].id))
.buildAddressEntry()); .buildAddressEntry());
} }
Contact contact = Contact( Contact contact = Contact(
@ -438,7 +470,15 @@ class _AddAddressBookEntryViewState
: null, : null,
child: Text( child: Text(
"Save", "Save",
style: STextStyles.button(context), style: STextStyles.button(context).copyWith(
color: shouldEnableSave
? Theme.of(context)
.extension<StackColors>()!
.buttonTextPrimary
: Theme.of(context)
.extension<StackColors>()!
.buttonTextPrimaryDisabled,
),
), ),
); );
}, },