Flutter文本输入优化插件easy_textformfield的使用
Flutter文本输入优化插件easy_textformfield的使用
特性
- 简单易用
- 易于理解
- 容易实现
- 可自定义
- 易于维护
- 调试方便
开始使用
TODO: 列出前提条件并提供或指向如何开始使用该包的信息。
使用方法
EasyTextFormField(
hint: const TFFHint(title: 'Password'),
header: const TFFHeader(title: 'Password'),
prefix: PrefixWithIconImage(iconImage: "assets/images/password.png"), // TODO: 替换为你自己的资源图像
validation: PasswordValidator.instance.validate,
secureType: SecureType.Toggle,
controller: _.passwordController,
onComplete: (){
// TODO: 登录功能在此触发
},
),
其他信息
请注意以下几点:
- 此包仍在开发中…
- 目前仅支持英文,但将在接下来的更新中支持[英文][法文][阿拉伯文]
- 如果在除已提及情况外的其他地方发现任何错误,请联系开发者:[ahmed.elsherbiny2020@gmail.com]
- 如欲贡献代码,请通过同一邮箱与开发者联系
-- 感谢使用此包 <3 --
** 由Sherbini用心制作 **
示例代码
import 'package:easy_textformfield/TextFields/Default/Implementation/Prefix/Implementation/imports_prefix.dart';
import 'package:easy_textformfield/TextFields/Default/Implementation/Text/Implementation/imports_text.dart';
import 'package:easy_textformfield/TextFields/Options/imports_options.dart';
import 'package:easy_textformfield/Validation/Implementation/PasswordValidator/password_validator.dart';
import 'package:easy_textformfield/Validation/Implementation/PhoneValidator/phone_validator.dart';
import 'package:easy_textformfield/easy_textformfield.dart';
import 'package:flutter/material.dart';
void main() {
return runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
return const MaterialApp(
debugShowCheckedModeBanner: false,
title: "Testing Easy Text Form Field",
home: LoginPage(),
);
}
}
class LoginPage extends StatefulWidget {
const LoginPage({Key? key}) : super(key: key);
[@override](/user/override)
State<LoginPage> createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
final TextEditingController phoneController = TextEditingController();
final TextEditingController passwordController = TextEditingController();
[@override](/user/override)
void initState() {
super.initState();
}
[@override](/user/override)
void dispose() {
phoneController.dispose();
passwordController.dispose();
super.dispose();
}
[@override](/user/override)
Widget build(BuildContext context) {
var node = FocusScope.of(context);
return Scaffold(
appBar: AppBar(
title: const Text("Testing Easy Text Form Field"),
),
body: SingleChildScrollView(
child: Form(
key: formKey,
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 16,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SizedBox(height: 24),
Text("Hello again", style: Theme.of(context).textTheme.headline6),
const SizedBox(height: 34),
Text("Enter your phone number and password", style: Theme.of(context).textTheme.subtitle1),
const SizedBox(height: 34),
EasyTextFormField(
hint: const TFFHint(
title: 'your phone number',
),
header: const TFFHeader(title: 'phone number'),
prefix: PrefixWithIconData(iconData: Icons.phone_android,),
validation: PhoneValidator.instance.validate,
controller: phoneController,
onComplete: () {
node.nextFocus();
},
),
const SizedBox(height: 24),
EasyTextFormField(
hint: const TFFHint(
title: 'كلمة المرور',
),
header: const TFFHeader(title: 'كلمة المرور'),
prefix: PrefixWithIconData(iconData: Icons.lock,),
validation: PasswordValidator.instance.validate,
secureType: SecureType.Toggle,
controller: passwordController,
onComplete: () {
node.unfocus();
/// 登录功能在此触发
},
),
],
),
),
),
),
);
}
}
更多关于Flutter文本输入优化插件easy_textformfield的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复
更多关于Flutter文本输入优化插件easy_textformfield的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
easy_textformfield
是一个用于优化 Flutter 文本输入体验的插件。它提供了一些便捷的功能,如自动验证、错误提示、输入格式化等,使得开发者能够更轻松地处理表单输入。
以下是 easy_textformfield
的基本使用方法和一些常见功能的示例。
1. 安装插件
首先,在 pubspec.yaml
文件中添加 easy_textformfield
依赖:
dependencies:
flutter:
sdk: flutter
easy_textformfield: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
安装依赖。
2. 基本使用
easy_textformfield
提供了一个 EasyTextFormField
组件,你可以像使用 TextField
一样使用它。
import 'package:flutter/material.dart';
import 'package:easy_textformfield/easy_textformfield.dart';
class MyForm extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('EasyTextFormField Example'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
EasyTextFormField(
labelText: 'Username',
hintText: 'Enter your username',
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your username';
}
return null;
},
),
SizedBox(height: 16),
EasyTextFormField(
labelText: 'Password',
hintText: 'Enter your password',
obscureText: true,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your password';
}
if (value.length < 6) {
return 'Password must be at least 6 characters';
}
return null;
},
),
SizedBox(height: 24),
ElevatedButton(
onPressed: () {
// 处理表单提交
},
child: Text('Submit'),
),
],
),
),
);
}
}
3. 常见功能
3.1 自动验证
EasyTextFormField
支持自动验证,你只需要在 validator
中提供验证逻辑即可。
EasyTextFormField(
labelText: 'Email',
hintText: 'Enter your email',
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your email';
}
if (!RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$').hasMatch(value)) {
return 'Please enter a valid email';
}
return null;
},
),
3.2 输入格式化
你可以使用 inputFormatters
来格式化输入内容。例如,限制输入为数字:
import 'package:flutter/services.dart';
EasyTextFormField(
labelText: 'Phone Number',
hintText: 'Enter your phone number',
inputFormatters: [
FilteringTextInputFormatter.digitsOnly,
],
),
3.3 自定义错误提示
你可以通过 errorText
属性自定义错误提示:
EasyTextFormField(
labelText: 'Username',
hintText: 'Enter your username',
errorText: 'This field is required',
),
3.4 控制焦点
你可以使用 focusNode
来控制输入框的焦点:
FocusNode _focusNode = FocusNode();
EasyTextFormField(
labelText: 'Username',
hintText: 'Enter your username',
focusNode: _focusNode,
),
4. 完整示例
import 'package:flutter/material.dart';
import 'package:easy_textformfield/easy_textformfield.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: MyForm(),
);
}
}
class MyForm extends StatelessWidget {
final _formKey = GlobalKey<FormState>();
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('EasyTextFormField Example'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Form(
key: _formKey,
child: Column(
children: [
EasyTextFormField(
labelText: 'Username',
hintText: 'Enter your username',
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your username';
}
return null;
},
),
SizedBox(height: 16),
EasyTextFormField(
labelText: 'Password',
hintText: 'Enter your password',
obscureText: true,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your password';
}
if (value.length < 6) {
return 'Password must be at least 6 characters';
}
return null;
},
),
SizedBox(height: 24),
ElevatedButton(
onPressed: () {
if (_formKey.currentState!.validate()) {
// 处理表单提交
}
},
child: Text('Submit'),
),
],
),
),
),
);
}
}