NEED to handle "already existing contact"
This commit is contained in:
parent
448edeb918
commit
8673ed3b0b
@ -102,6 +102,25 @@ class _ContactStateState extends State<ContactState> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool doesContactExist(Contact contact) {
|
||||||
|
// Example: consider it "existing" if there's a matching phone number
|
||||||
|
for (final existing in _allContacts) {
|
||||||
|
if (existing.toVCard() == contact.toVCard()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// for (final phone in existing.phones) {
|
||||||
|
// for (final newPhone in contact.phones) {
|
||||||
|
// // Simple exact match; you can do more advanced logic
|
||||||
|
// if (phone.normalizedNumber == newPhone.normalizedNumber) {
|
||||||
|
// return true;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// } We might switch to finer and smarter logic later, ex: remove trailing spaces, capitals
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return _InheritedContactState(
|
return _InheritedContactState(
|
||||||
|
@ -3,38 +3,7 @@ import 'package:flutter/material.dart';
|
|||||||
import 'package:flutter_contacts/flutter_contacts.dart';
|
import 'package:flutter_contacts/flutter_contacts.dart';
|
||||||
|
|
||||||
class AddContactButton extends StatelessWidget {
|
class AddContactButton extends StatelessWidget {
|
||||||
const AddContactButton({Key? key}) : super(key: key);
|
const AddContactButton({super.key});
|
||||||
|
|
||||||
|
|
||||||
/// Create the contact from vCard, insert it, and open the system Contacts app in edit mode.
|
|
||||||
Future<void> createContactFromVCardAndOpenEditor(String vCardString) async {
|
|
||||||
// Request permission first
|
|
||||||
final granted = await FlutterContacts.requestPermission(readonly: false);
|
|
||||||
if (!granted) {
|
|
||||||
debugPrint('Contacts permission denied');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a contact from vCard data
|
|
||||||
final newContact = Contact.fromVCard(vCardString);
|
|
||||||
|
|
||||||
// Insert it into the device's contacts
|
|
||||||
await newContact.insert();
|
|
||||||
|
|
||||||
// Open the system Contacts app with this contact in edit mode
|
|
||||||
final edited = await FlutterContacts.openExternalEdit(newContact.id);
|
|
||||||
if (edited == null) {
|
|
||||||
debugPrint(" ____ Could NOT edit contact !");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> createNewContactBlank() async {
|
|
||||||
// For a blank contact, we can rely on flutter_contacts to do an external insert:
|
|
||||||
// But if we specifically want the system's default "insert" flow, we can do:
|
|
||||||
// `await FlutterContacts.openExternalInsert();`
|
|
||||||
// or rely on your old Android-intent approach.
|
|
||||||
await FlutterContacts.openExternalInsert();
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
@ -62,9 +31,9 @@ class AddContactButton extends StatelessWidget {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
// If we got a vCard string back
|
|
||||||
if (vCardString != null && vCardString is String) {
|
if (vCardString != null && vCardString is String) {
|
||||||
await createContactFromVCardAndOpenEditor(vCardString);
|
await FlutterContacts.openExternalInsert(Contact
|
||||||
|
.fromVCard(vCardString));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
child: const Text(
|
child: const Text(
|
||||||
@ -76,7 +45,7 @@ class AddContactButton extends StatelessWidget {
|
|||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
Navigator.of(context).pop();
|
Navigator.of(context).pop();
|
||||||
// Create a blank contact entry
|
// Create a blank contact entry
|
||||||
await createNewContactBlank();
|
await FlutterContacts.openExternalInsert();
|
||||||
},
|
},
|
||||||
child: const Text(
|
child: const Text(
|
||||||
"Create new contact",
|
"Create new contact",
|
||||||
|
Loading…
Reference in New Issue
Block a user