【发布时间】:2022-11-01 12:57:00
【问题描述】:
我想将我的输入类型作为密码,所以我希望它被审查。我想使用“obscureText:true”,所以它可以工作,但是当我想将它声明为一个函数并添加一个按钮时,该按钮将在您单击时显示密码并在您再次单击时隐藏。我正在尝试添加 suffix 属性和 IconButton();但它不工作。
bool hide() {
return true;
}
@override
Widget build(BuildContext context){
return Form(
key: loginClass,
...
Padding(
padding: const EdgeInsets.symmetric(horizontal: 32, vertical: 8),
child: TextFormField(
controller: password,
obscureText: hide(),
decoration: const InputDecoration(
labelText: "Password",
hintText: "Enter your password",
border: OutlineInputBorder(),
icon: Icon(Icons.lock),
// Suffix line.
suffix: IconButton(
icon: Icon(Icons.visibility_rounded),
onPressed: !hide, // Error line.
),
),
validator: (String? value) {
if (value == null || value.isEmpty) {
return 'Please enter your password';
}
return null;
},
),
),
...
}
错误:
Performing hot restart...
Syncing files to device Android SDK built for x86...
lib/login.dart:107:31: Error: Not a constant expression.
onPressed: !hide,
^^^^
lib/login.dart:107:31: Error: A value of type 'bool Function()' can't be assigned to a variable of type 'bool'.
onPressed: !hide,
^
lib/login.dart:107:30: Error: The argument type 'bool' can't be assigned to the parameter type 'void Function()?'.
onPressed: !hide,
^
Restarted application in 218ms.
我想添加一个图标按钮。一旦你点击它,密码将被显示,但如果你再次点击将被审查。
【问题讨论】:
标签: android flutter flutter-android