92 lines
2.8 KiB
Dart
92 lines
2.8 KiB
Dart
// ignore_for_file: avoid_print
|
|
import 'package:flutter/material.dart';
|
|
import 'package:simcards/sim_card.dart';
|
|
import 'package:simcards/simcards.dart';
|
|
|
|
class SmsService {
|
|
final Simcards _simcards = Simcards();
|
|
|
|
Future<void> sendVerificationSms({
|
|
required BuildContext context,
|
|
required String destinationNumber,
|
|
required String message,
|
|
}) async {
|
|
try {
|
|
// Request permission to access SIM cards
|
|
await _simcards.requestPermission();
|
|
|
|
bool permissionGranted = await _simcards.hasPermission();
|
|
if (!permissionGranted) {
|
|
print("❌ Permission denied.");
|
|
return;
|
|
}
|
|
|
|
List<SimCard> simCardList = await _simcards.getSimCards();
|
|
if (simCardList.isEmpty) {
|
|
print("❌ No SIM detected.");
|
|
return;
|
|
}
|
|
|
|
// If only one SIM card is available, send SMS directly
|
|
if (simCardList.length == 1) {
|
|
await _sendSms(destinationNumber, message, simCardList.first);
|
|
return;
|
|
}
|
|
|
|
// Show a dialog to let the user select a SIM card
|
|
int? selectedSimSlot = await showDialog<int>(
|
|
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
|
|
await _sendSms(destinationNumber, message, simCardList[selectedSimSlot]);
|
|
} catch (e) {
|
|
print("⚠️ Error sending SMS: $e");
|
|
}
|
|
}
|
|
|
|
Future<void> _sendSms(
|
|
String destinationNumber, String message, SimCard selectedSim) async {
|
|
// SMS Logic Here
|
|
print("✅ SMS sent via ${selectedSim.displayName}");
|
|
}
|
|
} |