Flutter :- How to scroll the sceen without shrink the screen on the Stack view?(Scroll the the whole screen when the keyboard appears )

你。 提交于 2021-01-28 19:17:07

问题


Description
I am creating the login screen in which I have used the Stack widget, Currently, everything works fine but only one issue of the shrinking the view. When I use the resizeToAvoidBottomPadding: false inside the Scaffold then screen shrinking disappear but another problem arise that whole screen scroll not working, please check below lines of code

 class _LoginScreen extends State<LoginScreen> {
      @override
      Widget build(BuildContext context) {
        // TODO: implement build

        return Scaffold(
            resizeToAvoidBottomPadding: false,
            body:  Stack(
              children: <Widget>[
                Container(
                  height: double.infinity,
                  width: double.infinity,
                  child: Column(
                    children: <Widget>[
                      Expanded(
                        flex: 4,
                        child: Column(
                          children: <Widget>[
                            Expanded(
                              flex: 9,
                              child: Container(
                                color: Colors.blue,
                                child: Align(
                                  alignment: Alignment.centerLeft,
                                  child: RotatedBox(
                                    quarterTurns: 3,
                                    child: Container(
                                      child: Padding(
                                        padding: EdgeInsets.all(5),
                                        child: Text(
                                          "Login !!",
                                          style: TextStyle(
                                            fontSize: 30.0,
                                            color: Colors.white),
                                        ),
                                      ),
                                    ),
                                  ),
                                )),
                            ),
                            Expanded(
                              flex: 1,
                              child: Container(
                                color: Colors.white,
                              ),
                            )
                          ],
                        )),
                      Expanded(
                        flex: 6,
                        child: Container(
                          color: Colors.white,
                        ),
                      )
                    ],
                  ),
                ),
                Padding(
                  padding: EdgeInsets.only(left: 20.0, right: 20.0),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      new Image(
                        image: new AssetImage("images/logo.png"),
                        color: null,
                        height: 100.0,
                        width: 100.0,
                        fit: BoxFit.scaleDown,
                        alignment: Alignment.center,
                      ),
                      SizedBox(
                        height: 20.0,
                      ),
                      TextField(
                        keyboardType: TextInputType.number,
                        inputFormatters: [
                          LengthLimitingTextInputFormatter(10),
                        ],
                        decoration: new InputDecoration(
                          focusedBorder: OutlineInputBorder(
                            borderSide:
                            BorderSide(color: Colors.blue, width: 2.0),
                          ),
                          enabledBorder: OutlineInputBorder(
                            borderSide:
                            BorderSide(color: Colors.grey, width: 2.0),
                          ),
                          hintText: "Please enter mobile number")),
                      SizedBox(
                        height: 10.0,
                      ),
                      TextField(
                        obscureText: true,
                        inputFormatters: [
                          LengthLimitingTextInputFormatter(16),
                        ],
                        keyboardType: TextInputType.visiblePassword,
                        decoration: new InputDecoration(
                          focusedBorder: OutlineInputBorder(
                            borderSide:
                            BorderSide(color: Colors.blue, width: 2.0),
                          ),
                          enabledBorder: OutlineInputBorder(
                            borderSide:
                            BorderSide(color: Colors.grey, width: 2.0),
                          ),
                          hintText: "Password")),
                      SizedBox(
                        height: 3.0,
                      ),
                      Align(
                        alignment: Alignment.topRight,
                        child: Text(
                          "Forgot Password?",
                          style: TextStyle(fontSize: 12.0),
                        )),
                      SizedBox(
                        height: 3.0,
                      ),
                      SizedBox(
                        height: 10.0,
                      ),
                      RaisedButton(
                        onPressed: () {},
                        color: Colors.blue,
                        child: const Text(
                          'Login',
                          style: TextStyle(fontSize: 15.0, color: Colors.black45),
                        ),
                      )
                    ],
                  ),
                ),
              ],
            ));
      }
    }

From Above code, I am getting the following screen



I have used the ListView and SingleChildScrollView but it not working properly, please check my code with SingleChildScrollView, which i have tried

class _LoginScreen extends State<LoginScreen> {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build

