简体   繁体   English

如何在另一个文件 dart 中调用类方法

[英]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 .只需在TextFormFieldvalidator属性中调用您的验证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.

相关问题 Dart & Flutter:如何在另一个 class 中调用 class 方法 - Dart & Flutter: How to call for a class method inside another class 如何从 Flutter(Dart) 中的另一个类调用方法? - How to call method from another class in Flutter(Dart)? 无法调用另一个 dart 文件中的方法 - Unable to call a method in another dart file 从 flutter 和 dart 中的另一个 class 调用一个 class 中的方法 - Call a method in one class from another class in flutter and dart 从 Flutter 中的另一个 dart 文件在有状态小部件 class 下使用 SetState 方法调用 void - Call void with SetState method under stateful widget class from another dart file in Flutter 如何从另一个 Dart 文件中调用函数 - how to call a Function from another Dart file 如何访问一个 class 中的方法并在 flutter 中的另一个类方法中调用它(飞镖) - How to access methods in one class an call it in another classes method in flutter (dart) 如何从另一个Dart文件中调用有状态的widget(具有表单)方法?-Flutter - How to Call stateful widget(have form) method from another dart file?- Flutter 如何从另一个文件导入 class 以在 Dart/Flutter 中调用 Future - How do you import a class from another file to call a Future in Dart/Flutter 如何从 dart 中的另一个类访问一个类方法? - How to access one class method from another class in dart?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM