【发布时间】:2021-04-05 09:57:00
【问题描述】:
我一直在尝试开发一个应用程序并为其注册,我不确定如何以及在何处添加隐藏/查看密码的切换开关。它是在晦涩难懂的文字下吗?如果是这样,我如何将它的条件添加到我的代码中? (使用表单创建用于输入注册详细信息。)
实现自定义文本字段的表单代码:
_signUpForm() {
return Container(
padding: EdgeInsets.symmetric(horizontal: 25, vertical: 30),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(5)),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
CustomRoundedTextField(
label: 'Email',
onChange: (val) {
email = val;
},
),
CustomRoundedTextField(
label: 'Password',
isPassword: true,
onChange: (val) {
password = val;
},
),
SizedBox(height: 40),
CustomBlueRoundedButton(
child: Text(
'Register Account',
style: TextStyle(color: Colors.white),
),
onPressed: _createAccount,
),
SizedBox(height: 15,),
Container(
child: Center(
child: Text("Already have an account?"),
),
),
SizedBox(height: 5),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
InkWell(
onTap: openLoginPage,
child: Text("Login", style: TextStyle(
color: Color(0xff1A3C90),
fontWeight: FontWeight.w700
),),
)
],
)
],
),
);
}
自定义文本字段类代码:
class CustomRoundedTextField extends StatelessWidget {
final label;
final onChange;
final isPassword;
final bottomPadding;
final textCapitalization;
final inputType;
final controller;
final iconData;
CustomRoundedTextField(
{
this.iconData,
this.controller,
this.inputType = TextInputType.text,
this.label,
this.onChange,
this.isPassword = false,
this.bottomPadding = 16,
this.textCapitalization = TextCapitalization.none});
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(bottom: 16),
child: TextField(
controller: controller,
keyboardType: inputType,
textCapitalization: textCapitalization,
obscureText: isPassword,
style:
TextStyle(fontSize: 15, color: Colors.black),
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(15),
borderSide: BorderSide(
color: Colors.black54,
),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(15),
borderSide: BorderSide(
color: Colors.black,
),
),
labelText: label,
labelStyle: TextStyle(
fontSize: 15,
color: Colors.black,
fontWeight: FontWeight.w600,
),
),
onChanged: onChange,
),
);
}
}
【问题讨论】:
-
我能想到的最简单的方法是添加一个变量和一个按钮来切换显示密码。然后当按下按钮时, setState(show = !show) 来切换它。最后,将
ispassword字段从 true 更改为 show。