    return Scaffold(
        resizeToAvoidBottomPadding: false,
        body: SingleChildScrollView(
          child: IntrinsicHeight(
              child: Stack(
            children: <Widget>[
              Container(
                height: double.infinity,
                width: double.infinity,
                child: Column(
                  children: <Widget>[
                    Expanded(
                        flex: 4,
                        child: Column(
                          children: <Widget>[
                            Expanded(
                              flex: 9,
                              child: Container(
                                  color: Colors.blue,
                                  child: Align(
                                    alignment: Alignment.centerLeft,
                                    child: RotatedBox(
                                      quarterTurns: 3,
                                      child: Container(
                                        child: Padding(
                                          padding: EdgeInsets.all(5),
                                          child: Text(
                                            "Login !!",
                                            style: TextStyle(
                                                fontSize: 30.0,
                                                color: Colors.white),
                                          ),
                                        ),
                                      ),
                                    ),
                                  )),
                            ),
                            Expanded(
                              flex: 1,
                              child: Container(
                                color: Colors.white,
                              ),
                            )
                          ],
                        )),
                    Expanded(
                      flex: 6,
                      child: Container(
                        color: Colors.white,
                      ),
                    )
                  ],
                ),
              ),
              Padding(
                padding: EdgeInsets.only(left: 20.0, right: 20.0),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    new Image(
                      image: new AssetImage("images/logo.png"),
                      color: null,
                      height: 100.0,
                      width: 100.0,
                      fit: BoxFit.scaleDown,
                      alignment: Alignment.center,
                    ),
                    SizedBox(
                      height: 20.0,
                    ),
                    TextField(
                        keyboardType: TextInputType.number,
                        inputFormatters: [
                          LengthLimitingTextInputFormatter(10),
                        ],
                        decoration: new InputDecoration(
                            focusedBorder: OutlineInputBorder(
                              borderSide:
                                  BorderSide(color: Colors.blue, width: 2.0),
                            ),
                            enabledBorder: OutlineInputBorder(
                              borderSide:
                                  BorderSide(color: Colors.grey, width: 2.0),
                            ),
                            hintText: "Please enter mobile number")),
                    SizedBox(
                      height: 10.0,
                    ),
                    TextField(
                        obscureText: true,
                        inputFormatters: [
                          LengthLimitingTextInputFormatter(16),
                        ],
                        keyboardType: TextInputType.visiblePassword,
                        decoration: new InputDecoration(
                            focusedBorder: OutlineInputBorder(
                              borderSide:
                                  BorderSide(color: Colors.blue, width: 2.0),
                            ),
                            enabledBorder: OutlineInputBorder(
                              borderSide:
                                  BorderSide(color: Colors.grey, width: 2.0),
                            ),
                            hintText: "Password")),
                    SizedBox(
                      height: 3.0,
                    ),
                    Align(
                        alignment: Alignment.topRight,
                        child: Text(
                          "Forgot Password?",
                          style: TextStyle(fontSize: 12.0),
                        )),
                    SizedBox(
                      height: 3.0,
                    ),
                    SizedBox(
                      height: 10.0,
                    ),
                    RaisedButton(
                      onPressed: () {},
                      color: Colors.blue,
                      child: const Text(
                        'Login',
                        style: TextStyle(fontSize: 15.0, color: Colors.black45),
                      ),
                    )
                  ],
                ),
              ),
            ],
          )),
        ));
  }
}

And From the above code getting this result by using the SingleChildScrollView

Problem:- I want to scroll the whole screen when the keyboard appears, I have used all the Listview and SingleChildScrollView but not getting the solution, please help me on it. Thanks


回答1:


The problem is you're using Expanded widgets, you see expanded widgets are flexible in nature they will consume and shrink according to the available space. If you don't want that you need to specify a height.

https://i.imgur.com/wVgAUlN.mp4

class StacScroll extends StatefulWidget {
  StacScroll({Key key}) : super(key: key);

  @override
  _StacScrollState createState() => _StacScrollState();
}

