Flutter-问题:Wrap a column with an Expand in a SingleChildScrollView

首先来看下面例子,项目中需要实现如图功能,解析一下需求:

  1. 【确定】按钮需要放在页面底部
  2. 页面需要包裹在SingleChildScrollView中,当键盘出现时,系统才会滚动布局避免TextField被遮挡
  3. 登录按钮不会被顶到键盘上方(下面几种方案都没实现这需求,有知道解决方案的大大回复下?)
    在这里插入图片描述

想过的各种方案

方案1
return Container(
      child: Column(
        children: <Widget>[
          Expanded(
            child: Container(
              color: Colors.pink[50],
              child: SingleChildScrollView(
                  child: Column(
                children: <Widget>[
                  TextField(
                    decoration: InputDecoration(labelText: 'username', hintText: 'username'),
                  ),
                  TextField(
                    decoration: InputDecoration(labelText: 'password', hintText: 'password'),
                  ),
                ],
              )),
            ),
          ),
          RaisedButton(
            child: Text('login'),
            onPressed: () {},
          )
        ],
      ),
    );

将【登录】按钮单独拎出来,SingleChildScrollView只包裹上面部分内容。
这样有个问题,就是【登录】按钮在键盘弹起时会跑到键盘上面

方案2
@override
  Widget build(BuildContext context) {
    //屏幕高度-Appbar高度-标题栏高度
    var height = MediaQuery.of(context).size.height -
        kToolbarHeight -
        MediaQuery.of(context).padding.vertical;
    return SingleChildScrollView(
      child: Container(
        height: height,
        color: Colors.pink[50],
        child: Column(
          children: <Widget>[
            Container(
              height: 400,
            ),
            TextField(
              decoration: InputDecoration(labelText: 'username', hintText: 'username'),
            ),
            TextField(
              decoration: InputDecoration(labelText: 'password', hintText: 'password'),
            ),
            Spacer(),
            RaisedButton(
              child: Text('login'),
              onPressed: () {},
            )
          ],
        ),
      ),
    );
  }

SingleChildScrollView的child高度默认是wrap的,计算屏幕的高度,赋值给container,可以使按钮放置在底部
这样有个问题是如果屏幕内容比较多,会出现BOTTOM OVERFLOWED BY ** PIXELS的报错。该方案不可取

方案3

来自于flutter issues

@override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (context, constraints) {
        return SingleChildScrollView(
          child: ConstrainedBox(
            constraints:
                BoxConstraints(minWidth: constraints.maxWidth, minHeight: constraints.maxHeight),
            child: IntrinsicHeight(
              child: Column(
                children: <Widget>[
                  Container(
                    height: 400,
                  ),
                  TextField(
                    decoration: InputDecoration(labelText: 'username', hintText: 'username'),
                  ),
                  TextField(
                    decoration: InputDecoration(labelText: 'password', hintText: 'password'),
                  ),
                  Spacer(),
                  RaisedButton(
                    child: Text('login'),
                    onPressed: () {},
                  )
                ],
              ),
            ),
          ),
        );
      },
    );
  }
  • 使用LayoutBuilder,可以获取parent的布局约束,根据parent的最大宽高来设置ConstrainedBox的宽高
  • IntrinsicHeight可以将child的高度变为最大高度

但这样做还是会遇到【键盘弹出时按钮上移】的问题,暂时还没想到解决办法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值