87 lines
2.4 KiB
Dart
87 lines
2.4 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_contacts/flutter_contacts.dart';
|
|
import 'package:qr_flutter/qr_flutter.dart';
|
|
|
|
class ContactService {
|
|
Future<List<Contact>> fetchContacts() async {
|
|
if (await FlutterContacts.requestPermission()) {
|
|
List<Contact> contacts = await FlutterContacts.getContacts(
|
|
withProperties: true,
|
|
withThumbnail: true,
|
|
);
|
|
return contacts;
|
|
} else {
|
|
// Permission denied
|
|
return [];
|
|
}
|
|
}
|
|
|
|
Future<List<Contact>> fetchFavoriteContacts() async {
|
|
if (await FlutterContacts.requestPermission()) {
|
|
// Get all contacts and filter for favorites
|
|
List<Contact> allContacts = await FlutterContacts.getContacts(
|
|
withProperties: true,
|
|
withThumbnail: true,
|
|
);
|
|
return allContacts.where((c) => c.isStarred).toList();
|
|
} else {
|
|
// Permission denied
|
|
return [];
|
|
}
|
|
}
|
|
|
|
Future<Contact?> addNewContact(Contact contact) async {
|
|
if (await FlutterContacts.requestPermission()) {
|
|
try {
|
|
return await FlutterContacts.insertContact(contact);
|
|
} catch (e) {
|
|
debugPrint('Error adding contact: $e');
|
|
return null;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
void showContactQRCodeDialog(BuildContext context, Contact contact) {
|
|
final String vCard = contact.toVCard();
|
|
|
|
showDialog(
|
|
context: context,
|
|
builder: (BuildContext context) {
|
|
return AlertDialog(
|
|
backgroundColor: Colors.grey[900],
|
|
title: Text(
|
|
'QR Code for ${contact.displayName}',
|
|
style: const TextStyle(color: Colors.white),
|
|
),
|
|
content: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Container(
|
|
color: Colors.white,
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: QrImageView(
|
|
data: vCard,
|
|
version: QrVersions.auto,
|
|
size: 200.0,
|
|
),
|
|
),
|
|
const SizedBox(height: 16.0),
|
|
const Text(
|
|
'Scan this code to add this contact',
|
|
style: TextStyle(color: Colors.white70),
|
|
),
|
|
],
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.of(context).pop(),
|
|
child: const Text('Close'),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|