95 lines
3.3 KiB
Dart
95 lines
3.3 KiB
Dart
import 'package:dialer/widgets/qr_scanner.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_contacts/flutter_contacts.dart';
|
|
|
|
class AddContactButton extends StatelessWidget {
|
|
const AddContactButton({Key? key}) : super(key: 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
|
|
Widget build(BuildContext context) {
|
|
return IconButton(
|
|
icon: const Icon(Icons.add, color: Colors.blue),
|
|
onPressed: () {
|
|
showDialog(
|
|
context: context,
|
|
barrierDismissible: true, // Allows dismissal by tapping outside
|
|
builder: (BuildContext context) {
|
|
return AlertDialog(
|
|
backgroundColor: Colors.black,
|
|
content: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
TextButton(
|
|
onPressed: () async {
|
|
Navigator.of(context).pop(); // close dialog
|
|
|
|
// Go to QR Scanner
|
|
final vCardString = await Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => const QRCodeScannerScreen(),
|
|
),
|
|
);
|
|
|
|
// If we got a vCard string back
|
|
if (vCardString != null && vCardString is String) {
|
|
await createContactFromVCardAndOpenEditor(vCardString);
|
|
}
|
|
},
|
|
child: const Text(
|
|
"Scan QR code",
|
|
style: TextStyle(color: Colors.white),
|
|
),
|
|
),
|
|
TextButton(
|
|
onPressed: () async {
|
|
Navigator.of(context).pop();
|
|
// Create a blank contact entry
|
|
await createNewContactBlank();
|
|
},
|
|
child: const Text(
|
|
"Create new contact",
|
|
style: TextStyle(color: Colors.white),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
},
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|