You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
157 lines
5.0 KiB
157 lines
5.0 KiB
import 'package:flutter/foundation.dart'; |
|
import 'package:flutter/material.dart'; |
|
import 'package:flutter/widgets.dart'; |
|
import 'package:image_picker/image_picker.dart'; |
|
import 'dart:io'; |
|
import 'package:http/http.dart' as http; |
|
import 'contactService.dart'; |
|
import 'model/contactCreation.dart'; |
|
import 'dart:convert'; |
|
import 'dart:typed_data'; |
|
|
|
class ContactCreationPage extends StatefulWidget{ |
|
late ContactService contactService; |
|
ContactCreationPage({Key? key, required this.contactService}) : super(key: key); |
|
|
|
@override |
|
_ContatcCreationPageState createState() => _ContatcCreationPageState(); |
|
} |
|
|
|
class _ContatcCreationPageState extends State<ContactCreationPage> { |
|
final TextEditingController _nameController = TextEditingController(); |
|
final TextEditingController _emailController = TextEditingController(); |
|
final TextEditingController _phoneController = TextEditingController(); |
|
final TextEditingController _streetController = TextEditingController(); |
|
final TextEditingController _street2Controller = TextEditingController(); |
|
final TextEditingController _cityController = TextEditingController(); |
|
final ImagePicker _picker = ImagePicker(); |
|
String? _image; |
|
|
|
@override |
|
Widget build(BuildContext context) { |
|
return Scaffold( |
|
appBar: AppBar( |
|
title: Text('Create Contact'), |
|
), |
|
body: SingleChildScrollView( |
|
child:Padding( |
|
padding: EdgeInsets.all(16.0), |
|
child: Column( |
|
crossAxisAlignment: CrossAxisAlignment.start, |
|
children: [ |
|
TextFormField( |
|
controller: _nameController, |
|
decoration: InputDecoration(labelText: 'Name'), |
|
), |
|
TextFormField( |
|
controller: _emailController, |
|
decoration: InputDecoration(labelText: 'Email'), |
|
), |
|
TextFormField( |
|
controller: _phoneController, |
|
decoration: InputDecoration(labelText: 'Phone'), |
|
), |
|
SizedBox(height: 16), |
|
Text( |
|
'Address:', |
|
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold), |
|
), |
|
TextFormField( |
|
controller: _streetController, |
|
decoration: InputDecoration(labelText: 'Street'), |
|
), |
|
TextFormField( |
|
controller: _street2Controller, |
|
decoration: InputDecoration(labelText: 'Street2'), |
|
), |
|
TextFormField( |
|
controller: _cityController, |
|
decoration: InputDecoration(labelText: 'City'), |
|
), |
|
FloatingActionButton( |
|
onPressed: pickImage, |
|
tooltip: 'Pick Image', |
|
child: const Icon(Icons.add_a_photo), |
|
), |
|
Container( |
|
child: getImage(), |
|
), |
|
SizedBox(height: 16), |
|
ElevatedButton( |
|
onPressed: () { |
|
_saveContact(context); |
|
}, |
|
child: Text('Save Contact'), |
|
), |
|
], |
|
), |
|
) |
|
) |
|
); |
|
} |
|
|
|
|
|
getImage() { |
|
if (_image != null){ |
|
Uint8List imageBytes = base64.decode(_image.toString()); |
|
ImageProvider imageProvider = MemoryImage(imageBytes); |
|
return Image( |
|
image: imageProvider, |
|
height: 150, |
|
width: 150, |
|
); |
|
} |
|
else{ |
|
return null; |
|
} |
|
} |
|
|
|
Future pickImage() async { |
|
final XFile? image = await _picker.pickImage(source: ImageSource.gallery); |
|
final path = image?.path; |
|
if (path != null){ |
|
if (kIsWeb){ |
|
final response = await fetchWebImage(path); |
|
final imageValue = base64Encode(response); |
|
setState(() { |
|
_image = imageValue; |
|
}); |
|
} |
|
else { |
|
final response = await File(path).readAsBytes(); |
|
final imageValue = base64Encode(response); |
|
setState(() { |
|
_image = imageValue; |
|
}); |
|
} |
|
} |
|
} |
|
|
|
Future<List<int>> fetchWebImage(path) async { |
|
final response = await http.get( |
|
Uri.parse(path), |
|
); |
|
return response.bodyBytes; |
|
} |
|
|
|
void _saveContact(BuildContext context) async { |
|
final name = _nameController.text; |
|
final email = _emailController.text; |
|
final phone = _phoneController.text; |
|
final street = _streetController.text; |
|
final street2 = _street2Controller.text; |
|
final city = _cityController.text; |
|
final image1920 = _image; |
|
|
|
if (name.isNotEmpty && email.isNotEmpty) { |
|
final newContact = ContactCreation( |
|
name: name, email: email, phone: phone, street: street, street2: street2, city: city, image1920: image1920); |
|
final newCreatedContacts = await widget.contactService.contactCreateRequest(contact: newContact); |
|
Navigator.pop(context, newCreatedContacts); // Return the new contact to the previous page |
|
} else { |
|
ScaffoldMessenger.of(context).showSnackBar( |
|
SnackBar(content: Text('Please enter name and email')), |
|
); |
|
} |
|
} |
|
} |