【问题标题】:Textfield rises above keyboard文本字段高于键盘
【发布时间】:2021-09-02 23:24:38
【问题描述】:

我希望键盘向上推动文本字段,以便无需用户滚动即可看到完整的文本字段。尝试了 resizeToAvoidBottomInset: true 如Flutter TextFormField hidden by keyboard中所建议的@

但是,当文本字段处于焦点时,键盘仍会隐藏文本字段。

下面是我从 Scaffold 开始的代码:

Scaffold(
      resizeToAvoidBottomInset: true,
      floatingActionButton: Container(
        height: height * 0.054,
        width: width * 0.885,
        child: FloatingActionButton.extended(
          shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.all(Radius.circular(9.0))),
          isExtended: true,
          backgroundColor: Colos.blue,
          materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
          onPressed: () {
          },
          label: Text('SEND',
              style: TextStyle(
                  color: Colors.white
                  fontWeight: FontWeight.bold,
                  letterSpacing: 2.1,
                  fontSize: 13.5)),
        ),
      ),
      extendBody: true,
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      appBar: AppBar(
        backgroundColor: Colors.black,
        leading: IconButton(
          icon: Icon(Icons.chevron_left,
              size: 21, color: Colors.white
          onPressed: () {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => TestingHomePage()),
            );
          },
        ),
        centerTitle: true,
        title: Text('Textfield',
            style: TextStyle(
                color: Colors.white
                fontWeight: FontWeight.bold,
                fontSize: 18)),
      ),
      backgroundColor: Colors.black,
      body: SafeArea(
        child: SingleChildScrollView(
          child: Column(children: <Widget>[
            Padding(
              padding: EdgeInsets.only(top: height * 0.015),
              child: Container(
                margin: EdgeInsets.symmetric(
                  horizontal: width * 0.045,
                ),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.all(Radius.circular(12)),
                  color: Colors.red,
                ),
                child: Widget
                             
brackets

            Padding(
              padding: EdgeInsets.symmetric(
                  horizontal: width * 0.06, vertical: height * 0.02),
              child: TextField(
                style: TextStyle(color: Colors.white),
                maxLines: maxLines,
                autofocus: false,
                decoration: InputDecoration(
                  decoration...
                ),
                onChanged: (text) {
                  print("First text field: $text");

              brackets

可能是因为浮动按钮停靠了?

【问题讨论】:

  • 无论真假结果都是一样的。 ??????

标签: flutter textfield


【解决方案1】:

这里的问题是文本字段在滚动视图内。

遵循这个层次结构:

Scaffold(
  body: SafeArea(
    child: Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: [
        Expanded(
          child: SingleChildScrollView(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [ ... ] // Put your scroll view elements here.
            ),
          )
        ),
        // Put your text field outside the scroll view.
        Padding(
          padding: const EdgeInsets.all(8.0),
          child: TextField(),
        ),
      ],
    ),
  ),
);

这样,每当屏幕尺寸发生变化时,滚动视图会由于扩展的小部件而缩小,并且 TextField 保持固定在底部并随着键盘向上移动。


另外,请在进行更改后尝试设置 resizeToAvoidBottomInset: false/true 以更好地了解此属性的作用。

【讨论】:

  • 这项工作 Afridi,谢谢。但是有一个问题。文本字段上方的小部件实际上有一些关于如何填写答案的指南。因此,当上面的容器改变形状时,导轨就会被切断。有什么办法吗?
  • 更像是一种设计选择。如果我要实现类似的东西,只要文本字段获得焦点,我就会将重要的文本滚动到视图中。
猜你喜欢
  • 1970-01-01
  • 2018-06-15
  • 2019-03-13
  • 2016-12-28
  • 1970-01-01
  • 1970-01-01
  • 2014-03-18
  • 2011-01-19
  • 1970-01-01
相关资源
最近更新 更多