flutter - 如何从 SMS 获取 OTP - 自动填充

标签 flutter dart autofill one-time-password

我想自动捕获或读取 SMS 消息的 OTP。我做了一些像这样的代码测试:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Demo Auto OTP'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  TextEditingController _textController = TextEditingController();
  String _error;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Multi-Factor-Authentication"),
        ),
        body: Form(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            mainAxisSize: MainAxisSize.min,
            children: [
              TextField(
                controller: _textController,
                autofillHints: [ AutofillHints.oneTimeCode ],
                keyboardType: TextInputType.visiblePassword,
                maxLength: 6,
                maxLengthEnforced: true,
                style: TextStyle(fontSize: 32),
              ),

              RaisedButton(
                child: Text("Verify"),
                onPressed: () => Navigator.of(context).pop(_textController.value.text),
              ),
            ],
          ),
        )
    );
  }
}
这是测试短信:12345 is your code to log in.oneTimeCode 的 flutter 文档:
https://api.flutter.dev/flutter/services/AutofillHints/oneTimeCode-constant.html
flutter 自动填充:https://github.com/flutter/flutter/blob/7891006299/packages/flutter/lib/src/services/autofill.dart#L362
IOS:https://developer.apple.com/documentation/uikit/uitextcontenttype
安卓 :
https://developer.android.com/reference/androidx/autofill/HintConstants#AUTOFILL_HINT_SMS_OTP

最佳答案

你可以使用这个包:https://pub.dev/packages/sms_autofill
但请考虑以下限制:

Android SMS constraint For the code to be receive, it need to follow some rules as describe here: https://developers.google.com/identity/sms-retriever/verify

Be no longer than 140 bytes Begin with the prefix <#> Contain a one-time code that the client sends back to your server to complete the verification flow End with an 11-character hash string that identifies your app One example of SMS would be:

<#> ExampleApp: Your code is 123456 FA+9qCX9VSu

关于flutter - 如何从 SMS 获取 OTP - 自动填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65559187/

相关文章:

javascript - 在 Dart 中 .callMethod 返回后如何调用 .then?

java - 使用多种类型的数据包实现 Protobuf3 的最佳方法是什么?

CSS 设置输入自动填充图像

vba - 公式自动填充多列,使用 VBA 中的特定单元格

android - Flutter - 如何在 Firebase 中写入数据(实时 ddb): permission denied

dart - Flutter:将 ListTile 标记为在抽屉中被选中

list - 如果 Flutter 中的网格列表为空,则插入一个 Text()

flutter - 如何处理ListView滚动方向

javascript - 带有 Javascript 的 WebView 不断重新加载

ios - 如何在应用程序顶部显示状态栏?