60 lines
1.8 KiB
Dart
60 lines
1.8 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_contacts/flutter_contacts.dart';
|
|
import 'package:qr_flutter/qr_flutter.dart';
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
// Service to manage contact-related operations
|
|
class ContactService {
|
|
Future<List<Contact>> fetchContacts() async {
|
|
final hasPermission = await Permission.contacts.status;
|
|
if (!hasPermission.isGranted) {
|
|
return [];
|
|
}
|
|
|
|
try {
|
|
return await FlutterContacts.getContacts(
|
|
withProperties: true,
|
|
withThumbnail: true,
|
|
withAccounts: true,
|
|
withGroups: true,
|
|
withPhoto: true);
|
|
} catch (e) {
|
|
debugPrint('Error fetching contacts: $e');
|
|
return [];
|
|
}
|
|
}
|
|
|
|
Future<List<Contact>> fetchFavoriteContacts() async {
|
|
List<Contact> contacts = await fetchContacts();
|
|
return contacts.where((contact) => contact.isStarred).toList();
|
|
}
|
|
|
|
Future<void> addNewContact(Contact contact) async {
|
|
await FlutterContacts.insertContact(contact);
|
|
}
|
|
|
|
// Function to show an AlertDialog with a QR code for the contact's vCard
|
|
void showContactQRCodeDialog(BuildContext context, Contact contact) {
|
|
showDialog(
|
|
barrierColor: Colors.white24,
|
|
context: context,
|
|
barrierDismissible: true,
|
|
builder: (BuildContext context) {
|
|
return AlertDialog(
|
|
backgroundColor: Colors.black,
|
|
content: SizedBox(
|
|
width: 200,
|
|
height: 220,
|
|
child: QrImageView(
|
|
data: contact.toVCard(), // Generate vCard QR code
|
|
version: QrVersions.auto,
|
|
backgroundColor: Colors.white, // Make sure QR code is visible on black background
|
|
size: 200.0,
|
|
),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|