首页 > 解决方案 > 短信未从颤振应用程序发送

问题描述

我正在尝试使用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,
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

标签: fluttersmsmessaging

解决方案


我改变了方法并使用了 url_launcher 工具,效果很好。


推荐阅读