【问题标题】:How can i hide image widget when keyboard pops in flutter?当键盘弹出时如何隐藏图像小部件?
【发布时间】:2020-10-03 20:37:11
【问题描述】:

大家好,我需要你的帮助,我只想在键盘弹出时隐藏图像小部件, 但是当我单击文本字段并打开键盘时,它会一直关闭,你能帮我解决这个问题吗?我附上我的代码谢谢!

import 'package:flutter/material.dart';
import 'package:ovsursadmin/screens/authentication/login_card.dart';

class Login extends StatelessWidget {

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      backgroundColor: Colors.blue[900],
      body: Container(
        decoration: BoxDecoration(
          image: DecorationImage(
            image: ExactAssetImage('assets/bg-admin.png'),
            fit: BoxFit.cover,
            colorFilter: ColorFilter.mode(Colors.blue[900].withOpacity(0.1), BlendMode.dstATop),
          )
        ),
        child: SafeArea(
          child: Container(
            child : Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                SizedBox(height: 10.0,),
// this code i want to fix it keeps closing the keyboard when pops
                MediaQuery.of(context).viewInsets.bottom != 0 ? Container() : Image.asset('assets/urs.png',
                height: 100,
                width: 100,),
                SizedBox(height: 25.0,),
                Text('Admin Login', style: TextStyle(color: Colors.white),),
                Text('Online Voting System', style: TextStyle(color: Colors.white),),
                LoginCard(),
              ],
            )
          ),
        ),
      ),
    );
  }

}

This is my problem it keeps closing when you click the text field and open the keyboard

【问题讨论】:

  • 在您的情况下通过关闭或打开方式弹出?
  • 是的,打开键盘,

标签: image flutter keyboard widget hide


【解决方案1】:

使用

添加依赖:

依赖: 键盘可见性:^0.5.6

代码:

   bool _isKeyboardOpen = false;
    @protected
    void initState() {
      super.initState();

      KeyboardVisibilityNotification().addNewListener(
        onChange: (bool visible) {
        setState(() {
          _isKeyboardOpen = visible;
          });

        },
      );
    }

在构建小部件中使用代码喜欢:

选项1:

_isKeyboardOpen ? Container() : Image.asset('assets/urs.png',
                height: 100,
                width: 100,)

选项2:

  if(!_isKeyboardOpen)
      Image.asset('assets/urs.png', height: 100, width: 100,)

【讨论】:

  • 点击文本框时键盘一直关闭,就像上图一样,我想我是在刷新小部件,这就是它关闭键盘的原因?
  • 打开键盘后立即关闭
  • 在这种情况下添加选项 2
  • 单击文本字段时仍然关闭键盘先生!谢谢你
  • @NehryDedoro:您能否提供更多代码,您当前的代码无法重现您的问题。
猜你喜欢
  • 2019-06-08
  • 1970-01-01
  • 2023-04-02
  • 2011-12-13
  • 1970-01-01
  • 1970-01-01
  • 2021-10-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多