dartflutterflutter-layout

How to Move bottomsheet along with keyboard which has textfield(autofocused is true)?


I'm trying to make a bottomsheet that has a text field and autofocus is set to true so that the keyboard pops up. But, bottomsheet is overlapped by the keyboard. Is there a way to move bottomsheet above the keyboard?

Padding(
  padding:
      EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
  child: Column(children: <Widget>[
    TextField(
      autofocus: true,
      decoration: InputDecoration(hintText: 'Title'),
    ),
    TextField(
      decoration: InputDecoration(hintText: 'Details!'),
      keyboardType: TextInputType.multiline,
      maxLines: 4,
    ),
    TextField(
      decoration: InputDecoration(hintText: 'Additional details!'),
      keyboardType: TextInputType.multiline,
      maxLines: 4,
    ),]);

Solution

    1. All you need is to use Keyboard padding using MediaQuery.of(context).viewInsets.bottom

    2. For more insurance, set isScrollControlled = true of the BottomSheetDialog this will allow the bottom sheet to take the full required height.

    showModalBottomSheet(
            context: context,
            isScrollControlled: true,
            builder: (context) => Padding(
              padding: EdgeInsets.only(
                            bottom: MediaQuery.of(context).viewInsets.bottom),
              child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      Padding(
                        padding: const EdgeInsets.symmetric(horizontal: 12.0),
                        child: Text('Enter your address',
                            style: TextStyles.textBody2),
                      ),
                      SizedBox(
                        height: 8.0,
                      ),
                      TextField(
                          decoration: InputDecoration(
                            hintText: 'adddrss'
                          ),
                          autofocus: true,
                        ),                 
                    ],
                  ),
            ));
    

    Flutter 2.2 breaks the changes again!” Now you need to give bottom padding once again so the keyboard doesn't overlap the bottomsheet.

    IF you have RenderFlex overflowed on bottom just wrap your code with:

    SingleChildScrollView()
    

    it's focused automatically with your TextField() !