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.
133 lines
4.1 KiB
133 lines
4.1 KiB
5 months ago
|
import 'package:flutter/material.dart';
|
||
|
import 'package:odoo_contact_app/secureStorage.dart';
|
||
|
import 'main.dart';
|
||
|
import 'contactService.dart';
|
||
|
import 'model/contactList.dart';
|
||
|
import 'model/contactDetail.dart';
|
||
|
import 'contactDetailPage.dart';
|
||
|
import 'contactCreationPage.dart';
|
||
|
import 'dart:convert';
|
||
|
import 'dart:typed_data';
|
||
|
|
||
|
class HomePage extends StatefulWidget {
|
||
|
final String apikey ;
|
||
|
HomePage({Key? key, required this.apikey}) : super(key: key);
|
||
|
|
||
|
@override
|
||
|
_HomePageState createState() => _HomePageState();
|
||
|
}
|
||
|
|
||
|
class _HomePageState extends State<HomePage> {
|
||
|
late ContactService contactService;
|
||
|
late Future<List<ContactList>> contactResultFuture;
|
||
|
|
||
|
Future<List<ContactList>> getContactValues() async {
|
||
|
final response = await contactService.contactListRequest();
|
||
|
final List<ContactList>contactResult = response.map((json) => ContactList.fromJson(json)).toList();
|
||
|
return contactResult;
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
void initState() {
|
||
|
super.initState();
|
||
|
contactService = ContactService(widget.apikey);
|
||
|
contactResultFuture = getContactValues();
|
||
|
}
|
||
|
|
||
|
getCircleAvatar(image) {
|
||
|
if (image != null && image != false){
|
||
|
Uint8List imageBytes = base64.decode(image);
|
||
|
ImageProvider imageProvider = MemoryImage(imageBytes);
|
||
|
return CircleAvatar(
|
||
|
backgroundImage: imageProvider,
|
||
|
);
|
||
|
}
|
||
|
else{
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
return Scaffold(
|
||
|
appBar: AppBar(
|
||
|
title: Text('Contact List'),
|
||
|
actions: [
|
||
|
TextButton(
|
||
|
child: Text('Create'),
|
||
|
onPressed: () async {
|
||
|
// Save changes and navigate back
|
||
|
_openContactCreationForm();
|
||
|
},
|
||
|
),
|
||
|
TextButton(
|
||
|
child: Text('Logout'),
|
||
|
onPressed: () async {
|
||
|
// Save changes and navigate back
|
||
|
print('Create Clicked');
|
||
|
_performLogoutOperation(context);
|
||
|
},
|
||
|
),
|
||
|
]
|
||
|
),
|
||
|
body: FutureBuilder(
|
||
|
future: contactResultFuture,
|
||
|
builder: (context, snapshot) {
|
||
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
||
|
return Center(child: CircularProgressIndicator());
|
||
|
} else if (snapshot.hasError) {
|
||
|
return Center(child: Text('Error: ${snapshot.error}'));
|
||
|
} else {
|
||
|
final contacts = snapshot.data!;
|
||
|
return ListView.builder(
|
||
|
itemCount: contacts.length,
|
||
|
itemBuilder: (context, index) {
|
||
|
final contact = contacts[index];
|
||
|
return ListTile(
|
||
|
leading: getCircleAvatar(contact.image1920),
|
||
|
title: Text(contact.name),
|
||
|
subtitle: Column(
|
||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||
|
children: [
|
||
|
Text(contact.email.toString()),
|
||
|
// Text(contact.phone),
|
||
|
],
|
||
|
),
|
||
|
onTap: () async {
|
||
|
contactService.contactDetailRequest(id: contact.id).then((value){
|
||
|
final ContactDetail contactResult = ContactDetail.fromJson(value);
|
||
|
Navigator.push(
|
||
|
context, MaterialPageRoute(builder: (_) => ContatcDetailPage(contact: contactResult, contactService: contactService,)));
|
||
|
});
|
||
|
},
|
||
|
);
|
||
|
},
|
||
|
);
|
||
|
}
|
||
|
},
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
|
||
|
_openContactCreationForm() {
|
||
|
Navigator.push(
|
||
|
context,
|
||
|
MaterialPageRoute(builder: (context) => ContactCreationPage(contactService: contactService,)),
|
||
|
).then((newContact) {
|
||
|
if (newContact != null) {
|
||
|
setState(() {
|
||
|
contactResultFuture = getContactValues(); // Add new contact to the list
|
||
|
});
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
void _performLogoutOperation(BuildContext context) async {
|
||
|
await userCreds.deleteAll(); // Reset user credentials
|
||
|
Navigator.pushReplacement(
|
||
|
context,
|
||
|
MaterialPageRoute(builder: (context) => LoginPage()),
|
||
|
);
|
||
|
}
|
||
|
}
|