2024-09-16 15:07:04 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2024-10-17 13:16:39 +00:00
|
|
|
import 'package:flutter_contacts/flutter_contacts.dart'; // Updated package
|
2024-09-16 15:07:04 +00:00
|
|
|
|
|
|
|
class ContactPage extends StatefulWidget {
|
|
|
|
const ContactPage({super.key});
|
|
|
|
|
|
|
|
@override
|
|
|
|
_ContactPageState createState() => _ContactPageState();
|
|
|
|
}
|
2024-10-17 13:16:39 +00:00
|
|
|
|
2024-09-16 15:07:04 +00:00
|
|
|
class _ContactPageState extends State<ContactPage> {
|
2024-10-17 13:16:39 +00:00
|
|
|
List<Contact> _contacts = [];
|
|
|
|
bool _loading = true;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
_fetchContacts();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request permission and fetch contacts
|
|
|
|
Future<void> _fetchContacts() async {
|
|
|
|
if (await FlutterContacts.requestPermission()) {
|
|
|
|
List<Contact> contacts = await FlutterContacts.getContacts(withProperties: true, withThumbnail: true);
|
|
|
|
setState(() {
|
|
|
|
_contacts = contacts;
|
|
|
|
_loading = false;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
setState(() {
|
|
|
|
_loading = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a new contact using flutter_contacts
|
|
|
|
Future<void> _addNewContact() async {
|
|
|
|
Contact newContact = Contact(
|
|
|
|
name: Name(first: 'John', last: 'Doe'),
|
|
|
|
phones: [Phone('123456789')],
|
|
|
|
);
|
|
|
|
await FlutterContacts.insertContact(newContact);
|
|
|
|
_fetchContacts(); // Refresh the contact list
|
|
|
|
}
|
|
|
|
|
2024-09-16 15:07:04 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
title: const Text('Contacts'),
|
|
|
|
),
|
2024-10-17 13:16:39 +00:00
|
|
|
body: _loading
|
|
|
|
? const Center(child: CircularProgressIndicator())
|
|
|
|
: _contacts.isEmpty
|
|
|
|
? const Center(child: Text('No contacts found'))
|
|
|
|
: ListView.builder(
|
|
|
|
itemCount: _contacts.length,
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
return ContactTile(contact: _contacts[index]);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
floatingActionButton: FloatingActionButton(
|
|
|
|
child: const Icon(Icons.add),
|
|
|
|
onPressed: _addNewContact,
|
2024-09-16 15:07:04 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-17 13:16:39 +00:00
|
|
|
// Contact Tile to display each contact
|
|
|
|
class ContactTile extends StatelessWidget {
|
|
|
|
final Contact contact;
|
2024-09-16 15:07:04 +00:00
|
|
|
|
2024-10-17 13:16:39 +00:00
|
|
|
const ContactTile({super.key, required this.contact});
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return ListTile(
|
|
|
|
leading: (contact.thumbnail != null)
|
|
|
|
? CircleAvatar(backgroundImage: MemoryImage(contact.thumbnail!))
|
|
|
|
: CircleAvatar(child: Text(_getInitials(contact.displayName))),
|
|
|
|
title: Text(contact.displayName ?? 'No Name'),
|
|
|
|
subtitle: contact.phones.isNotEmpty
|
|
|
|
? Text(contact.phones.first.number)
|
|
|
|
: const Text('No phone number'),
|
|
|
|
trailing: IconButton(
|
|
|
|
icon: const Icon(Icons.call),
|
|
|
|
onPressed: () {
|
|
|
|
// Handle call action
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
String _getInitials(String? name) {
|
|
|
|
if (name == null || name.isEmpty) return "";
|
|
|
|
List<String> names = name.split(' ');
|
|
|
|
return names.map((n) => n[0]).take(2).join().toUpperCase();
|
|
|
|
}
|
|
|
|
}
|