Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Show TextField above keyboard

I know there are many opened questions on this, I’ve tried many, but none worked in my case.. So I want to have the TextField pop up above the keyboard, then go back to its original position, when keyboard is not showing.

Original position:

Original position

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

When the keyboard pops up, my screen overflows like so:

Overflowed screen

Code:


    @override
      Widget build(BuildContext context) {
        return Scaffold(
          resizeToAvoidBottomInset: true,
          appBar: AppBar(
            title: Text(
              'Chat',
              style: TextStyle(
                  fontFamily: 'Chalet',
                  fontWeight: FontWeight.w300,
                  fontSize: widget.deviceWidth * 0.06),
            ),
          ),
          backgroundColor: whitePrimary,
          body: Column(
            children: [
              SizedBox(
                height: widget.deviceHeight * 0.8,
                child: ListView.builder(
                    itemCount: _messages.length,
                    itemBuilder: (BuildContext context, int index) {
                      return ListView(
                        children: [Text(_messages[index])],
                      );
                    }),
              ),
              const Divider(height: 1.0),
              _buildTextComposer()
            ],
          ),
        );
      }
    
      Widget _buildTextComposer() {
        return SizedBox(
          width: widget.deviceWidth,
          child: TextField(
            controller: _textFieldController,
            decoration: InputDecoration(
              border: InputBorder.none,
              focusedBorder: InputBorder.none,
              enabledBorder: InputBorder.none,
              errorBorder: InputBorder.none,
              disabledBorder: InputBorder.none,
              filled: true,
              fillColor: whitePrimary,
              suffixIcon: IconButton(
                icon: const Icon(
                  Icons.send_rounded,
                  color: orangePrimary,
                ),
                onPressed: () {},
              ),
              hintStyle: TextStyle(fontSize: widget.deviceWidth * 0.03),
              hintText: 'Write a message...',
            ),
          ),
        );
      }

where deviceHeight
and deviceWidth

    final size = MediaQuery.of(context).size;
    
        double deviceWidth = size.width;
        double deviceHeight = size.height;

>Solution :

Don’t use device height from MediaQuery to set the ListView height, but instead, wrap the ListView with Expanded.

Column(
  children: [
    Expanded(
      child: ListView.builder(
        // ...
      ),
    // ... other children
  ],
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading