G-EIP-700-TLS-7-1-eip-steph.../dialer/lib/features/composition/composition.dart

262 lines
9.4 KiB
Dart
Raw Normal View History

2024-10-26 20:53:30 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_contacts/flutter_contacts.dart';
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 = "";
List<Contact> _allContacts = [];
List<Contact> _filteredContacts = [];
final ContactService _contactService = ContactService();
@override
void initState() {
super.initState();
_fetchContacts();
}
Future<void> _fetchContacts() async {
if (await FlutterContacts.requestPermission()) {
_allContacts = await _contactService.fetchContacts();
_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;
_filterContacts();
2024-10-26 20:53:30 +00:00
});
}
void _onDeletePress() {
setState(() {
if (dialedNumber.isNotEmpty) {
dialedNumber = dialedNumber.substring(0, dialedNumber.length - 1);
_filterContacts();
2024-10-26 20:53:30 +00:00
}
});
}
void _onClearPress() {
setState(() {
dialedNumber = "";
_filteredContacts = _allContacts;
});
}
// Placeholder function for adding contact
void addContact(String number) {
// This function is empty for now
}
2024-10-26 20:53:30 +00:00
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
body: Stack(
2024-10-26 20:53:30 +00:00
children: [
Column(
children: [
// Top half: Display contacts matching dialed number
Expanded(
flex: 2,
2024-12-15 18:38:35 +00:00
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,
children: [
Expanded(
child: ListView(
children: _filteredContacts.isNotEmpty
? _filteredContacts.map((contact) {
return ListTile(
title: Text(
contact.displayName,
2024-12-15 18:38:35 +00:00
style: const TextStyle(color: Colors.white),
),
subtitle: contact.phones.isNotEmpty
? Text(
contact.phones.first.number,
2024-12-15 18:38:35 +00:00
style: const TextStyle(color: Colors.grey),
)
: null,
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
// Call button
IconButton(
2024-12-15 18:38:35 +00:00
icon: Icon(Icons.phone, color: Colors.green[300], size: 20),
onPressed: () {
2024-12-15 18:38:35 +00:00
print('Calling ${contact.displayName}');
},
),
// Text button
IconButton(
2024-12-15 18:38:35 +00:00
icon: Icon(Icons.message, color: Colors.blue[300], size: 20),
onPressed: () {
2024-12-15 18:38:35 +00:00
print('Texting ${contact.displayName}');
},
),
],
),
onTap: () {
// Handle contact selection if needed
},
);
}).toList()
2024-12-15 18:38:35 +00:00
: [Center(child: Text('No contacts found', style: TextStyle(color: Colors.white)))],
),
),
],
),
),
),
// 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,
children: [
Expanded(
child: Align(
alignment: Alignment.center,
child: Text(
dialedNumber,
2024-12-15 18:38:35 +00:00
style: const TextStyle(fontSize: 24, color: Colors.white),
overflow: TextOverflow.ellipsis,
),
),
),
IconButton(
onPressed: _onClearPress,
2024-12-15 18:38:35 +00:00
icon: const Icon(Icons.backspace, color: Colors.white),
),
],
),
const SizedBox(height: 10),
// Dialpad
Expanded(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Row(
2024-12-15 18:38:35 +00:00
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
_buildDialButton('1'),
_buildDialButton('2'),
_buildDialButton('3'),
],
),
Row(
2024-12-15 18:38:35 +00:00
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
_buildDialButton('4'),
_buildDialButton('5'),
_buildDialButton('6'),
],
),
Row(
2024-12-15 18:38:35 +00:00
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
_buildDialButton('7'),
_buildDialButton('8'),
_buildDialButton('9'),
],
),
Row(
2024-12-15 18:38:35 +00:00
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
_buildDialButton('*'),
_buildDialButton('0'),
_buildDialButton('#'),
],
),
],
),
),
),
],
),
),
2024-10-26 20:53:30 +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: () {
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(),
padding: const EdgeInsets.all(16),
2024-10-26 20:53:30 +00:00
),
child: Text(
number,
style: const TextStyle(
fontSize: 24,
color: Colors.white,
),
),
);
}
}