Added: contact_list_screen.dart

This commit is contained in:
Hadi Mottale 2025-06-11 11:34:00 +03:30
parent 568a923982
commit 4b31ec316a
1 changed files with 95 additions and 0 deletions

View File

@ -0,0 +1,95 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:contacts_dataist_ir/models/contact.dart';
import 'package:contacts_dataist_ir/database/database_helper.dart';
class ContactListScreen extends StatefulWidget {
const ContactListScreen({super.key});
@override
State<ContactListScreen> createState() => _ContactListScreenState();
}
class _ContactListScreenState extends State<ContactListScreen> {
final DatabaseHelper _dbHelper = DatabaseHelper();
List<Contact> _contacts = [];
bool _isLoading = true;
@override
void initState() {
super.initState();
_loadContacts();
}
Future<void> _loadContacts() async {
setState(() {
_isLoading = true;
});
try {
final contacts = await _dbHelper.getContacts();
setState(() {
_contacts = contacts;
});
} catch (e) {
if (kDebugMode) {
print('Error loading contacts: $e');
}
} finally {
setState(() {
_isLoading = false;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Contacts Dataist IR'),
backgroundColor: Theme.of(context).colorScheme.primary,
),
body: _isLoading
? const Center(child: CircularProgressIndicator())
: _contacts.isEmpty
? const Center(
child: Text('No contacts found. Add a new one!', style: TextStyle(fontSize: 18)),
)
: ListView.builder(
itemCount: _contacts.length,
itemBuilder: (context, index) {
final contact = _contacts[index];
return Card(
margin: const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
child: ListTile(
title: Text(contact.name),
subtitle: Text(contact.phone ?? contact.email ?? 'No phone or email'),
onTap: () {
if (kDebugMode) {
print('Tapped on ${contact.name}');
}
},
trailing: IconButton(
icon: const Icon(Icons.delete, color: Colors.red),
onPressed: () async {
if (kDebugMode) {
print('Delete ${contact.name}');
}
await _dbHelper.deleteContact(contact.id!);
_loadContacts();
},
),
),
);
},
),
floatingActionButton: FloatingActionButton(
onPressed: () {
if (kDebugMode) {
print('Add New Contact');
}
},
child: const Icon(Icons.add),
),
);
}
}