feat:增加表单组件

This commit is contained in:
2025-07-07 20:02:48 +08:00
parent 570a141133
commit e2ebe917ac
9 changed files with 155 additions and 66 deletions

View File

@@ -1,4 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:form_builder_validators/form_builder_validators.dart';
class LoginPage extends StatefulWidget {
const LoginPage({super.key});
@@ -21,7 +23,7 @@ class _LoginPage extends State<LoginPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
backgroundColor: Colors.grey[100],
body: Form(
key: _formKey,
autovalidateMode: AutovalidateMode.onUserInteraction,
@@ -84,40 +86,36 @@ class _LoginPage extends State<LoginPage> {
}
Widget buildUsernameTextField() {
return TextFormField(
return FormBuilderTextField(
name: 'username',
decoration: InputDecoration(
labelText: '账号',
prefixIcon: Icon(Icons.person),
hintText: "请输入账号",
border: OutlineInputBorder(),
floatingLabelBehavior: FloatingLabelBehavior.always,
filled: true,
fillColor: Colors.grey[100],
fillColor: Colors.white,
),
validator: (v) {
if (v!.isEmpty) {
return '请输入账号';
}
},
validator: FormBuilderValidators.required(),
onSaved: (v) => _username = v!,
);
}
Widget buildPasswordTextField(BuildContext context) {
return TextFormField(
return FormBuilderTextField(
name: 'password',
obscureText: _isObscure, // 是否显示文字
onSaved: (v) => _password = v!,
validator: (v) {
if (v!.isEmpty) {
return '请输入密码';
}
},
validator: FormBuilderValidators.required(),
decoration: InputDecoration(
labelText: "密码",
prefixIcon: Icon(Icons.lock),
hintText: "请输入密码",
border: OutlineInputBorder(),
filled: true,
fillColor: Colors.grey[100],
fillColor: Colors.white,
floatingLabelBehavior: FloatingLabelBehavior.always,
suffixIcon: IconButton(
icon: Icon(Icons.remove_red_eye, color: _eyeColor),
onPressed: () {