Flutter自定义表单插件custom_form_w的使用

Flutter自定义表单插件custom_form_w的使用

CustomFormW 插件介绍

CustomFormW 是一个灵活且可定制的表单插件,适用于 Flutter。它允许动态创建带有验证、自定义标签、样式和更多功能的表单。

示例代码

import 'package:custom_form_w/custom_form_w.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      home: Scaffold(
        body: CustomFormW(
          numberOfFields: 3,
          requiredFieldIndices: [1, 2 ],
          labelText: ['Name', 'Email', 'Phone'],
          onSubmit: () {
            print('Form submitted');
          },
          // for email validation, password length validation and phone number validation
          keyboardType: [
              TextInputType.text,
              TextInputType.emailAddress,
              TextInputType.phone,
              TextInputType.visiblePassword,
              TextInputType.visiblePassword,
              TextInputType.text,
              TextInputType.text,
            ],
            suffixIcon: [
              Icon(Icons.person),
              Icon(Icons.email),
              Icon(Icons.phone),
              // if you want to hide the icon just pass null
              Icon(null),
              Icon(null),
              Icon(null),
              Icon(Icons.location_city),
            ],
        ),
      ),
    ),
  );
}

功能特性

  • 动态创建表单:指定字段数量。
  • 高亮必填项:通过星号 (*) 标记必填项。
  • 自定义标签、提示和样式:可以自定义每个字段的标签、提示和样式。
  • 支持多种输入类型:如文本、电子邮件等。
  • 简单验证:支持必填字段验证。
  • 自定义提交按钮:可以设置提交按钮的文本、颜色和动作。
  • 可配置的边框样式和表单布局
  • 可选的提交按钮可见性
  • 可选的验证Snackbar
  • 可选的电话号码验证
  • 可选的电子邮件验证
  • 可选的密码长度验证
  • 可选的验证Snackbar文本
  • 可选的电话号码正则表达式
  • 可选的电话号码错误消息
  • 可选的电子邮件正则表达式
  • 可选的电子邮件错误消息
  • 可选的密码最小长度
  • 可选的密码长度错误消息

安装

pubspec.yaml 文件中添加以下依赖:

dependencies:
  custom_form_w: ^2.0.1

运行

运行命令:

flutter pub get

更多关于Flutter自定义表单插件custom_form_w的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter自定义表单插件custom_form_w的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是一个关于如何在Flutter中使用自定义表单插件custom_form_w的示例代码。请注意,由于custom_form_w不是一个广泛知名的Flutter插件,我假设它提供了基本的表单字段和验证功能。如果实际的插件API有所不同,请根据官方文档进行调整。

首先,确保你已经在pubspec.yaml文件中添加了custom_form_w依赖:

dependencies:
  flutter:
    sdk: flutter
  custom_form_w: ^x.y.z  # 替换为实际的版本号

然后运行flutter pub get来安装依赖。

接下来是一个使用custom_form_w插件的示例代码:

import 'package:flutter/material.dart';
import 'package:custom_form_w/custom_form_w.dart';  // 假设插件的import路径

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Custom Form Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: CustomFormScreen(),
    );
  }
}

class CustomFormScreen extends StatefulWidget {
  @override
  _CustomFormScreenState createState() => _CustomFormScreenState();
}

class _CustomFormScreenState extends State<CustomFormScreen> {
  final _formKey = GlobalKey<FormState>();

  String? _name;
  String? _email;
  String? _password;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Custom Form Example'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Form(
          key: _formKey,
          child: Column(
            children: <Widget>[
              CustomTextFormField(
                labelText: 'Name',
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return 'Name is required';
                  }
                  return null;
                },
                onSaved: (value) {
                  _name = value;
                },
              ),
              CustomTextFormField(
                labelText: 'Email',
                validator: (value) {
                  if (value == null || !value.contains('@')) {
                    return 'Please enter a valid email address';
                  }
                  return null;
                },
                onSaved: (value) {
                  _email = value;
                },
              ),
              CustomPasswordFormField(
                labelText: 'Password',
                validator: (value) {
                  if (value == null || value.length < 6) {
                    return 'Password must be at least 6 characters long';
                  }
                  return null;
                },
                onSaved: (value) {
                  _password = value;
                },
              ),
              SizedBox(height: 20),
              ElevatedButton(
                onPressed: () {
                  if (_formKey.currentState!.validate()) {
                    _formKey.currentState!.save();
                    // Handle form submission, e.g., save to database or navigate to next screen
                    print('Name: $_name');
                    print('Email: $_email');
                    print('Password: $_password');
                  }
                },
                child: Text('Submit'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

// 假设插件提供了自定义的文本表单字段和密码表单字段
class CustomTextFormField extends StatelessWidget {
  final String labelText;
  final FormFieldValidator<String?>? validator;
  final FormFieldSetter<String?>? onSaved;

  const CustomTextFormField({
    Key? key,
    required this.labelText,
    this.validator,
    this.onSaved,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // 这里我们假设插件提供了一个名为`CustomTextField`的组件
    return TextFormField(
      decoration: InputDecoration(labelText: Text(labelText)),
      validator: validator,
      onSaved: onSaved,
      // 可能的额外自定义属性或行为可以在这里添加
    );
  }
}

class CustomPasswordFormField extends StatelessWidget {
  final String labelText;
  final FormFieldValidator<String?>? validator;
  final FormFieldSetter<String?>? onSaved;

  const CustomPasswordFormField({
    Key? key,
    required this.labelText,
    this.validator,
    this.onSaved,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // 假设插件也提供了一个名为`CustomPasswordField`的组件,但这里为了简化,我们还是使用TextFormField
    // 并添加一些密码字段常见的特性,如遮挡输入字符
    return TextFormField(
      decoration: InputDecoration(labelText: Text(labelText)),
      obscureText: true,  // 遮挡输入字符
      validator: validator,
      onSaved: onSaved,
      // 可能的额外自定义属性或行为可以在这里添加
    );
  }
}

在这个示例中,我们假设custom_form_w插件提供了CustomTextFormFieldCustomPasswordFormField两个组件,但为简化起见,我们实际上使用的是Flutter自带的TextFormField。如果custom_form_w提供了特定的自定义组件和API,你应该根据插件的文档进行相应的调整。

请确保查看custom_form_w的官方文档以获取准确的组件名称和使用方法,并根据需要调整上述代码。

回到顶部