大佬教程收集整理的这篇文章主要介绍了如何执行 TextField 的验证?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我是 Flutter 新手,我想对 TextFIEld 执行某种验证。我想检查它是否为空,如果不是,邮件是否格式正确。我正在为此苦苦挣扎,我不确定是否应该将 StartPage
的主类更改为 StatefulWidget
。此类包含使用 TextFIEld
重定向到新页面的按钮。在此字段中,我们输入电子邮件,然后单击按钮进行一些 http 请求(当然是在验证之后)。我读到我需要将带有 TextFIEld
的类更改为 extends State
类。
我发现例如:https://flutter.dev/docs/cookbook/forms/validation 但对我来说很奇怪,首先我们创建 MyApp,它是 Stateless
然后我们重定向到扩展 StatefulWidget
的类并且它没有做任何事情然后重定向到下一个extends State
。
实现它的最佳实践是什么?感谢您的帮助。
起始页
import 'package:Flutter/cuperTino.dart';
import 'package:Flutter/material.dart';
import 'package:.../ui/pages/LoginPage.dart';
class StartPage extends StatelessWidget {
@overrIDe
Widget build(BuildContext context) {
return Scaffold(
body: Container(
padding: EdgeInsets.all(60.0),wIDth: Mediaquery.of(context).size.wIDth,height: Mediaquery.of(context).size.height,child: column(
crossAxisAlignment: CrossAxisAlignment.center,children: [
Flutterlogo(size: 150),SizedBox(
height: 50.0,),Text(
'blabla',style: TextStyle(FontSize: 60,color: color(0xffffffff)),textAlign: TextAlign.center,SizedBox(
height: 30.0,Text(
'Wypożyczaj to,czego potrzebujesz',style: TextStyle(FontSize: 23,SizedBox(
height: 70.0,ConsTrainedBox(
consTraints: BoxConsTraints.tightFor(wIDth: 240,height: 60),child: Elevatedbutton(
onpressed: () {
Navigator.push(context,new MaterialPageRoute(builder: (context) => LoginPage()));
},child: Text('Zaloguj się',style: TextStyle(FontSize: 30,textAlign: TextAlign.center),style: buttonStyle(
BACkgroundcolor: MaterialStateProperty.all<color>(
color(0xFF6FC76C),shape: MaterialStateProperty.all<RoundedRectangleborder>(
RoundedRectangleborder(
borderRadius: borderRadius.circular(25.0),Outlinedbutton(
onpressed: null,style: buttonStyle(
sIDe: MaterialStateProperty.all(
bordersIDe(color: colors.whitE),shape: MaterialStateProperty.all(
RoundedRectangleborder(
borderRadius: borderRadius.circular(25.0),child: const Text("blabla",style: TextStyle(
FontSize: 20,color: color(0xffffffff),],BACkgroundcolor: const color(0xFE1A5BFF),);
}
}
和登录页面
import 'dart:convert';
import 'dart:io';
import 'package:Flutter/material.dart';
import 'package:Flutter_signin_button/Flutter_signin_button.dart';
import 'package:http/http.dart' as http;
class LoginPage extends StatelessWidget {
final String baseURL =
"....";
final emailController = TextEdiTingController();
bool emailValIDate = false;
@overrIDe
Widget build(BuildContext context) {
return Scaffold(
appbar: Appbar(
title: Text("Zaloguj się"),body: Container(
padding: EdgeInsets.all(40.0),children: [
Flutterlogo(size: 130),Text(
'Zaloguj lub zarejestruj się za pomocą adresu e-mail',style: TextStyle(
FontSize: 18,color: color(0xff000000),TextFIEld(
controller: emailController,decoration: inputdecoration(
border: Outlineinputborder(),hintText: 'Adres e-mail',fillcolor: color(0xffdbdbdb),filled: true,errorText:
emailValIDate ? 'Please enter a Username' : null)),SizedBox(
height: 15.0,child: Elevatedbutton(
onpressed: () => movetoProperWindowBasedOnEmail(
emailController.text,context),child: Text('blabla',style: TextStyle(
FontSize: 30,style: buttonStyle(
BACkgroundcolor: MaterialStateProperty.all<color>(
color(0xFF2F45C6),SizedBox(
height: 35.0,Text(
'Możesz też się zalogować za pomocą:',SignInbutton(
buttons.Google,text: "Sign up with Google",onpressed: () {},SignInbutton(
buttons.Facebook,text: "Sign up with Facebook",)
],BACkgroundcolor: const color(0xFEF2F7FD),);
}
Future<voID> movetoProperWindowBasedOnEmail(
String text,BuildContext context) async {
//valIDation
if (valIDation == 'false') {
....
}
}
}
}
这是一个如何将 textformfield 与表单小部件和验证器一起使用的示例
import 'package:flutter/material.dart';
class AddUser extends StatefulWidget {
@override
_AddUserState createState() => _AddUserState();
}
class _AddUserState extends State<AddUser> {
TextEdiTingController id = TextEdiTingController();
TextEdiTingController name = TextEdiTingController();
TextEdiTingController lastName = TextEdiTingController();
TextEdiTingController age = TextEdiTingController();
final _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: SingleChildScrollView(
child: Container(
height: MediaQuery.of(context).size.height,width: MediaQuery.of(context).size.width,padding: EdgeInsets.all(8.0),child: Form(
key: _formKey,child: column(
mainAxisAlignment: MainAxisAlignment.center,children: [
Text(
'Graphql example',style: TextStyle(fontSize: 22),),SizedBox(
height: 50,TextFormField(
validator: (val) {
return val.isEmpty
? 'please provide a valid value'
: null;
},controller: id,decoration: InputDecoration(
hintText: 'ID',border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),borderside: Borderside(color: Colors.whitE))),SizedBox(
height: 20,controller: age,decoration: InputDecoration(
hintText: 'Age',controller: name,decoration: InputDecoration(
hintText: 'FirstName',controller: lastName,decoration: InputDecoration(
hintText: 'LastName',RaisedButton(
onPressed: () {
if (_formKey.currentState.validate()) {}
},shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),color: Colors.black,child: Text(
'Submit',style: TextStyle(color: Colors.white,fontSize: 16),))
],);
}
}
,
在阅读您的问题时,我意识到您需要对 Flutter Widget 类型有基本的了解。
即 => Stateful
和 Stateless
小部件。
我建议您先搜索这些。那么你就很容易有一个了解了。否则这些答案只会给你一条鱼,而不是教你如何钓鱼。
,我建议您在该小部件中使用 flutter 的新表单小部件,您可以传递多个 textformfields 小部件。
这些新小部件最好的部分是它们接受一个验证器密钥,您可以在其中编写特定文本字段的所有验证,并在验证通过时返回 false。
查看下面的表格以更好地理解
https://www.github.com/mrinaljain/flutter_shoping_cart/tree/master/lib%2Fscreens%2Fedit_product_screen.dart
以上是大佬教程为你收集整理的如何执行 TextField 的验证?全部内容,希望文章能够帮你解决如何执行 TextField 的验证?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。