Flutter双卡双待短信发送插件flutter_sms_dual的使用
Flutter双卡双待短信发送插件flutter_sms_dual的使用
本项目是一个新的Flutter插件项目,旨在实现双卡双待手机的短信发送功能。它包含Android和iOS平台的具体实现代码。
开始使用
安装插件
首先,在你的pubspec.yaml
文件中添加flutter_sms_dual
插件:
dependencies:
flutter:
sdk: flutter
flutter_sms_dual: ^1.0.0
然后运行flutter pub get
以安装该插件。
初始化插件
在应用初始化时,你需要调用initPlatformState()
方法来初始化控制器和其他状态变量。
Future<void> initPlatformState() async {
_controllerPeople = TextEditingController();
_controllerMessage = TextEditingController();
}
发送短信
使用_sendSMS
方法可以发送短信。此方法接受一个电话号码列表作为参数,并通过FlutterSmsDual
插件发送短信。
Future<void> _sendSMS(List<String> recipients) async {
try {
String _result = await FlutterSmsDual().sendSMS(
message: _controllerMessage.text,
recipients: recipients,
sendDirect: sendDirect,
sendFromDefaultSIM: sendFromDefaultSIM);
setState(() => _message = _result);
} catch (error) {
setState(() => _message = error.toString());
}
}
检查是否可以发送短信
使用_canSendSMS
方法检查设备是否支持发送短信。
Future<bool> _canSendSMS() async {
bool _result = await FlutterSmsDual().canSendSMS();
setState(() => _canSendSMSMessage = _result ? 'This unit can send SMS' : 'This unit cannot send SMS');
return _result;
}
构建UI
以下是完整的示例代码,展示了如何构建用户界面以输入电话号码和消息,并发送短信。
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_sms_dual/flutter_sms_dual.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
[@override](/user/override)
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late TextEditingController _controllerPeople, _controllerMessage;
String? _message, body;
String _canSendSMSMessage = 'Check is not run.';
List<String> people = [];
bool sendDirect = false;
bool sendFromDefaultSIM = false;
[@override](/user/override)
void initState() {
super.initState();
initPlatformState();
}
Future<void> initPlatformState() async {
_controllerPeople = TextEditingController();
_controllerMessage = TextEditingController();
}
Future<void> _sendSMS(List<String> recipients) async {
try {
String _result = await FlutterSmsDual().sendSMS(
message: _controllerMessage.text,
recipients: recipients,
sendDirect: sendDirect,
sendFromDefaultSIM: sendFromDefaultSIM);
setState(() => _message = _result);
} catch (error) {
setState(() => _message = error.toString());
}
}
Future<bool> _canSendSMS() async {
bool _result = await FlutterSmsDual().canSendSMS();
setState(() => _canSendSMSMessage = _result ? 'This unit can send SMS' : 'This unit cannot send SMS');
return _result;
}
Widget _phoneTile(String name) {
return Padding(
padding: const EdgeInsets.all(3),
child: Container(
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(color: Colors.grey.shade300),
top: BorderSide(color: Colors.grey.shade300),
left: BorderSide(color: Colors.grey.shade300),
right: BorderSide(color: Colors.grey.shade300),
)),
child: Padding(
padding: const EdgeInsets.all(4),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
IconButton(
icon: const Icon(Icons.close),
onPressed: () => setState(() => people.remove(name)),
),
Padding(
padding: const EdgeInsets.all(0),
child: Text(
name,
textScaleFactor: 1,
style: const TextStyle(fontSize: 12),
),
)
],
),
)),
);
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: const Text('Android SMS Example'),
),
body: ListView(
children: <Widget>[
if (people.isEmpty)
const SizedBox(height: 0)
else
SizedBox(
height: 90,
child: Padding(
padding: const EdgeInsets.all(3),
child: ListView(
scrollDirection: Axis.horizontal,
children: List<Widget>.generate(people.length, (int index) {
return _phoneTile(people[index]);
}),
),
),
),
ListTile(
leading: const Icon(Icons.people),
title: TextField(
controller: _controllerPeople,
decoration:
const InputDecoration(labelText: 'Add Phone Number'),
keyboardType: TextInputType.number,
onChanged: (String value) => setState(() {}),
),
trailing: IconButton(
icon: const Icon(Icons.add),
onPressed: _controllerPeople.text.isEmpty
? null
: () => setState(() {
people.add(_controllerPeople.text.toString());
_controllerPeople.clear();
}),
),
),
const Divider(),
ListTile(
leading: const Icon(Icons.message),
title: TextField(
decoration: const InputDecoration(labelText: 'Add Message'),
controller: _controllerMessage,
onChanged: (String value) => setState(() {}),
),
),
const Divider(),
ListTile(
title: const Text('Can send SMS'),
subtitle: Text(_canSendSMSMessage),
trailing: IconButton(
padding: const EdgeInsets.symmetric(vertical: 16),
icon: const Icon(Icons.check),
onPressed: () {
_canSendSMS();
},
),
),
SwitchListTile(
title: const Text('Send Direct'),
subtitle: const Text(
'Should we skip the additional dialog? (Android only)'),
value: sendDirect,
onChanged: (bool newValue) {
setState(() {
sendDirect = newValue;
});
}),
Padding(
padding: const EdgeInsets.all(8),
child: ElevatedButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.resolveWith(
(states) => Theme.of(context).colorScheme.secondary),
padding: MaterialStateProperty.resolveWith(
(states) => const EdgeInsets.symmetric(vertical: 16)),
),
onPressed: () {
_send();
},
child: Text(
'SEND',
style: Theme.of(context).textTheme.displaySmall,
),
),
),
Visibility(
visible: _message != null,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
child: Padding(
padding: const EdgeInsets.all(12),
child: Text(
_message ?? 'No Data',
maxLines: null,
),
),
),
],
),
),
],
),
),
);
}
void _send() {
if (people.isEmpty) {
setState(() => _message = 'At Least 1 Person or Message Required');
} else {
_sendSMS(people);
}
}
}
更多关于Flutter双卡双待短信发送插件flutter_sms_dual的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter双卡双待短信发送插件flutter_sms_dual的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,关于flutter_sms_dual
插件的使用,这里是一个简要的示例代码案例,它展示了如何在Flutter应用中利用该插件实现双卡双待设备的短信发送功能。请注意,为了运行这个示例,你需要确保已经正确安装并配置了flutter_sms_dual
插件。
首先,确保你的pubspec.yaml
文件中已经添加了flutter_sms_dual
依赖:
dependencies:
flutter:
sdk: flutter
flutter_sms_dual: ^最新版本号 # 请替换为最新的版本号
然后,运行flutter pub get
来安装依赖。
接下来,是一个简单的Flutter应用示例,展示了如何使用flutter_sms_dual
发送短信:
import 'package:flutter/material.dart';
import 'package:flutter_sms_dual/flutter_sms_dual.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Flutter Dual SIM SMS Example'),
),
body: Center(
child: SendSmsScreen(),
),
),
);
}
}
class SendSmsScreen extends StatefulWidget {
@override
_SendSmsScreenState createState() => _SendSmsScreenState();
}
class _SendSmsScreenState extends State<SendSmsScreen> {
final TextEditingController _phoneNumberController = TextEditingController();
final TextEditingController _messageController = TextEditingController();
String _simSlot = 'Select SIM';
String? _resultMessage;
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextField(
controller: _phoneNumberController,
decoration: InputDecoration(labelText: 'Phone Number'),
keyboardType: TextInputType.phone,
),
TextField(
controller: _messageController,
decoration: InputDecoration(labelText: 'Message'),
maxLines: 5,
),
DropdownButtonFormField<String>(
value: _simSlot,
hint: Text('Select SIM'),
onChanged: (newValue) {
setState(() {
_simSlot = newValue!;
});
},
items: ['SIM 1', 'SIM 2'].map((sim) {
return DropdownMenuItem<String>(
value: sim,
child: Text(sim),
);
}).toList(),
),
ElevatedButton(
onPressed: () async {
String phoneNumber = _phoneNumberController.text;
String message = _messageController.text;
if (phoneNumber.isEmpty || message.isEmpty || _simSlot == 'Select SIM') {
setState(() {
_resultMessage = 'Please fill all fields.';
});
return;
}
int simSlot = _simSlot == 'SIM 1' ? 0 : 1; // Assuming 0 for SIM 1 and 1 for SIM 2
try {
bool success = await FlutterSmsDual.sendSms(
phoneNumber: phoneNumber,
message: message,
simSlot: simSlot,
);
setState(() {
_resultMessage = success ? 'Message sent successfully!' : 'Failed to send message.';
});
} catch (e) {
setState(() {
_resultMessage = 'Error: ${e.message}';
});
}
},
child: Text('Send SMS'),
),
Text(_resultMessage ?? ''),
],
);
}
}
注意:
-
上述代码中的
Column
应该是Column
组件的正确使用方式,通常你会在一个Padding
或Container
中使用它,并且需要mainAxisSize: MainAxisSize.min
或其他适当的布局参数来确保它不会占满整个屏幕。这里为了简洁,我省略了这些。 -
FlutterSmsDual.sendSms
方法的simSlot
参数是一个整数,通常0代表SIM 1,1代表SIM 2,但这取决于设备的实现和插件的API。 -
错误处理部分只是简单地捕获异常并显示错误信息。在实际应用中,你可能需要更详细的错误处理逻辑。
-
请确保在真实设备上测试双卡双待功能,因为模拟器可能不支持此功能。
-
根据
flutter_sms_dual
插件的文档,可能需要额外的权限配置,如SEND_SMS
权限等,请确保在AndroidManifest.xml
中正确配置。
这个示例应该能帮助你入门如何在Flutter应用中使用flutter_sms_dual
插件发送短信。