【问题标题】:How to use column inside single child scroll view flutter如何在单个子滚动视图颤动中使用列
【发布时间】:2020-06-13 06:18:29
【问题描述】:

我在单个子滚动视图中有一个列。当我使用键盘输入时,我需要让我的登录屏幕滚动,因为键盘隐藏了密码文本字段。

class UserRegistration extends StatefulWidget {

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

class _UserRegistrationState extends State<UserRegistration> {
  @override
  Widget build(BuildContext context) {

    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
      statusBarColor: Color(0xFF6C62FF)
    ));

    return SafeArea(
      child: GestureDetector(
        onTap: () => FocusScope.of(context).requestFocus(FocusNode()),
        child: Scaffold(
          resizeToAvoidBottomInset: false,
          resizeToAvoidBottomPadding: false,
          backgroundColor: Color(0xFF6C62FF),
          body:   SingleChildScrollView(
            padding: EdgeInsets.symmetric(horizontal: 30),
            child: Column([![enter image description here][1]][1]
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Center(child: FittedBox(child: Text('Welcome', style: TextStyle(fontFamily: 'SourceSans', fontSize: 50, fontWeight: FontWeight.w600, color: Color(0xFFFFD700)),))),
                Container(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text('Register', style: TextStyle(fontFamily: 'SourceSans', fontSize: 40, fontWeight: FontWeight.w600, color: Colors.white)),
                      SizedBox(height: 5,),
                      Text('Start from today', style: TextStyle(fontFamily: 'SourceSans', fontSize: 25, letterSpacing: 1.5,fontWeight: FontWeight.w600, color: Colors.white), overflow: TextOverflow.ellipsis,),
                    ],
                  ),
                ),
                Form(
                  child: Column(
                    children: [
                      EditTextNormal(hintText: 'Email', iconData: Icons.email, textInputType: TextInputType.emailAddress, validate: false, errorText: 'Enter your email',),
                      SizedBox(height: 20,),
                      EditTextObscure(hintText: 'Password', iconData: Icons.lock, textInputType: TextInputType.text, validate: false, errorText: 'Enter your password',),
                      SizedBox(height: 50,),
                      Container(
                        height: 50,
                        width: 180,
                        child: FlatButton(
                          splashColor: Colors.transparent,
                          highlightColor: Colors.transparent,
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(8)
                          ),
                          onPressed: () {},
                          color: Colors.white,
                          child: Text('Register', style: TextStyle(color: Color(0xFF6C62FF), fontSize: 20), overflow: TextOverflow.ellipsis,),
                        ),
                      )
                    ],
                  ),
                ),
                Center(
                  child: RichText(
                    text: TextSpan(
                      children: <TextSpan>[
                        TextSpan(text: 'Login', style: TextStyle(color: Color(0xFFFFD700), letterSpacing: 1, wordSpacing: 1.5))
                      ],
                      text: 'Have an account? ',
                      style: TextStyle(fontSize: 18, fontFamily: 'SourceSans', fontWeight: FontWeight.bold, letterSpacing: 1, wordSpacing: 1.5)
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

这是我的代码,但是当我在单个子滚动视图空间内使用一列时,这里均匀地不起作用。请给我一个解决方案。

我的输出:

预期输出:

【问题讨论】:

    标签: android ios google-chrome flutter


    【解决方案1】:

    试试这个

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

    【讨论】:

    • 请逐行解释代码的作用
    【解决方案2】:

    SingleChildScrollView 中添加容器并根据您的键盘打开与否为其分配高度。

    添加依赖:

    dependencies:
      keyboard_visibility: ^0.5.6
    

    initState() 中初始化键盘侦听器以进行回调

    bool _isKeyBoardShown = false;
    
      @protected
      void initState() {
        super.initState();
        KeyboardVisibilityNotification().addNewListener(
          onChange: (bool visible) {
            setState(() {
              _isKeyBoardShown = visible;
            });
          },
        );
      }
    

    根据_isKeyBoardShown 的值决定是否在屏幕上增加额外的高度。

    Container(
              width: MediaQuery.of(context).size.width,
              height: _isKeyBoardShown
                  ? MediaQuery.of(context).size.height +
                      MediaQuery.of(context).size.height / 2
                  : MediaQuery.of(context).size.height,
              child: Column(....)
               )
    

    注意:使用MediaQuery 来决定额外的高度不要使用硬编码值 这里我用MediaQuery.of(context).size.height / 2

    【讨论】:

    • 我会试试你的新解决方案。
    • 即使键盘不可见页面正在滚动,这也不起作用。
    • @PythonHub:你在 initState() 中正确使用 setState(),添加 print 语句并检查
    • @PythonHub:你能给我看看代码吗?添加您的问题,以便我可以调查
    猜你喜欢
    • 2019-03-20
    • 1970-01-01
    • 2020-12-23
    • 1970-01-01
    • 2021-12-21
    • 2019-08-25
    • 2020-08-09
    • 2022-08-15
    • 2019-04-08
    相关资源
    最近更新 更多