【Flutter】三十六、Flutter解决点击非输入框时关闭键盘问题及TextFormField焦点切换问题

在这里插入图片描述

Form(
      key: _key,
      child: Column(
        children: <Widget>[
          TextFormField(
            controller: _userController,
            focusNode: _userFocusNode,
            decoration: InputDecoration(
                icon: Icon(Icons.person_outline, size: 35.0, color: Theme.of(context).primaryColor),
                labelText: '用户名'
            ),
            validator: (value) {
              if (value.isEmpty) {
                return '用户名不能为空';
              }
              return null;
            },
            onSaved: (value) {
              setState(() {
                _userName = value;
              });
            },
          ),
          SizedBox(height: 10.0,),
          TextFormField(
            controller: _passwordController,
            focusNode: _passwordFocusNode,
            obscureText: true,
            decoration: InputDecoration(
                icon: Icon(Icons.lock_outline, size: 35.0, color: Theme.of(context).primaryColor),
                labelText: '密码'
            ),
            onSaved: (value) {
              setState(() {
                _password = value;
              });
            },
            validator: (value) {
              if (value.isEmpty) {
                return '密码不能为空';
              }
              return null;
            },
          ),
          SizedBox(height: 40.0,),
          FlatButton(
            textColor: Colors.white,
            child: Container(
              alignment: Alignment.center,
              width: width*0.6,
              height: 40,
              decoration: BoxDecoration(
                boxShadow: [
                  BoxShadow(color: Colors.grey,offset: Offset(1, 1), blurRadius: 8, spreadRadius: -5),
                  BoxShadow(color: Colors.grey, offset: Offset(-1, -1), blurRadius: 8, spreadRadius: -5),
                  BoxShadow(color: Colors.grey, offset: Offset(1, -1), blurRadius: 8, spreadRadius: -5),
                  BoxShadow(color: Colors.grey, offset: Offset(-1, 1), blurRadius: 8, spreadRadius: -5)
                ],
                borderRadius: BorderRadius.circular(40),
                gradient: LinearGradient(
                  colors: [
                    Color(0xFF498CF7),
                    Color(0xFF6D7AF1)
                  ],
                  begin: Alignment.centerLeft,
                  end: Alignment.centerRight
                )
              ),
              child: Text(
                '登录',
                style: TextStyle(
                  letterSpacing: 40.0,
                  fontSize: 18.0
                ),
              ),
            ),
            onPressed: () {
              if (_key.currentState.validate()) {
                _key.currentState.save();
                print(_userName);
                print(_password);
              }
            },
          )
        ],
      ),
    )

    如图上方是一个登录表单,当在输入框中输入文字后,点击空白处是无法将键盘收回的,为解决这一问题可以在Form外面包裹一层GestureDetector,并在onTap中调用FocusScope.of(context).requestFocus(blankNode)方法即可。

FocusNode blankNode = FocusNode();
...
Widget build () {
	reutrn GestureDetector(
		onTap: () {
			FocusScope.of(context).requestFocus(blankNode);
		},
		...
	);
}

    如果当输入完用户名后,要求焦点自动跳至密码框,则可以在用户名输入框的onEditingComplete事件中调用FocusScope.of(context).requestFocus(_passwordFocusNode);
    修改后的效果如下:
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MAXLZ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值