当键盘弹出时如何隐藏图像小部件?

问题描述 投票:0回答:1

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

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
投票

使用中

添加依赖项:

依赖关系: keyboard_visibility:^ 0.5.6

代码:

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

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

        },
      );
    }

在构建小部件中使用代码,如:

Option1:

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

Option2:

  if(!_isKeyboardOpen)
      Image.asset('assets/urs.png', height: 100, width: 100,)
© www.soinside.com 2019 - 2024. All rights reserved.