SMS Send without manual sim ard choosing option

This commit is contained in:
2025-10-22 12:49:38 +05:30
parent 7a0265ad8d
commit c9c52b39fa
3 changed files with 65 additions and 77 deletions

View File

@@ -1,92 +1,70 @@
// ignore_for_file: avoid_print // ignore_for_file: avoid_print
import 'dart:io';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:simcards/sim_card.dart'; import 'package:simcards/sim_card.dart';
import 'package:simcards/simcards.dart'; import 'package:simcards/simcards.dart';
import 'package:flutter_sms/flutter_sms.dart';
import 'package:uuid/uuid.dart';
class SmsService { class SmsService {
final Simcards _simcards = Simcards(); final Simcards _simcards = Simcards();
Future<void> sendVerificationSms({ Future<void> sendVerificationSms({
required BuildContext context, required BuildContext context,
required String destinationNumber, required String destinationNumber,
required String message, required String message,
}) async { }) async {
try { try {
// Request permission to access SIM cards await _simcards.requestPermission();
await _simcards.requestPermission();
bool permissionGranted = await _simcards.hasPermission(); bool permissionGranted = await _simcards.hasPermission();
if (!permissionGranted) { if (!permissionGranted) {
print("❌ Permission denied."); print("❌ Permission denied." );
return; return;
} }
List<SimCard> simCardList = await _simcards.getSimCards(); List<SimCard> simCardList = await _simcards.getSimCards();
if (simCardList.isEmpty) { if (simCardList.isEmpty) {
print("❌ No SIM detected."); print("❌ No SIM detected." );
return; return;
} }
// If only one SIM card is available, send SMS directly await _sendSms(destinationNumber, message, simCardList.first);
if (simCardList.length == 1) {
await _sendSms(destinationNumber, message, simCardList.first);
return;
}
// Show a dialog to let the user select a SIM card } catch (e) {
int? selectedSimSlot = await showDialog<int>( print("⚠️ Error in SMS process: $e");
context: context, }
builder: (BuildContext dialogContext) { }
int? tempSelection;
return AlertDialog(
title: const Text("Select SIM to send SMS"),
content: StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return DropdownButton<int>(
hint: const Text("Choose SIM"),
value: tempSelection,
onChanged: (int? newValue) {
setState(() {
tempSelection = newValue;
});
},
items: List.generate(simCardList.length, (index) {
final sim = simCardList[index];
return DropdownMenuItem<int>(
value: index,
child: Text(sim.displayName ?? "SIM ${index + 1}"),
);
}),
);
},
),
actions: [
TextButton(
onPressed: () {
Navigator.pop(dialogContext, tempSelection);
},
child: const Text("OK"),
),
],
);
},
);
if (selectedSimSlot == null) {
print("⚠️ SIM not selected.");
return;
}
// Send SMS using the selected SIM card Future<void> _sendSms(
await _sendSms(destinationNumber, message, simCardList[selectedSimSlot]); String destinationNumber, String message, SimCard selectedSim) async {
} catch (e) { if (Platform.isAndroid) {
print("⚠️ Error sending SMS: $e"); try {
} // Generate a unique ID
} var uuid = const Uuid();
String uniqueId = uuid.v4(); // Generates a random v4 UUID
Future<void> _sendSms( // The message will be the unique ID
String destinationNumber, String message, SimCard selectedSim) async { String smsMessage = uniqueId;
// SMS Logic Here
print("✅ SMS sent via ${selectedSim.displayName}"); // flutter_sms sends the message using the device's default SMS app.
} // It does not programmatically select a SIM card. The 'selectedSim'
// parameter from the dialog will not be used to choose the sending SIM.
String result = await sendSMS(
message: smsMessage,
recipients: [destinationNumber],
sendDirect: true, // This attempts to send directly without opening the messaging app.
// It requires more permissions and might not work on all devices/Androidversions.
// If false, it will open the default SMS app.
);
print("✅ SMS send result: $result. Sent via ${selectedSim.displayName} (Note: OS default SIM isused).");
} catch (e) {
print("⚠️ Error sending SMS: $e");
}
} else {
print("⚠️ SMS sending is only supported on Android.");
}
}
} }

View File

@@ -307,6 +307,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "3.1.2" version: "3.1.2"
flutter_sms:
dependency: "direct main"
description:
name: flutter_sms
sha256: "2fe5f584f02596343557eeca56348f9b82413fefe83a423fab880cdbdf54d8d8"
url: "https://pub.dev"
source: hosted
version: "2.3.3"
flutter_svg: flutter_svg:
dependency: "direct main" dependency: "direct main"
description: description:
@@ -955,7 +963,7 @@ packages:
source: hosted source: hosted
version: "3.1.4" version: "3.1.4"
uuid: uuid:
dependency: transitive dependency: "direct main"
description: description:
name: uuid name: uuid
sha256: a5be9ef6618a7ac1e964353ef476418026db906c4facdedaa299b7a2e71690ff sha256: a5be9ef6618a7ac1e964353ef476418026db906c4facdedaa299b7a2e71690ff

View File

@@ -62,6 +62,8 @@ dependencies:
showcaseview: ^2.0.3 showcaseview: ^2.0.3
package_info_plus: ^4.2.0 package_info_plus: ^4.2.0
simcards: ^0.0.1 simcards: ^0.0.1
flutter_sms: ^2.3.3
uuid: ^4.5.1
# jailbreak_root_detection: "^1.1.6" # jailbreak_root_detection: "^1.1.6"