From 22914aa1a5fcd937b1e2a3f773907f01591b58f2 Mon Sep 17 00:00:00 2001 From: Hadi Mottale Date: Wed, 11 Jun 2025 16:23:19 +0330 Subject: [PATCH] Refactored: contact_list_screen.dart --- lib/screens/contact_list_screen.dart | 52 ++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/lib/screens/contact_list_screen.dart b/lib/screens/contact_list_screen.dart index 0b03173..8777da0 100644 --- a/lib/screens/contact_list_screen.dart +++ b/lib/screens/contact_list_screen.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/foundation.dart'; import 'package:contacts_dataist_ir/models/contact.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 { const ContactListScreen({super.key}); @@ -41,6 +42,19 @@ class _ContactListScreenState extends State { } } + void _navigateToAddEditContact({Contact? contact}) async { + final result = await Navigator.push( + context, + MaterialPageRoute( + builder: (context) => AddEditContactScreen(contact: contact), + ), + ); + + if (result == true) { + _loadContacts(); + } + } + @override Widget build(BuildContext context) { return Scaffold( @@ -73,18 +87,38 @@ class _ContactListScreenState extends State { contact.phone ?? contact.email ?? 'No phone or email', ), onTap: () { - if (kDebugMode) { - print('Tapped on ${contact.name}'); - } + _navigateToAddEditContact(contact: contact); }, trailing: IconButton( icon: const Icon(Icons.delete, color: Colors.red), onPressed: () async { - if (kDebugMode) { - print('Delete ${contact.name}'); + final confirm = await showDialog( + 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 { ), floatingActionButton: FloatingActionButton( onPressed: () { - if (kDebugMode) { - print('Add New Contact'); - } + _navigateToAddEditContact(); }, child: const Icon(Icons.add), ),