【问题标题】:Move textfield up when keyboard appears in Flutter当键盘出现在 Flutter 中时向上移动文本字段
【发布时间】:2020-05-21 10:11:39
【问题描述】:

我想让我的文本字段在键盘出现时上升。键盘在文本字段前面,所以我看不到我写的内容,我没有找到很多解决问题的方法,或者不是很干净。

Widget build(BuildContext context) {
return Scaffold(
  backgroundColor: Theme.of(context).backgroundColor,
  body: Padding(
    padding: const EdgeInsets.all(8.0),
    child: Column(
      children: <Widget>[
        conseil(text),
        Spacer(),
        InkWell(
          onTap: () => [
            pic.getImage().then((a) {
              setState(() {
                myimg = myimage;
              });
            })
          ],
          child: Container(
            decoration: BoxDecoration(
                border: Border.all(width: 2.0, color: mygreen),
                boxShadow: <BoxShadow>[
                  BoxShadow(
                      color: mygreen, blurRadius: 0, offset: Offset(7, 3))
                ],
                shape: BoxShape.circle),
            child: ClipOval(
              child: SizedBox(
                width: 140,
                height: 140,
                child: (myimg != null)
                    ? Image.file(myimg, fit: BoxFit.fill)
                    : Image(
                        image: AssetImage('assets/images/others/add.png'),
                        fit: BoxFit.fill,
                      ),
              ),
            ),
          ),
        ),
        (myimage == null)?
        Text("Choisir une photo"): SizedBox(height: 1),
        Spacer(),
        SizedBox(
          width: 250,
          child: TextField(
            textAlign: TextAlign.center,
            style: TextStyle(color: Colors.white),
            decoration: InputDecoration(
                enabledBorder: OutlineInputBorder(
                    borderSide:
                        BorderSide(color: Color(0xFF37cd41), width: 2)),
                hintText: 'TON PRENOM',
                hintStyle: TextStyle(color: Colors.white)),
            controller: name,
          ),
        ),
        Spacer(),
        button(mypink, 'CONTINUER', widget.admin, context, name),
        Spacer(),
      ],
    ),
  ),
);
}
}

【问题讨论】:

    标签: user-interface flutter


    【解决方案1】:

    尝试使用SingleChildScrollView 和ConstrainedBox。

    Scaffold(
      body: SingleChildScrollView(
        child: ConstrainedBox(
          constraints: BoxConstraints(maxHeight: MediaQuery.of(context).size.height),
          child: yourWidget()
    

    查看以下答案:https://stackoverflow.com/a/59783374/12709039

    【讨论】:

    • 我这样做了,但是当键盘启动时我无法滚动:/
    • 键盘打开时不会自动滚动,手动向上滚动页面
    • 当我增加 maxheight 时,我可以滚动,但不仅仅是在键盘出现时
    【解决方案2】:

    试试这个

      Column(
          children: <Widget>[
            Expanded(
              child: SingleChildScrollView(
                child: Column(
                         (Your other Widgets)
    

    【讨论】:

    • 所有小部件消失:/
    • @LucaMathevet 答案已更新,您是否执行过热重启?
    • 更新了我的答案尝试一下,因为我使用的是相同的并且工作正常
    • 我收到错误:ParentDataElement: is not true
    • 还是黑屏
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-30
    • 2020-12-11
    • 2014-12-26
    • 1970-01-01
    • 1970-01-01
    • 2017-01-29
    • 2019-10-22
    相关资源
    最近更新 更多