简体   繁体   中英

Flutter:Keyboard not show when focus TextField

I use "TextField" in my flutter project.In simulator iPhone SE 2nd,the keyboard show in normal.But in simulator iPhone 11 Pro,the keyboard doesn't show.Is there something wrong in my code?Thanks!

class _LoginWidgetState extends State<LoginWidget> {
  final TextEditingController _userNameController = new TextEditingController();
  final TextEditingController _passwordController = new TextEditingController();

  @override
  Widget build(BuildContext context) {
    _userNameController.addListener(() {});
    _passwordController.addListener(() {});

    return Container(
      width: widget.parentSize.width / 1.5,
      height: widget.parentSize.height / 2,
      child: Column(
        children: [
          TextField(
            autofocus: true,
            controller: _userNameController,
            decoration: InputDecoration(
              prefixIcon: Icon(Icons.people),
              hintText: '请输入用户名',
            ),
          ),
          TextField(
            autofocus: false,
            obscureText: true,
            controller: _passwordController,
            decoration: InputDecoration(
              prefixIcon: Icon(Icons.lock),
              hintText: '请输入密码',
            ),
          ),
          SizedBox(
            width: double.infinity,
            child: RaisedButton(
              color: Color(ARGB.BTN_REGISTER),
              onPressed: () {},
              child: Text('注册'),
            ),
          ),
        ],
      ),
    );
  }
}

Try this 尝试这个看看

FocusNode focusNode;

class _LoginWidgetState extends State<LoginWidget> {
  final TextEditingController _userNameController = new TextEditingController();
  final TextEditingController _passwordController = new TextEditingController();

  @override
  Widget build(BuildContext context) {
    _userNameController.addListener(() {});
    _passwordController.addListener(() {});

    return GestureDetector(
    onTap: () {
    FocusScopeNode currentFocus = FocusScope.of(context);
        if (!currentFocus.hasPrimaryFocus) {
          currentFocus.unfocus();
        }}

    child: Container(
      width: widget.parentSize.width / 1.5,
      height: widget.parentSize.height / 2,
      child: Column(
        children: [
          TextField(
            focusNode: focusNode,
            autofocus: true,
            controller: _userNameController,
            decoration: InputDecoration(
              prefixIcon: Icon(Icons.people),
              hintText: '请输入用户名',
            ),
          ),
          TextField(
            autofocus: false,
            obscureText: true,
            controller: _passwordController,
            decoration: InputDecoration(
              prefixIcon: Icon(Icons.lock),
              hintText: '请输入密码',
            ),
          ),
          SizedBox(
            width: double.infinity,
            child: RaisedButton(
              color: Color(ARGB.BTN_REGISTER),
              onPressed: () {},
              child: Text('注册'),
            ),
          ),
        ],
      ),
    ));
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM