From 3cb06f2e93f0745fbfdd063eb821060982e54c27 Mon Sep 17 00:00:00 2001 From: Florian Griffon Date: Sun, 15 Dec 2024 02:57:55 +0100 Subject: [PATCH] feat: composition-page: call sms add contact --- .../lib/features/composition/composition.dart | 69 +++++++++++-------- 1 file changed, 42 insertions(+), 27 deletions(-) diff --git a/dialer/lib/features/composition/composition.dart b/dialer/lib/features/composition/composition.dart index b224f05..488b4a5 100644 --- a/dialer/lib/features/composition/composition.dart +++ b/dialer/lib/features/composition/composition.dart @@ -1,6 +1,8 @@ import 'package:flutter/material.dart'; import 'package:flutter_contacts/flutter_contacts.dart'; +import 'package:url_launcher/url_launcher.dart'; import '../../widgets/contact_service.dart'; +import '../contacts/widgets/add_contact_button.dart'; class CompositionPage extends StatefulWidget { const CompositionPage({super.key}); @@ -63,9 +65,24 @@ class _CompositionPageState extends State { }); } - // Placeholder function for adding contact - void addContact(String number) { - // This function is empty for now + // Function to call a contact's number + void _launchPhoneDialer(String phoneNumber) async { + final uri = Uri(scheme: 'tel', path: phoneNumber); + if (await canLaunchUrl(uri)) { + await launchUrl(uri); + } else { + debugPrint('Could not launch $phoneNumber'); + } + } + + // Function to send an SMS to a contact's number + void _launchSms(String phoneNumber) async { + final uri = Uri(scheme: 'sms', path: phoneNumber); + if (await canLaunchUrl(uri)) { + await launchUrl(uri); + } else { + debugPrint('Could not send SMS to $phoneNumber'); + } } @override @@ -90,19 +107,20 @@ class _CompositionPageState extends State { child: ListView( children: _filteredContacts.isNotEmpty ? _filteredContacts.map((contact) { + final phoneNumber = contact.phones.isNotEmpty + ? contact.phones.first.number + : 'No phone number'; 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, + subtitle: Text( + phoneNumber, + style: + const TextStyle(color: Colors.grey), + ), trailing: Row( mainAxisSize: MainAxisSize.min, children: [ @@ -112,18 +130,16 @@ class _CompositionPageState extends State { color: Colors.green[300], size: 20), onPressed: () { - print( - 'Calling ${contact.displayName}'); + _launchPhoneDialer(phoneNumber); }, ), - // Text button + // Message button IconButton( icon: Icon(Icons.message, color: Colors.blue[300], size: 20), onPressed: () { - print( - 'Texting ${contact.displayName}'); + _launchSms(phoneNumber); }, ), ], @@ -228,20 +244,19 @@ class _CompositionPageState extends State { ), ), ), - - // 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), - ), - ), ], ), + + // Add Contact Button + Positioned( + bottom: 20.0, + left: 0, + right: 0, + child: Center( + child: AddContactButton(), + ), + ), + // Top Row with Back Arrow Positioned( top: 40.0,