2024-10-26 20:53:30 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2024-11-05 22:39:48 +00:00
|
|
|
import 'package:flutter_contacts/flutter_contacts.dart';
|
2024-12-12 16:47:49 +00:00
|
|
|
import '../../widgets/contact_service.dart';
|
2024-10-26 20:53:30 +00:00
|
|
|
|
|
|
|
class CompositionPage extends StatefulWidget {
|
|
|
|
const CompositionPage({super.key});
|
|
|
|
|
|
|
|
@override
|
|
|
|
_CompositionPageState createState() => _CompositionPageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _CompositionPageState extends State<CompositionPage> {
|
|
|
|
String dialedNumber = "";
|
2024-11-05 22:39:48 +00:00
|
|
|
List<Contact> _allContacts = [];
|
|
|
|
List<Contact> _filteredContacts = [];
|
2024-12-12 16:47:49 +00:00
|
|
|
final ContactService _contactService = ContactService();
|
2024-11-05 22:39:48 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
_fetchContacts();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _fetchContacts() async {
|
|
|
|
if (await FlutterContacts.requestPermission()) {
|
2024-12-12 16:47:49 +00:00
|
|
|
_allContacts = await _contactService.fetchContacts();
|
2024-11-05 22:39:48 +00:00
|
|
|
_filteredContacts = _allContacts;
|
|
|
|
setState(() {});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _filterContacts() {
|
|
|
|
setState(() {
|
|
|
|
_filteredContacts = _allContacts.where((contact) {
|
|
|
|
final phoneMatch = contact.phones.any((phone) =>
|
|
|
|
phone.number.replaceAll(RegExp(r'\D'), '').contains(dialedNumber));
|
|
|
|
final nameMatch = contact.displayName
|
|
|
|
.toLowerCase()
|
|
|
|
.contains(dialedNumber.toLowerCase());
|
|
|
|
return phoneMatch || nameMatch;
|
|
|
|
}).toList();
|
|
|
|
});
|
|
|
|
}
|
2024-10-26 20:53:30 +00:00
|
|
|
|
|
|
|
void _onNumberPress(String number) {
|
|
|
|
setState(() {
|
|
|
|
dialedNumber += number;
|
2024-11-05 22:39:48 +00:00
|
|
|
_filterContacts();
|
2024-10-26 20:53:30 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void _onDeletePress() {
|
|
|
|
setState(() {
|
|
|
|
if (dialedNumber.isNotEmpty) {
|
|
|
|
dialedNumber = dialedNumber.substring(0, dialedNumber.length - 1);
|
2024-11-05 22:39:48 +00:00
|
|
|
_filterContacts();
|
2024-10-26 20:53:30 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-10-31 17:54:38 +00:00
|
|
|
void _onClearPress() {
|
|
|
|
setState(() {
|
|
|
|
dialedNumber = "";
|
2024-11-05 22:39:48 +00:00
|
|
|
_filteredContacts = _allContacts;
|
2024-10-31 17:54:38 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-11-05 22:39:48 +00:00
|
|
|
// Placeholder function for adding contact
|
|
|
|
void addContact(String number) {
|
|
|
|
// This function is empty for now
|
2024-10-31 17:54:38 +00:00
|
|
|
}
|
|
|
|
|
2024-10-26 20:53:30 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
backgroundColor: Colors.black,
|
2024-11-05 22:39:48 +00:00
|
|
|
body: Stack(
|
2024-10-26 20:53:30 +00:00
|
|
|
children: [
|
2024-11-05 22:39:48 +00:00
|
|
|
Column(
|
|
|
|
children: [
|
|
|
|
// Top half: Display contacts matching dialed number
|
|
|
|
Expanded(
|
|
|
|
flex: 2,
|
|
|
|
child:
|
|
|
|
Container(
|
|
|
|
padding: const EdgeInsets.only(top: 42.0, left: 16.0, right: 16.0, bottom: 16.0),
|
|
|
|
color: Colors.black,
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
2024-10-31 17:54:38 +00:00
|
|
|
children: [
|
|
|
|
Expanded(
|
2024-11-05 22:39:48 +00:00
|
|
|
child: ListView(
|
|
|
|
children: _filteredContacts.isNotEmpty
|
|
|
|
? _filteredContacts.map((contact) {
|
|
|
|
return ListTile(
|
|
|
|
title: Text(
|
|
|
|
contact.displayName,
|
|
|
|
style: const TextStyle(color: Colors.white),
|
|
|
|
),
|
|
|
|
subtitle: contact.phones.isNotEmpty
|
|
|
|
? Text(
|
|
|
|
contact.phones.first.number,
|
|
|
|
style: const TextStyle(color: Colors.grey),
|
|
|
|
)
|
|
|
|
: null,
|
|
|
|
trailing: Row(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
|
|
|
// Call button
|
|
|
|
IconButton(
|
|
|
|
icon: Icon(Icons.phone, color: Colors.green[300], size: 20),
|
|
|
|
onPressed: () {
|
|
|
|
print('Calling ${contact.displayName}');
|
|
|
|
},
|
|
|
|
),
|
|
|
|
// Text button
|
|
|
|
IconButton(
|
|
|
|
icon: Icon(Icons.message, color: Colors.blue[300], size: 20),
|
|
|
|
onPressed: () {
|
|
|
|
print('Texting ${contact.displayName}');
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
onTap: () {
|
|
|
|
// Handle contact selection if needed
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}).toList()
|
|
|
|
: [Center(child: Text('No contacts found', style: TextStyle(color: Colors.white)))],
|
2024-10-31 17:54:38 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2024-11-05 22:39:48 +00:00
|
|
|
),
|
|
|
|
),
|
2024-10-31 17:54:38 +00:00
|
|
|
|
2024-11-05 22:39:48 +00:00
|
|
|
// Bottom half: Dialpad and Dialed number display with erase button
|
|
|
|
Expanded(
|
|
|
|
flex: 2,
|
|
|
|
child: Container(
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 16.0),
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
// Display dialed number with erase button
|
|
|
|
Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
2024-10-31 17:54:38 +00:00
|
|
|
children: [
|
2024-11-05 22:39:48 +00:00
|
|
|
Expanded(
|
|
|
|
child: Align(
|
|
|
|
alignment: Alignment.center,
|
|
|
|
child: Text(
|
|
|
|
dialedNumber,
|
|
|
|
style: const TextStyle(fontSize: 24, color: Colors.white),
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
),
|
|
|
|
),
|
2024-10-31 17:54:38 +00:00
|
|
|
),
|
2024-11-05 22:39:48 +00:00
|
|
|
IconButton(
|
|
|
|
onPressed: _onClearPress,
|
|
|
|
icon: const Icon(Icons.backspace, color: Colors.white),
|
2024-10-31 17:54:38 +00:00
|
|
|
),
|
2024-11-05 22:39:48 +00:00
|
|
|
],
|
|
|
|
),
|
|
|
|
const SizedBox(height: 10),
|
|
|
|
|
|
|
|
// Dialpad
|
|
|
|
Expanded(
|
|
|
|
child: SingleChildScrollView(
|
|
|
|
child: Column(
|
2024-10-31 17:54:38 +00:00
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
|
|
|
children: [
|
2024-11-05 22:39:48 +00:00
|
|
|
Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
|
|
|
children: [
|
|
|
|
_buildDialButton('1'),
|
|
|
|
_buildDialButton('2'),
|
|
|
|
_buildDialButton('3'),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
|
|
|
children: [
|
|
|
|
_buildDialButton('4'),
|
|
|
|
_buildDialButton('5'),
|
|
|
|
_buildDialButton('6'),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
|
|
|
children: [
|
|
|
|
_buildDialButton('7'),
|
|
|
|
_buildDialButton('8'),
|
|
|
|
_buildDialButton('9'),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
|
|
|
children: [
|
|
|
|
_buildDialButton('*'),
|
|
|
|
_buildDialButton('0'),
|
|
|
|
_buildDialButton('#'),
|
|
|
|
],
|
|
|
|
),
|
2024-10-31 17:54:38 +00:00
|
|
|
],
|
|
|
|
),
|
2024-11-05 22:39:48 +00:00
|
|
|
),
|
2024-10-31 17:54:38 +00:00
|
|
|
),
|
2024-11-05 22:39:48 +00:00
|
|
|
],
|
2024-10-31 17:54:38 +00:00
|
|
|
),
|
2024-11-05 22:39:48 +00:00
|
|
|
),
|
2024-10-26 20:53:30 +00:00
|
|
|
),
|
2024-10-31 17:54:38 +00:00
|
|
|
|
2024-11-05 22:39:48 +00:00
|
|
|
// Add Contact Button with empty function call
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.only(bottom: 20.0),
|
|
|
|
child: FloatingActionButton(
|
|
|
|
backgroundColor: Colors.blue,
|
|
|
|
onPressed: () {
|
|
|
|
addContact(dialedNumber);
|
|
|
|
},
|
|
|
|
child: const Icon(Icons.person_add, color: Colors.white),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
// Top Row with Back Arrow
|
|
|
|
Positioned(
|
|
|
|
top: 40.0,
|
|
|
|
left: 16.0,
|
|
|
|
child: IconButton(
|
|
|
|
icon: const Icon(Icons.arrow_back, color: Colors.white),
|
2024-10-26 20:53:30 +00:00
|
|
|
onPressed: () {
|
2024-11-05 22:39:48 +00:00
|
|
|
Navigator.pop(context);
|
2024-10-26 20:53:30 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildDialButton(String number) {
|
|
|
|
return ElevatedButton(
|
|
|
|
onPressed: () => _onNumberPress(number),
|
|
|
|
style: ElevatedButton.styleFrom(
|
|
|
|
backgroundColor: Colors.black,
|
|
|
|
shape: const CircleBorder(),
|
2024-11-05 22:39:48 +00:00
|
|
|
padding: const EdgeInsets.all(16),
|
2024-10-26 20:53:30 +00:00
|
|
|
),
|
|
|
|
child: Text(
|
|
|
|
number,
|
|
|
|
style: const TextStyle(
|
|
|
|
fontSize: 24,
|
|
|
|
color: Colors.white,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|