class _StacScrollState extends State<StacScroll> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        resizeToAvoidBottomInset: true,
        body: Container(
          height: double.infinity,
          width: double.infinity,
          // margin:
          //     EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
          child: SingleChildScrollView(
            child: Stack(
              children: <Widget>[
                Container(
                  color: Colors.blue,
                  height: MediaQuery.of(context).size.height * 0.3,
                  width: MediaQuery.of(context).size.width,
                  child: RotatedBox(
                      quarterTurns: 3,
                      child: Container(
                        child: Padding(
                          padding: EdgeInsets.all(5),
                          child: Text(
                            "Login !!",
                            style:
                                TextStyle(fontSize: 30.0, color: Colors.white),
                          ),
                        ),
                      )),
                ),
                Container(
                  margin: EdgeInsets.only(
                      top: MediaQuery.of(context).size.height * 0.3),
                  child: Padding(
                    padding: EdgeInsets.only(left: 20.0, right: 20.0),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        new Image(
                          image: new AssetImage("images/logo.png"),
                          color: null,
                          height: 100.0,
                          width: 100.0,
                          fit: BoxFit.scaleDown,
                          alignment: Alignment.center,
                        ),
                        SizedBox(
                          height: 20.0,
                        ),
                        TextField(
                            keyboardType: TextInputType.number,
                            decoration: new InputDecoration(
                                focusedBorder: OutlineInputBorder(
                                  borderSide: BorderSide(
                                      color: Colors.blue, width: 2.0),
                                ),
                                enabledBorder: OutlineInputBorder(
                                  borderSide: BorderSide(
                                      color: Colors.grey, width: 2.0),
                                ),
                                hintText: "Please enter mobile number")),
                        SizedBox(
                          height: 10.0,
                        ),
                        TextField(
                            obscureText: true,
                            keyboardType: TextInputType.visiblePassword,
                            decoration: new InputDecoration(
                                focusedBorder: OutlineInputBorder(
                                  borderSide: BorderSide(
                                      color: Colors.blue, width: 2.0),
                                ),
                                enabledBorder: OutlineInputBorder(
                                  borderSide: BorderSide(
                                      color: Colors.grey, width: 2.0),
                                ),
                                hintText: "Password")),
                        SizedBox(
                          height: 3.0,
                        ),
                        Align(
                            alignment: Alignment.topRight,
                            child: Text(
                              "Forgot Password?",
                              style: TextStyle(fontSize: 12.0),
                            )),
                        SizedBox(
                          height: 3.0,
                        ),
                        SizedBox(
                          height: 10.0,
                        ),
                        RaisedButton(
                          onPressed: () {},
                          color: Colors.blue,
                          child: const Text(
                            'Login',
                            style: TextStyle(
                                fontSize: 15.0, color: Colors.black45),
                          ),
                        )
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
        ));
  }
}



回答2:


Thanks @Nadeem by which problem has resolved Whenever we want to scroll the full screen when keyboard appear then we should not use the Expand widget for it.I was also doing the same mistake, for the other user i have modified the code for full scroll when keyboard appear, I have used the MediaQuery for it,Please check my below code of it.

import 'package:flutter/material.dart';

class LoginScreen extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _LoginScreen();
  }
}

class _LoginScreen extends State<LoginScreen> {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text("Login"),
      ),
      body: Container(
        height: double.infinity,
        width: double.infinity,
        child: SingleChildScrollView(
          child: Stack(
            children: <Widget>[
              Container(
                color: Colors.blue,
                height: MediaQuery.of(context).size.height * 0.3,
              ),
              Container(
                  color: Colors.white,
                  height: MediaQuery.of(context).size.height * 0.59,
                  padding: EdgeInsets.all(10.0),
                  margin: EdgeInsets.only(
                      top: MediaQuery.of(context).size.height * 0.3),
                  child: Container(
                    margin: EdgeInsets.only(top: 70.0),
                    child: Column(
                      children: <Widget>[
                        TextFormField(
                            decoration: InputDecoration(
                                labelText: 'Enter your username')),
                        TextFormField(
                            decoration: InputDecoration(labelText: 'Password')),
                        SizedBox(
                          height: 20.0,
                        ),
                        RaisedButton(
                          color: Colors.yellow,
                          child: Text("Submit",
                              style: TextStyle(color: Colors.blue)),
                          onPressed: () {},
                        )
                      ],
                    ),
                  )),
              Center(
                child: Card(
                  color: Colors.yellow,
                  elevation: 8,
                  margin: EdgeInsets.only(
                      top: MediaQuery.of(context).size.height * .25),
                  child: Container(
                    child: Center(
                        child: Text(
                      "Radhe",
                      style: TextStyle(color: Colors.blue, fontSize: 20.0),
                    )),
                    height: MediaQuery.of(context).size.height * .1,
                    width: MediaQuery.of(context).size.width * .3,
                  ),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}

Please check the output of it.



来源:https://stackoverflow.com/questions/60630481/flutter-how-to-scroll-the-sceen-without-shrink-the-screen-on-the-stack-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!