Flutter:-如何在不缩小Stack视图的屏幕的情况下滚动屏幕?(出现键盘时滚动整个屏幕) [英] Flutter :- How to scroll the sceen without shrink the screen on the Stack view?(Scroll the the whole screen when the keyboard appears )

查看:103
本文介绍了Flutter:-如何在不缩小Stack视图的屏幕的情况下滚动屏幕?(出现键盘时滚动整个屏幕)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说明
我正在创建登录屏幕,在其中使用了 Stack 小部件,目前,一切正常,但只有一个缩小视图的问题.当我在 Scaffold 内使用 resizeToAvoidBottomPadding:false 时,屏幕收缩消失了,但另一个问题是整个屏幕滚动无法正常工作,请检查以下几行代码

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



我已经使用了 ListView SingleChildScrollView ,但是它无法正常工作,请使用我尝试过的 SingleChildScrollView 检查我的代码



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),
                      ),
                    )
                  ],
                ),
              ),
            ],
          )),
        ));
  }
}

然后从上面的代码中通过使用 SingleChildScrollView

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

问题:-我想在键盘出现时滚动整个屏幕,我已经使用了所有的 Listview 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

推荐答案

问题是您正在使用扩展窗口小部件,您会看到扩展窗口小部件本质上是灵活的,它们会根据可用空间消耗和缩小.如果不希望,则需要指定高度.

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),
                          ),
                        )
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
        ));
  }
}

这篇关于Flutter:-如何在不缩小Stack视图的屏幕的情况下滚动屏幕?(出现键盘时滚动整个屏幕)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