Home > other >  How to change widget once the keyboard is out in Flutter
How to change widget once the keyboard is out in Flutter

Time:10-31

I am trying to design an app and my registration form appear to be quite big. I would like to be able to remove the logo once the keyboard is out and get it back once the keyboard disappear. In other term I want a widget to react to the keyboard. Will it be possible?

Below is the registration form

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,
                        ),
                      ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Kindly let me know

CodePudding user response:

You could check if the keyboard is not visible, and if so then show your logo widget:

            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'),
                      ),
                  ),
                ],
              ),
  • Related