Refactored: contact_list_screen.dart

This commit is contained in:
Hadi Mottale 2025-06-11 16:23:19 +03:30
parent 7a361b256a
commit 22914aa1a5
1 changed files with 42 additions and 10 deletions

View File

@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:contacts_dataist_ir/models/contact.dart'; import 'package:contacts_dataist_ir/models/contact.dart';
import 'package:contacts_dataist_ir/database/database_helper.dart'; import 'package:contacts_dataist_ir/database/database_helper.dart';
import 'package:contacts_dataist_ir/screens/add_edit_contact_screen.dart'; // Ensure this import is here
class ContactListScreen extends StatefulWidget { class ContactListScreen extends StatefulWidget {
const ContactListScreen({super.key}); const ContactListScreen({super.key});
@ -41,6 +42,19 @@ class _ContactListScreenState extends State<ContactListScreen> {
} }
} }
void _navigateToAddEditContact({Contact? contact}) async {
final result = await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => AddEditContactScreen(contact: contact),
),
);
if (result == true) {
_loadContacts();
}
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
@ -73,18 +87,38 @@ class _ContactListScreenState extends State<ContactListScreen> {
contact.phone ?? contact.email ?? 'No phone or email', contact.phone ?? contact.email ?? 'No phone or email',
), ),
onTap: () { onTap: () {
if (kDebugMode) { _navigateToAddEditContact(contact: contact);
print('Tapped on ${contact.name}');
}
}, },
trailing: IconButton( trailing: IconButton(
icon: const Icon(Icons.delete, color: Colors.red), icon: const Icon(Icons.delete, color: Colors.red),
onPressed: () async { onPressed: () async {
if (kDebugMode) { final confirm = await showDialog<bool>(
print('Delete ${contact.name}'); context: context,
builder:
(context) => AlertDialog(
title: const Text('Delete Contact'),
content: Text(
'Are you sure you want to delete ${contact.name}?',
),
actions: [
TextButton(
onPressed:
() => Navigator.pop(context, false),
child: const Text('Cancel'),
),
TextButton(
onPressed:
() => Navigator.pop(context, true),
child: const Text('Delete'),
),
],
),
);
if (confirm == true) {
await _dbHelper.deleteContact(contact.id!);
_loadContacts();
} }
await _dbHelper.deleteContact(contact.id!);
_loadContacts();
}, },
), ),
), ),
@ -93,9 +127,7 @@ class _ContactListScreenState extends State<ContactListScreen> {
), ),
floatingActionButton: FloatingActionButton( floatingActionButton: FloatingActionButton(
onPressed: () { onPressed: () {
if (kDebugMode) { _navigateToAddEditContact();
print('Add New Contact');
}
}, },
child: const Icon(Icons.add), child: const Icon(Icons.add),
), ),