How to avoid BottomBar to close and result in disposing soft keyboard

38 views Asked by At

When the keyboard opens, it might be causing the bottom sheet to close, which will then result in disposing any widgets inside it.

                    globals.addinfolistitem[widget.shotlistindex]
                            [widget.projectindex]
                        .add('Add');
                    shottypefocusnode.add(FocusNode());
                    print("FocusNode");

This is the one of the optiosn inside the BottomBar,the function is when the item is triggered then craete a new listitem in the container and bottombar will to close and the soft keyboard and cursor will focus on the TextFields. But now the soft keyboard is hidden.


  BottomBar(Widget popupmenu) {
    showModalBottomSheet(
        constraints: BoxConstraints(
          maxHeight: globals.slidingpanelmaxheight.isNaN
              ? 500
              : globals.slidingpanelmaxheight,
          maxWidth: MediaQuery
              .of(context)
              .size
              .width - 10,
        ),
        backgroundColor: Colors.transparent,
        context: context,
        builder: (context) {
          return popupmenu;
        }).then((value) {
      print("popupmenu bottom sheet closed");

      // Set focus on TextField
      if (!myFocusNode.hasFocus) {
        FocusScope.of(context).requestFocus(myFocusNode);
      }
    });
  }

I try to request focus again when the bottom sheet closed but doesnt work. enter image description here

Once the BottomBar items is triggered then add the selected item to the container and soft keyboard focus on the Textfields

enter image description here

1

There are 1 answers

0
TheSylar On

First of all, I think I do not fully understand the problem, but as far as I understand, I hope that if you make the following structure according to one of the appropriate state management methods, it will work.

import 'package:flutter/material.dart';

class MyPage extends StatefulWidget {
  @override
  _MyPageState createState() => _MyPageState();
}

class _MyPageState extends State<MyPage> {
  bool isBottomBarOpen = false;

  void toggleBottomBar() {
    setState(() {
      isBottomBarOpen = !isBottomBarOpen;
    });

    if (isBottomBarOpen) {
      FocusScope.of(context).requestFocus(FocusNode());
    } else {
      FocusScope.of(context).unfocus();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Example'),
      ),
      body: GestureDetector(
        onTap: toggleBottomBar,
        child: Stack(
          children: [
            // Your main content
            // ...
            
            // BottomBar
            if (isBottomBarOpen)
              Container(
                color: Colors.grey,
                height: 100,
                // Add your bottom bar content here
                // ...
              ),
          ],
        ),
      ),
    );
  }
}