This commit is contained in:
Florian Griffon 2024-12-15 19:51:32 +01:00
parent 21b6b0a29a
commit b39e612ae7
2 changed files with 57 additions and 11 deletions

View File

@ -2,8 +2,6 @@
<uses-permission android:name="android.permission.READ_CONTACTS"/> <uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/> <uses-permission android:name="android.permission.WRITE_CONTACTS"/>
<uses-permission android:name="android.permission.CALL_PHONE" /> <uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-feature android:name="android.hardware.camera" android:required="false" />
<!-- The INTERNET permission is required for development. Specifically, <!-- The INTERNET permission is required for development. Specifically,
the Flutter tool needs it to communicate with the running application the Flutter tool needs it to communicate with the running application
to allow setting breakpoints, to provide hot reload, etc. to allow setting breakpoints, to provide hot reload, etc.

View File

@ -1,6 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_contacts/flutter_contacts.dart'; import 'package:flutter_contacts/flutter_contacts.dart';
import '../../widgets/contact_service.dart'; import 'package:url_launcher/url_launcher.dart';
import '../call/call.dart';
class CompositionPage extends StatefulWidget { class CompositionPage extends StatefulWidget {
const CompositionPage({super.key}); const CompositionPage({super.key});
@ -13,7 +14,6 @@ class _CompositionPageState extends State<CompositionPage> {
String dialedNumber = ""; String dialedNumber = "";
List<Contact> _allContacts = []; List<Contact> _allContacts = [];
List<Contact> _filteredContacts = []; List<Contact> _filteredContacts = [];
final ContactService _contactService = ContactService();
@override @override
void initState() { void initState() {
@ -22,9 +22,11 @@ class _CompositionPageState extends State<CompositionPage> {
} }
Future<void> _fetchContacts() async { Future<void> _fetchContacts() async {
_allContacts = await _contactService.fetchContacts(); if (await FlutterContacts.requestPermission()) {
_filteredContacts = _allContacts; _allContacts = await FlutterContacts.getContacts(withProperties: true);
setState(() {}); _filteredContacts = _allContacts;
setState(() {});
}
} }
void _filterContacts() { void _filterContacts() {
@ -40,6 +42,35 @@ class _CompositionPageState extends State<CompositionPage> {
}); });
} }
void _onCallPress(String phoneNumber) async {
final uri = Uri(scheme: 'tel', path: phoneNumber);
if (await canLaunchUrl(uri)) {
// Launch the system dialer in the background
launchUrl(uri);
// Navigate to your custom call page
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => CallPage(phoneNumber: phoneNumber),
),
);
} else {
print('Could not launch $uri');
}
}
// Function to send an SMS
void _onTextPress(String phoneNumber) async {
final uri = Uri(scheme: 'sms', path: phoneNumber);
if (await canLaunchUrl(uri)) {
// Launch the SMS app with the given phone number
launchUrl(uri);
} else {
print('Could not launch SMS to $phoneNumber');
}
}
void _onNumberPress(String number) { void _onNumberPress(String number) {
setState(() { setState(() {
dialedNumber += number; dialedNumber += number;
@ -112,18 +143,35 @@ class _CompositionPageState extends State<CompositionPage> {
color: Colors.green[300], color: Colors.green[300],
size: 20), size: 20),
onPressed: () { onPressed: () {
print( final phoneNumber = contact
'Calling ${contact.displayName}'); .phones.isNotEmpty
? contact.phones.first.number
: null;
if (phoneNumber != null) {
_onCallPress(phoneNumber);
} else {
print(
'${contact.displayName} has no phone number.');
}
}, },
), ),
// Text button // Text button
IconButton( IconButton(
icon: Icon(Icons.message, icon: Icon(Icons.message,
color: Colors.blue[300], color: Colors.blue[300],
size: 20), size: 20),
onPressed: () { onPressed: () {
print( final phoneNumber = contact
'Texting ${contact.displayName}'); .phones.isNotEmpty
? contact.phones.first.number
: null;
if (phoneNumber != null) {
_onTextPress(phoneNumber); // Text functionality
} else {
print(
'${contact.displayName} has no phone number.');
}
}, },
), ),
], ],