[英]How to call class method in another file dart
I've been studying darts for a few days.我已经学习飞镖几天了。 I have created a validation class which I will use in the TextFormField validator, but I don't know how to call the function我创建了一个验证类,我将在 TextFormField 验证器中使用它,但我不知道如何调用该函数
i know the documentation flutter use this我知道文档颤动使用这个
validator: (value) {
if (value.isEmpty) {
return 'Please enter some text';
}
return null;
}
but i create this class, so when I need it just call the function但是我创建了这个类,所以当我需要它时只需调用该函数
class Validation {
String validatePassword(String value) {
if (value.length < 4) {
return 'Password Minimal 4 Karakter';
}
return null;
}
String validateEmail(String value) {
if (!value.contains('@')) {
return 'Email Tidak Valid';
}
return null;
}
String validatedName(String value) {
if (value.isEmpty) {
return 'Nama Tidak Boleh Kosong';
}
return null;
}
}
and this is the code that will call the validation class这是将调用验证类的代码
import 'package:flutter/material.dart';
import 'package:flutapp/src/mixins/validation.dart';
class RegisterScreen extends StatefulWidget {
createState() {
return RegisterScreenState();
}
}
class RegisterScreenState extends State<RegisterScreen> with Validation {
final formKey = GlobalKey<FormState>();
String name = '';
String email = '';
String password = '';
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.all(20.0),
child: Form(
key: formKey,
child: Column(
children: [
nameField(),
emailField(),
passwordField(),
registerButton(),
],
),
)
);
}
}
Widget nameField() {
return TextFormField(
decoration: InputDecoration(
labelText: 'Nama Lengkap'
),
validator: validateName,
);
}
Widget emailField() {
return TextFormField(
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(
labelText: 'Email',
hintText: 'contoh@gmail.com'
),
);
}
Widget passwordField() {
return TextFormField(
obscureText: true,
decoration: InputDecoration(
labelText: 'Password',
hintText: 'contoh@password123'
),
);
}
Widget registerButton() {
return RaisedButton(
color: Colors.blueAccent,
onPressed: () {
},
child: Text('Register'),
);
}
any answer will be appreciated任何答案将不胜感激
Just call your validator in the validator
property of the TextFormField
.只需在TextFormField
的validator
属性中调用您的验证validator
。
return TextFormField(
obscureText: true,
// added this line
validator: Validation(). validatePassword,
decoration: InputDecoration(
labelText: 'Password',
hintText: 'contoh@password123'
),
);
The function has the same signature as the validator property, so you can just do this, instead of this:该函数与验证器属性具有相同的签名,因此您可以这样做,而不是这样:
return TextFormField(
obscureText: true,
// added this line
validator: (String value)=> Validation().validatePassword(value),
decoration: InputDecoration(
labelText: 'Password',
hintText: 'contoh@password123'
),
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.