96 lines
3.2 KiB
Dart
96 lines
3.2 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
class PasswordInputField extends StatefulWidget {
|
|
final Function(String?) onSaved;
|
|
final String? Function(String?)? validator;
|
|
final bool isObscure;
|
|
final Function(bool) onObscureChanged;
|
|
final bool loginType;
|
|
final String hintText;
|
|
final String titleText;
|
|
final TextEditingController? controller; // 添加控制器参数
|
|
|
|
|
|
const PasswordInputField({
|
|
super.key,
|
|
required this.onSaved,
|
|
this.validator,
|
|
required this.isObscure,
|
|
required this.onObscureChanged,
|
|
required this.titleText,
|
|
this.hintText="请输入密码",
|
|
this.loginType = true,
|
|
this.controller, // 初始化控制器
|
|
|
|
});
|
|
|
|
@override
|
|
_PasswordInputFieldState createState() => _PasswordInputFieldState();
|
|
}
|
|
|
|
class _PasswordInputFieldState extends State<PasswordInputField> {
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
if (widget.loginType) {
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
const SizedBox(height:20),
|
|
// 显示“密码”文本
|
|
Text(
|
|
widget.titleText,
|
|
style:const TextStyle(color: Colors.white, fontSize: 16),
|
|
),
|
|
// 密码输入框
|
|
TextFormField(
|
|
controller: widget.controller, // 使用控制器
|
|
obscureText: widget.isObscure,
|
|
onSaved: widget.onSaved,
|
|
validator: widget.validator,
|
|
style: TextStyle(color: Colors.white.withOpacity(0.5),fontSize: 14,),
|
|
decoration: InputDecoration(
|
|
hintText: widget.hintText,
|
|
hintStyle: TextStyle(color: Colors.white.withOpacity(0.5),fontSize: 14,), // 提示文字颜色
|
|
border: const UnderlineInputBorder(), // 只显示下划线
|
|
focusedBorder: const UnderlineInputBorder(
|
|
borderSide: BorderSide(color: Colors.white), // 聚焦时下划线颜色
|
|
),
|
|
suffixIcon: IconButton(
|
|
icon: Icon(
|
|
widget.isObscure ? Icons.visibility_off : Icons.visibility,
|
|
color: Colors.white.withOpacity(0.3),
|
|
),
|
|
onPressed: () => widget.onObscureChanged(!widget.isObscure),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
|
|
} else {
|
|
return const SizedBox.shrink(); // 不显示密码输入框
|
|
}
|
|
}
|
|
|
|
}
|
|
// Widget build(BuildContext context) {
|
|
// return TextFormField(
|
|
// obscureText: widget.isObscure,
|
|
// onSaved: widget.onSaved,
|
|
// validator: widget.validator,
|
|
// style: TextStyle(color: Colors.white.withOpacity(0.5)),
|
|
// decoration: InputDecoration(
|
|
// labelText: "密码",
|
|
// labelStyle: const TextStyle(color: Colors.white, fontSize: 16),
|
|
// hintText: "请输入密码",
|
|
// helperStyle: const TextStyle(color: Color(0xff7adcd9)),
|
|
// suffixIcon: IconButton(
|
|
// icon: Icon(
|
|
// widget.isObscure ? Icons.visibility_off : Icons.visibility,
|
|
// color: widget.isObscure ? Colors.grey : Theme.of(context).iconTheme.color,
|
|
// ),
|
|
// onPressed: () => widget.onObscureChanged(!widget.isObscure),
|
|
// ),
|
|
// ),
|
|
// );
|
|
// } |