Flutter表单处理插件lo_form的使用
Flutter表单处理插件lo_form的使用
低代码和轻量级的Flutter表单库。
关于
LoForm
是一个低代码和轻量级的Flutter表单库,受Formik
启发,但实现方式有所不同。由于TypeScript与Dart的差异,实现方式也不同。LoForm旨在专注于简洁性,并解决其他现有解决方案的大多数缺点和限制。
入门指南
要了解如何开始并查看一些示例,请参阅文档。
许可证
请查看许可证。
完整示例代码
// ignore_for_file: use_build_context_synchronously
import 'package:flutter/material.dart';
import 'package:lo_form/lo_form.dart';
import 'app.dart';
import 'extensions.dart';
import 'fake_api.dart';
void main() => runApp(const App());
class RegisterForm extends StatelessWidget {
/// 用于在网站包中通知表单状态的变化
final ValueChanged<LoFormState>? onStateChanged;
const RegisterForm({
Key? key,
this.onStateChanged,
}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
return LoForm<String>(
onReady: onStateChanged,
onChanged: onStateChanged,
submittableWhen: (status) => status.isValid || status.isSubmitted,
validators: [
LoMatchValidator('Password', 'Confirm'),
LoFormValidator(
(values) {
final username = values.get('Username');
return {
if (username == 'whoami') 'Username': 'Who are you?',
};
},
),
],
onSubmit: (values, setErrors) async {
final client = FakeApi();
final response = await client.register(
username: values.get('Username'),
password: values.get('Password'),
);
if (!response.isError) {
context.showSnackBar('Welcome, ${response.data!}');
return true; // 成功提交
} else if (response.validationErrors == null) {
context.showSnackBar(response.message);
return false; // 提交失败
} else {
// 无效提交(API错误)
setErrors(response.validationErrors!);
return null;
}
},
builder: (form) {
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// 用户名或邮箱输入框
LoTextField(
loKey: 'Username',
initialValue: 'yrn',
validators: [
LoRequiredValidator(), // 必填验证器
LoFieldValidator.any(
[
LoRegExpValidator(r'^[a-z][a-z_]{2,}$'), // 正则表达式验证器
LoRegExpValidator.email(), // 邮箱验证器
],
'Invalid username or email', // 错误信息
)
],
props: const TextFieldProps(
decoration: InputDecoration(
hintText: 'Username or Email',
),
),
),
const SizedBox(height: 16), // 空白间距
// 密码输入框
LoTextField(
loKey: 'Password',
validators: [
LoRequiredValidator(), // 必填验证器
LoLengthValidator.min(6), // 最小长度验证器
],
props: const TextFieldProps(
obscureText: true, // 密文输入
),
),
const SizedBox(height: 16), // 空白间距
// 确认密码输入框
LoTextField(
loKey: 'Confirm',
validators: [
LoRequiredValidator(), // 必填验证器
LoLengthValidator.min(6), // 最小长度验证器
],
props: const TextFieldProps(
obscureText: true, // 密文输入
decoration: InputDecoration(
hintText: 'Confirm Password', // 提示文本
),
),
),
const SizedBox(height: 16), // 空白间距
// 协议复选框
LoCheckbox(
loKey: 'Agreement',
label: const Text('I agree to all the terms and conditions'), // 复选框标签
validators: [
LoFieldValidator((v) => v != true ? 'Required' : null), // 必填验证器
],
),
const Spacer(), // 伸缩空间
// 注册按钮
ElevatedButton(
onPressed: form.submit, // 提交表单
child: const Text('Register'), // 按钮文本
),
],
);
},
);
}
}
更多关于Flutter表单处理插件lo_form的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter表单处理插件lo_form的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
lo_form
是一个用于简化 Flutter 表单处理的插件。它提供了一种简洁的方式来处理表单的验证、提交和数据管理。以下是如何使用 lo_form
的基本步骤:
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 lo_form
依赖:
dependencies:
flutter:
sdk: flutter
lo_form: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来安装依赖。
2. 导入包
在你的 Dart 文件中导入 lo_form
:
import 'package:lo_form/lo_form.dart';
3. 创建表单
使用 LoForm
来创建表单。LoForm
是一个 Form
的包装器,它提供了额外的功能来处理表单数据。
class MyForm extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('LoForm Example'),
),
body: LoForm(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
LoTextField(
name: 'username',
labelText: 'Username',
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your username';
}
return null;
},
),
SizedBox(height: 16),
LoTextField(
name: 'password',
labelText: 'Password',
obscureText: true,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your password';
}
return null;
},
),
SizedBox(height: 24),
ElevatedButton(
onPressed: () {
if (LoForm.of(context).validate()) {
// 表单验证通过,获取表单数据
final formData = LoForm.of(context).getValues();
print(formData);
}
},
child: Text('Submit'),
),
],
),
),
),
);
}
}
4. 表单字段
LoTextField
是一个 TextFormField
的包装器,它自动处理表单数据的绑定和验证。你可以使用 name
属性来标识表单字段,并在提交时获取该字段的值。
5. 表单验证
在 LoTextField
中,你可以使用 validator
属性来定义验证逻辑。如果验证失败,返回一个字符串错误消息;如果验证成功,返回 null
。
6. 提交表单
在提交按钮的 onPressed
回调中,你可以使用 LoForm.of(context).validate()
来验证表单。如果表单验证通过,你可以使用 LoForm.of(context).getValues()
来获取表单数据。
7. 获取表单数据
LoForm.of(context).getValues()
返回一个 Map<String, dynamic>
,其中键是表单字段的 name
,值是用户输入的数据。
完整示例
以下是一个完整的示例,展示了如何使用 lo_form
创建一个简单的登录表单:
import 'package:flutter/material.dart';
import 'package:lo_form/lo_form.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'LoForm Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyForm(),
);
}
}
class MyForm extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('LoForm Example'),
),
body: LoForm(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
LoTextField(
name: 'username',
labelText: 'Username',
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your username';
}
return null;
},
),
SizedBox(height: 16),
LoTextField(
name: 'password',
labelText: 'Password',
obscureText: true,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your password';
}
return null;
},
),
SizedBox(height: 24),
ElevatedButton(
onPressed: () {
if (LoForm.of(context).validate()) {
// 表单验证通过,获取表单数据
final formData = LoForm.of(context).getValues();
print(formData);
}
},
child: Text('Submit'),
),
],
),
),
),
);
}
}