首页 > 解决方案 > 如何在 Flutter 中退出键盘后更改小部件

问题描述

我正在尝试设计一个应用程序,我的注册表似乎很大。我希望能够在键盘退出后删除徽标,并在键盘消失后将其取回。换句话说,我想要一个小部件对键盘做出反应。有可能吗?

下面是报名表

import 'package:flutter/material.dart';
import 'package:flutter_signin_button/button_list.dart';
import 'package:tembea/components/rounded_button.dart';
import 'package:tembea/constants.dart';
import 'package:flutter_signin_button/flutter_signin_button.dart';

class RegistrationScreen extends StatelessWidget {
  const RegistrationScreen({Key? key}) : super(key: key);
  static const String id =  'registration_screen';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      body: Padding(
        padding:const EdgeInsets.symmetric(horizontal: 24.0),
        child: Flexible(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Hero(
                      tag: 'logo',
                      child: SizedBox(
                        height: 100,
                          child: Image.asset('images/logo.png'),
                      ),
                  ),
                ],
              ),
              const SizedBox(
                height: 20.0,
              ),
               TextField(
                textAlign: TextAlign.center,
                style:const TextStyle(
                  color: Colors.white,
                ),
                onChanged: (value){},
                 decoration: kInputDecoration.copyWith(
                   hintText: 'Enter username',
                 ),
              ),
              const SizedBox(
                height: 20.0,
              ),

              TextField(
                textAlign: TextAlign.center,
                style: const TextStyle(
                  color: Colors.white,
                ),
                onChanged: (value){},
                decoration: kInputDecoration.copyWith(
                  hintText: 'Enter email',
                ),
              ),
              const SizedBox(
                height: 20.0,
              ),
              TextField(
                textAlign: TextAlign.center,
                obscureText: true,
                style: const TextStyle(
                  color: Colors.white,
                ),
                onChanged: (value){},
                decoration: kInputDecoration.copyWith(
                  hintText: 'Enter password',
                ),
              ),
              const SizedBox(
                height: 20.0,
              ),
              TextField(
                textAlign: TextAlign.center,
                obscureText: true,
                style: const TextStyle(
                  color: Colors.white,
                ),
                onChanged: (value){},
                decoration: kInputDecoration.copyWith(
                  hintText: 'Confirm password',
                ),
              ),
              const SizedBox(
                height: 20.0,
              ),
              RoundedButton(
                buttonName: 'Register',
                color: Colors.lightGreen,
                onPressed: (){},
              ),
              const SizedBox(
                height: 20,
              ),
              SignInButton(
              Buttons.Google,
              text: 'Sign Up with Google',
              onPressed: (){},
              ),
              const SizedBox(
                height: 20,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  const Text(
                      'Already have an account?',
                    style: TextStyle(
                      color: Colors.white,
                    ),
                  ),
                  const SizedBox(
                    height: 5.0,
                  ),
                  TextButton(
                      onPressed: (){},
                      child: const Text(
                          'Sign In',
                        style: TextStyle(
                          color: Colors.green,
                        ),
                      ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}

请告诉我

标签: flutter

解决方案


您可以检查键盘是否不可见,如果是,则显示您的徽标小部件:

            if(MediaQuery.of(context).viewInsets.bottom==0)
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Hero(
                      tag: 'logo',
                      child: SizedBox(
                        height: 100,
                          child: Image.asset('images/logo.png'),
                      ),
                  ),
                ],
              ),

推荐阅读