【问题标题】:Can I use two widgets for the same key?我可以为同一个键使用两个小部件吗?
【发布时间】:2021-08-09 19:15:21
【问题描述】:

当我触摸按钮时,我试图在终端中查看密码和用户电子邮件,但它给了我一些错误。

  • 我尝试使用两个键,但它给了我错误
  • 我试图在我的小部件上方放置一个 body: form,但我没有脚手架,因此它不起作用,而且我尝试更改为脚手架,但情况更糟,而且我没有更多背景。

所以有一种方法可以显示密码和电子邮件,它允许使用多种密钥的 _formKey1 和 _formKey2 系统? 我认为问题出在我的 globalkey 上

这是我的一些代码

class _BodyState extends State<Body> {
  final _formKey =new GlobalKey<FormState>();

  late String _email;
  late String _password;

@override
  Widget build (BuildContext context) {
    return Background(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text("SIGNUP", style: TextStyle(
                color: Colors.white,
                fontWeight: FontWeight.bold,
                fontSize: 20),
            ),
            Container(child: Image.asset('assets/ctlogo.png', height: 200),
            ),
            RoundedInputField(
              key: _formKey,
                hintText: "Your Email",
                onChanged:(value) => setState(() => _email = value),
            ),
            RoundedPasswordField(
              key: _formKey,
              onChanged: (value) => setState(() => _password = value),
            ),
            Container(margin: EdgeInsets.symmetric(vertical: 20),
              width: 300,
              child: ClipRRect(
                borderRadius: BorderRadius.circular(29),
                // ignore: deprecated_member_use
                child: FlatButton(
                  padding: EdgeInsets.symmetric(vertical: 14, horizontal: 40),
                  color: DarkTurquoise,
                  onPressed: (){print(_email); print(_password);
                    Navigator.push(
                      context,

出现错误“相同键的小部件太多”和“LateInitializationError: Field '_password@30293343' has not been initialized

还有我的 rounded_pa​​ssword_field 代码

class RoundedPasswordField extends StatefulWidget {

  final ValueChanged<String> onChanged;
  const RoundedPasswordField({
    required this.onChanged,
    Key? key,
  }) : super(key: key);

  @override
  _RoundedPasswordFieldState createState() => _RoundedPasswordFieldState();
}
class _RoundedPasswordFieldState extends State<RoundedPasswordField> {
  bool _isSecret = true;

  @override
  Widget build(BuildContext context) {
    return TextFieldContainer(
      child: TextField(
        obscureText: _isSecret,
        decoration: InputDecoration(
          hintText: "Password",
          border: InputBorder.none,
          icon: Icon(
            Icons.lock,
            color: DarkTurquoise,
          ),
          suffixIcon: InkWell(
            onTap: () =>
                setState(() => _isSecret = !_isSecret),
            child: Icon(!_isSecret
                ? Icons.visibility
                : Icons.visibility_off, color: DarkTurquoise),
          ),
        ),
      ),
    );
  }
}

【问题讨论】:

    标签: flutter email dart passwords key


    【解决方案1】:

    不,两个小部件不能共享GlobalKey。此外,您的 GlobalKey 只能在 Form 小部件中使用,因为它包含 FormState

    在这种情况下,您可能不需要使用GlobalKey,因为您没有使用它。相反,请尝试删除密钥,然后再次测试您的代码。

    此外,只有在您确定它们将被正确初始化时才使用late fields。在这种情况下,请尝试将它们替换为:

    String _email = '';
    String _password = '';
    

    【讨论】:

    • 谢谢它的工作,我没有看到密码字段是正常的吗?
    • 什么意思?文本是否被点遮挡,或者整个字段没有出现?尝试查看 RoundedPasswordField 实现。
    • 在终端运行的应用程序中,我有这个 I/flutter (3141): ajidj@gmail.com I/flutter (3141): (就像你可以看到之后什么都没有,而它应该包含密码
    • 您确定RoundedPasswordField 小部件正在调用onChanged 函数吗?
    • 谢谢你的问题是从那里,我只是放了一个onChanged: widget.onChanged; 就可以了
    猜你喜欢
    • 1970-01-01
    • 2018-01-25
    • 1970-01-01
    • 2015-05-29
    • 2012-06-18
    • 2018-01-07
    • 1970-01-01
    • 1970-01-01
    • 2016-03-19
    相关资源
    最近更新 更多