android 颤动TextFormField(修饰:...,后缀:图标按钮(按下时:,));图标按钮打开印刷错误

iovurdzv  于 2022-11-03  发布在  Android
关注(0)|答案(4)|浏览(100)

我想给我的输入类型作为密码,所以我想它作为审查。我想使用“obscureText:true”,所以它可以工作,但当我想声明它是一个函数,并添加一个按钮,将显示密码,当你点击它,并隐藏当你再次点击它。但它不起作用。

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.

我想添加一个图标按钮。一旦你点击它,密码将显示,但如果你再次点击将被审查。

llew8vvj

llew8vvj1#

你必须声明一个布尔变量,并根据可视性按钮来改变它的值。另外,如果你不使用BLOC或GETX,你必须使用一个有状态的小部件来获取屏幕的更新。另外,如果你用函数或setState赋给一个onPressed函数,不要使用const。我已经为你做了一个示例代码...你可以通过这个链接直接在浏览器上试用它:https://zapp.run/edit/flutter-zn206ban306?entry=lib/main.dart&file=lib/main.dart

ymdaylpp

ymdaylpp2#

onPressed可以是nullfunction,您不能将!hide赋给它。创建一个新的状态变量以维护StatefulWidget中的可见性状态。

suffix: IconButton(
  icon: Icon(Icons.visibility_rounded),
  onPressed: () {
    setState(() {
      hideState = !hideState;
    });   
  },
),
jum4pzuy

jum4pzuy3#

suffixIcon: IconButton(
                      icon: Icon(
                          _isObscure
                              ? Icons.visibility_outlined
                              : Icons.visibility_off_outlined,
                          color: HexColor(CustomAppTheme.borderColor),
                        ),
                        onPressed: () {
                          setState(() {
                            _isObscure = !_isObscure;
                          });
                        },
                      ),
ncecgwcz

ncecgwcz4#

import 'package:flutter/material.dart';

void main() {
  runApp(const MaterialApp(
    debugShowCheckedModeBanner: false,
    home: Pass(),
  ));
}

class Pass extends StatefulWidget {
  const Pass({Key? key}) : super(key: key);

  @override
  State<Pass> createState() => _PassState();
}

class _PassState extends State<Pass> {
  bool hidePassword = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: TextFormField(
          obscureText: hidePassword,
          decoration: InputDecoration(
            border: const OutlineInputBorder(),
            labelText: 'Password',
            hintText: 'Enter Password',
            suffixIcon: IconButton(
              icon: Icon(
                hidePassword ? Icons.visibility : Icons.visibility_off,
              ),
              onPressed: () {
                setState(() {
                  hidePassword = !hidePassword;
                });
              },
            ),
          ),
        ),
      ),
    );
  }
}

那你可以试试这个

相关问题