📜  如何在 Flutter 中获取短代码短信号码 - Dart 代码示例

📅  最后修改于: 2022-03-11 14:48:04.883000             🧑  作者: Mango

代码示例2
class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State {
  String _nirbinumber = '12345';
  // _lastMessage is probably redundant, as you can use msg (below)
  SmsMessage _lastMessage = new SmsMessage('', '');

  @override
  void initState() {
    super.initState();
    // listen to the stream of *all* message arriving
    new SmsReceiver().onSmsReceived.listen((SmsMessage msg) {
      // filter out the replies by number
      if (msg.address == _nirbinumber) {
        // fantastique - it's one of the ones we want
        setState(() {
          _lastMessage = msg;
        });
        saveGeoValue(msg); // this cannot use the value in _lastMessage as it will not have been set yet
        showMap(msg);
      }
    });
  }

  void _send() {
    // fire (and forget)
    new SmsSender().sendSms(new SmsMessage(_nirbinumber, 'test message'));
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('SMS demo'),
      ),
      body: new Center(
        child: new Text(_lastMessage.body),
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: _send,
        tooltip: 'Send SMS',
        child: new Icon(Icons.sms),
      ),
    );
  }
}