Refactored: contact_list_screen.dart
This commit is contained in:
parent
7a361b256a
commit
22914aa1a5
|
@ -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<ContactListScreen> {
|
|||
}
|
||||
}
|
||||
|
||||
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<ContactListScreen> {
|
|||
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<bool>(
|
||||
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();
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
|
@ -93,9 +127,7 @@ class _ContactListScreenState extends State<ContactListScreen> {
|
|||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: () {
|
||||
if (kDebugMode) {
|
||||
print('Add New Contact');
|
||||
}
|
||||
_navigateToAddEditContact();
|
||||
},
|
||||
child: const Icon(Icons.add),
|
||||
),
|
||||
|
|
Loading…
Reference in New Issue