When the keyboard appears, the Flutter widgets resize. How to prevent this?

后端 未结 9 1231
灰色年华
灰色年华 2020-12-02 05:51

I have a Column of Expanded widgets like this:

 return new Container(
      child: new Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
               


        
相关标签:
9条回答
  • 2020-12-02 06:42

    My suggestion is to use resizeToAvoidBottomInset: false anyway to prevent widgets from resizing if the keyboard suddenly appears on the screen. For example, if a user uses Facebook chat heads while in your app.

    To prevent the keyboard from overlaying widgets, on screens where you need it, I suggest the following approach, where is the height of SingleChildScrollView reduced to the height of the available space. In this case, SingleChildScrollView also scrolls to the focused widget.

    final double screenHeight = MediaQuery.of(context).size.height;
    final double keyboardHeight = MediaQuery.of(context).viewInsets.bottom;
    return Scaffold(
      resizeToAvoidBottomInset: false,
      body: SizedBox(
        height: screenHeight - keyboardHeight,
        child: SingleChildScrollView(
          child: Column(
            children: [
              const SizedBox(height: 200),
              for (var i = 0; i < 10; i++) const TextField()
            ],
          ),
        ),
      ),
    );
    
    0 讨论(0)
  • 2020-12-02 06:45

    My approach is to use SingleChildScrollView with the ClampingScrollPhysics physics.

    SingleChildScrollView(
      physics: ClampingScrollPhysics(),
      child: Container(),
    )
    
    0 讨论(0)
  • 2020-12-02 06:51

    Most other answers suggest using resizeToAvoidBottomPadding=false. In my experience this allows the keyboard to cover up text fields if they are underneath where the keyboard would appear.

    My current solution is to force my column to be the same height as the screen, then place it in a SingleChildScrollView so that Flutter automatically scrolls my screen up just enough when the keyboard is used.

    Widget build(BuildContext context) {
      return Scaffold(
        body: SingleChildScrollView(
          physics: NeverScrollableScrollPhysics(),
          child: ConstrainedBox(
            constraints: BoxConstraints(
              minWidth: MediaQuery.of(context).size.width,
              minHeight: MediaQuery.of(context).size.height,
            ),
            child: IntrinsicHeight(
              child: Column(
                mainAxisSize: MainAxisSize.max,
                children: <Widget>[
                  // CONTENT HERE
                ],
              ),
            ),
          ),
        ),
      );
    }
    

    I use NeverScrollableScrollPhysics so that the user cannot scroll around themselves.

    0 讨论(0)
提交回复
热议问题