Added: add_edit_contact_screen.dart
This commit is contained in:
parent
9fd956814d
commit
7a361b256a
|
@ -0,0 +1,133 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:contacts_dataist_ir/models/contact.dart';
|
||||
import 'package:contacts_dataist_ir/database/database_helper.dart';
|
||||
|
||||
class AddEditContactScreen extends StatefulWidget {
|
||||
final Contact? contact; // Optional: if editing an existing contact
|
||||
|
||||
const AddEditContactScreen({super.key, this.contact});
|
||||
|
||||
@override
|
||||
State<AddEditContactScreen> createState() => _AddEditContactScreenState();
|
||||
}
|
||||
|
||||
class _AddEditContactScreenState extends State<AddEditContactScreen> {
|
||||
final _formKey = GlobalKey<FormState>(); // Key for form validation
|
||||
final DatabaseHelper _dbHelper = DatabaseHelper();
|
||||
|
||||
late TextEditingController _nameController;
|
||||
late TextEditingController _phoneController;
|
||||
late TextEditingController _emailController;
|
||||
late TextEditingController _addressController;
|
||||
late TextEditingController _companyController;
|
||||
late TextEditingController _notesController;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_nameController = TextEditingController(text: widget.contact?.name ?? '');
|
||||
_phoneController = TextEditingController(text: widget.contact?.phone ?? '');
|
||||
_emailController = TextEditingController(text: widget.contact?.email ?? '');
|
||||
_addressController = TextEditingController(text: widget.contact?.address ?? '');
|
||||
_companyController = TextEditingController(text: widget.contact?.company ?? '');
|
||||
_notesController = TextEditingController(text: widget.contact?.notes ?? '');
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_nameController.dispose();
|
||||
_phoneController.dispose();
|
||||
_emailController.dispose();
|
||||
_addressController.dispose();
|
||||
_companyController.dispose();
|
||||
_notesController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _saveContact() async {
|
||||
if (_formKey.currentState!.validate()) {
|
||||
final isEditing = widget.contact != null;
|
||||
final Contact contact = Contact(
|
||||
id: isEditing ? widget.contact!.id : null,
|
||||
name: _nameController.text,
|
||||
phone: _phoneController.text.isEmpty ? null : _phoneController.text,
|
||||
email: _emailController.text.isEmpty ? null : _emailController.text,
|
||||
address: _addressController.text.isEmpty ? null : _addressController.text,
|
||||
company: _companyController.text.isEmpty ? null : _companyController.text,
|
||||
notes: _notesController.text.isEmpty ? null : _notesController.text,
|
||||
);
|
||||
|
||||
try {
|
||||
if (isEditing) {
|
||||
await _dbHelper.updateContact(contact);
|
||||
} else {
|
||||
await _dbHelper.insertContact(contact);
|
||||
}
|
||||
if (mounted) { // Check if the widget is still mounted before navigating
|
||||
Navigator.pop(context, true); // Pop with 'true' indicating success
|
||||
}
|
||||
} catch (e) {
|
||||
print('Error saving contact: $e');
|
||||
// TODO: Show an error message to the user
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(widget.contact == null ? 'Add New Contact' : 'Edit Contact'),
|
||||
backgroundColor: Theme.of(context).colorScheme.primary,
|
||||
),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: ListView(
|
||||
children: <Widget>[
|
||||
TextFormField(
|
||||
controller: _nameController,
|
||||
decoration: const InputDecoration(labelText: 'Name *'),
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter a name';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
TextFormField(
|
||||
controller: _phoneController,
|
||||
decoration: const InputDecoration(labelText: 'Phone'),
|
||||
keyboardType: TextInputType.phone,
|
||||
),
|
||||
TextFormField(
|
||||
controller: _emailController,
|
||||
decoration: const InputDecoration(labelText: 'Email'),
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
),
|
||||
TextFormField(
|
||||
controller: _addressController,
|
||||
decoration: const InputDecoration(labelText: 'Address'),
|
||||
),
|
||||
TextFormField(
|
||||
controller: _companyController,
|
||||
decoration: const InputDecoration(labelText: 'Company'),
|
||||
),
|
||||
TextFormField(
|
||||
controller: _notesController,
|
||||
decoration: const InputDecoration(labelText: 'Notes'),
|
||||
maxLines: 3,
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
ElevatedButton(
|
||||
onPressed: _saveContact,
|
||||
child: Text(widget.contact == null ? 'Save Contact' : 'Update Contact'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue