flutter - 短信未从颤振应用程序发送
问题描述
我正在尝试使用flutter_sms从我的flutter应用程序发送短信:^2.3.1。我已经实现了我在网上找到的示例中的代码。当我执行 _sendSMS() 并在观察窗口中观察 _message 时,当我在调用 _sendSMS() 后到达下一行的断点时,它会显示“正在收集数据”。我正在将消息发送到我的个人 Android 手机,但我没有收到它们。我不知道我错过了什么,所以我真的希望有人已经做到了这一点并且可以帮助我。
下面是我正在使用的代码:
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter_sms/flutter_sms.dart';
class SendTextMessage extends StatefulWidget {
@override
_SendTextMessageState createState() => new _SendTextMessageState();
}
class _SendTextMessageState extends State<SendTextMessage> {
TextEditingController _controllerPeople, _controllerMessage;
String _message, body;
List<String> people = [];
@override
void initState() {
super.initState();
initPlatformState();
}
Future<void> initPlatformState() async {
_controllerPeople = TextEditingController();
_controllerMessage = TextEditingController();
}
void _sendSMS(String message, List<String> recipents) async {
String _result =
await sendSMS(message: message, recipients: recipents);
_message = _result;
}
Widget _phoneTile(String name) {
return Padding(
padding: const EdgeInsets.all(3.0),
child: Container(
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(color: Colors.grey[300]),
top: BorderSide(color: Colors.grey[300]),
left: BorderSide(color: Colors.grey[300]),
right: BorderSide(color: Colors.grey[300]),
)),
child: Padding(
padding: EdgeInsets.all(4.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
IconButton(
icon: Icon(Icons.close),
onPressed: () => setState(() => people.remove(name)),
),
Padding(
padding: const EdgeInsets.all(0.0),
child: Text(
name,
textScaleFactor: 1.0,
style: TextStyle(fontSize: 12.0),
),
)
],
),
)),
);
}
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,
title: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset('assets/images/Appbar_logo.png',
fit: BoxFit.cover, height: 56),
],
),
),
resizeToAvoidBottomInset: false, // This fixes the keyboard white space
body: ListView(
children: <Widget>[
people == null || people.isEmpty
? Container(
height: 0.0,
)
: Container(
height: 90.0,
child: Padding(
padding: const EdgeInsets.all(3.0),
child: ListView(
scrollDirection: Axis.horizontal,
children:
List<Widget>.generate(people.length, (int index) {
return _phoneTile(people[index]);
}),
),
),
),
ListTile(
leading: Icon(Icons.people),
title: TextField(
controller: _controllerPeople,
decoration: InputDecoration(labelText: "Add Phone Number"),
onChanged: (String value) => setState(() {}),
),
trailing: IconButton(
icon: Icon(Icons.add),
onPressed: _controllerPeople.text.isEmpty
? null
: () => setState(() {
people.add(_controllerPeople.text.toString());
_controllerPeople.clear();
}),
),
),
Divider(),
ListTile(
leading: Icon(Icons.message),
title: TextField(
decoration: InputDecoration(labelText: " Add Message"),
controller: _controllerMessage,
onChanged: (String value) => setState(() {}),
),
trailing: IconButton(
icon: Icon(Icons.save),
onPressed: _controllerMessage.text.isEmpty
? null
: () => setState(() {
body = _controllerMessage.text.toString();
_controllerMessage.clear();
}),
),
),
Divider(),
ListTile(
leading: Icon(Icons.text_fields),
title: Text(body ?? "No Message Set"),
subtitle: people == null || people.isEmpty
? null
: Text(people.toString()),
trailing: IconButton(
icon: Icon(Icons.send),
onPressed: () {
if ((people == null || people.isEmpty) ||
(body == null || body.isEmpty)) {
setState(() =>
_message = "At Least 1 Person or Message Required");
} else {
_sendSMS(body, people);
setState(() =>_message);
}
},
),
),
Divider(),
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(12.0),
child: Text(
_message ?? "No Data",
maxLines: null,
),
),
],
),
],
),
),
);
}
}
解决方案
我改变了方法并使用了 url_launcher 工具,效果很好。
推荐阅读
- node.js - 如何在 AWS CDK 堆栈中压缩 Node Lambda 依赖项?
- powershell - 在 PowerShell 中开始睡眠后奇怪的 CPU 使用率下降
- python - 如何将贝叶斯块应用于 Python 中的时间序列数据?
- mysql - 如何根据不同日期范围的不同费率求和?
- android - 设置 ImageAnalyzer 和 PreviewView 的宽高相同
- gitlab - 如何在自动运行的情况下手动运行 GitLab CI 管道?
- http - 流式视频的请求流
- javascript - 删除 console.log 中的“空格”?
- mysql - Hibernate 高级映射 - 一对一 -
- c# - 用不同的列表填充列表视图