I want to catch or read OTP of SMS messages automatically. I did some tests like this code :
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),
),
],
),
)
);
}
}
And this is the test SMS message : 12345 is your code to log in.
Flutter documentation for oneTimeCode: https://api.flutter.dev/flutter/services/AutofillHints/oneTimeCode-constant.html
Flutter Autofill : https://github.com/flutter/flutter/blob/7891006299/packages/flutter/lib/src/services/autofill.dart#L362
IOS : https://developer.apple.com/documentation/uikit/uitextcontenttype
Android : https://developer.android.com/reference/androidx/autofill/HintConstants#AUTOFILL_HINT_SMS_OTP
