Я хочу подтвердить номер телефона / адрес электронной почты, который пользователь вводит в одно поле текстовой формы, как только пользователь вводит значение. Я пробовал использовать различные методы проверки, но это то, насколько близко я подошел возврат с ошибками. Я хочу, чтобы пользователь вводил адрес электронной почты или номер телефона, чтобы получить одноразовый пароль для входа в приложение. Я прикрепляю изображение [экрана пользовательского интерфейса] [1], чтобы показать, чего я хочу достичь. Я попытался определить окончательный глобальный ключ в main.dart как:
MyApp({Key key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
// _formKey and _autoValidate
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
bool _autoValidate = false;
String _name;
String _email;
String _mobile;
`````````````````````````````````````````
I want to validate inputs on my login page and it is returning with errors in the areas highlight in bold text.
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
class LoginPage extends StatefulWidget {
@override
LoginPageState createState() => LoginPageState();
}
class LoginPageState extends State<LoginPage> {
**Widget _showLoginInput() {
return Padding(
padding: EdgeInsets.only(top: 20.0),
child: TextFormField(
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Enter phone number/email id',
hintText: 'Enter phone number/email id',
),
keyboardType: TextInputType.emailAddress,
validator: validateEmail,
onSaved: (String val) {
_email = val;
},
keyboardType: TextInputType.phone,
validator: validateMobile,
onSaved: (String val) {
_mobile = val;
},
));
}
String validateMobile(String value) {
// Indian Mobile number are of 10 digit only
if (value.length != 10)
return 'Mobile Number must be of 10 digit';
else
return null;
}
String validateEmail(String value) {
Pattern pattern =
r'^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$';
RegExp regex = new RegExp(pattern);
if (!regex.hasMatch(value))
return 'Enter Valid Email';
else
return null;
}**
Widget _showOTPAction() {
return Positioned(
bottom: 0,
child: RaisedButton(
onPressed: () {},
textColor: Colors.white,
padding: const EdgeInsets.all(0.0),
child: Container(
height: 50,
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topRight,
end: Alignment.bottomLeft,
colors: [
Colors.blue[700],
Colors.blue,
Colors.deepPurple[700]
])),
padding: const EdgeInsets.all(15.0),
child: const Text(
'GET OTP',
style: TextStyle(fontSize: 20),
textAlign: TextAlign.center,
),
),
));
}
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomPadding: false,
body: Stack(children: <Widget>[
Container(
padding: EdgeInsets.symmetric(horizontal: 10.0),
**child: new Form(
key: _formKey,
autovalidate: _autoValidate,**
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SizedBox(
height: 180.0,
),
Padding(
padding: EdgeInsets.all(10.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
**children: <Widget>[
_showLiteTitle(),
_showAddressTitle(),
_showLoginInput(),
_showSkipAction()
],**
))
]))),
_showOTPAction(),
]));
}
}
[1]: https://i.stack.imgur.com/jZQIx.png