【问题标题】:How to make the TextField expands in flutterwhen the user types in it?当用户输入时,如何使 TextField 扩展?
【发布时间】:2019-10-20 00:37:48
【问题描述】:

我正在设置一个文本字段,当在键盘上按下 Enter 时该文本字段将展开 当我尝试在最大行中设置 null 时,它不显示文本字段内的文本 我什至设置了扩展=真 我在代码中 size.height 的某个地方使用了媒体查询大小 我将文本字段包装在灵活的小部件中 并添加一些填充,然后将整个灵活的小部件包装在 Row 中 最后一行被包裹在一个容器中

所以这段代码不起作用帮助我解决这个问题

容器(

          height: size.height * .059,
          width: double.infinity,
          child: new Row(
            children: <Widget>[
              new Flexible(
                child: Padding(
                  padding: EdgeInsets.only(right: size.width * 0.007),
                  child: new TextField(
                    controller: _textFieldController,
                    textInputAction: TextInputAction.newline,
                    keyboardType: TextInputType.multiline,
                    maxLines: null,
                    expands: true,
                    style: TextStyle(color: Colors.black),
                    cursorColor: Colors.blue,
                    decoration: InputDecoration(
                      focusedBorder: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(30.0),
                          borderSide: BorderSide(color: Color(0xff8e9291))),
                      enabledBorder: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(30.0),
                          borderSide: BorderSide(color: Color(0xff8e9291))),
                      prefixIcon: Icon(
                        Icons.message,
                        color: Color(0xff8e9291),
                      ),
                      hintText: "Type a message",
                      hintStyle: TextStyle(
                        color: Color(0xff8e9291),
                      ),
                    ),
                  ),
                ),
              ),
              FloatingActionButton(
                backgroundColor: Colors.blue[400],
                onPressed: () {
                },
                tooltip: 'Send',
                child: RotationTransition(
                    turns: AlwaysStoppedAnimation(-35 / 360),
                    child: Icon(
                      Icons.send,
                      size: 20,
                    )),
              ),
            ],
          ),
        ),

这是我正在使用的文本字段..

【问题讨论】:

    标签: flutter


    【解决方案1】:

    您可以删除Container 的高度,因为这将形成一个固定的高度,并删除TextFieldexpands,因为这是为了使其填充其父高度。

    【讨论】:

    • 非常感谢它运行良好,但我还需要使用滚动条在文本字段中滚动文本,我该如何实现?是否有可能在文本文件中设置 listview 或 Singlechildscrollview ???
    • TextField 已经这样做了。当 TextField 有足够的文本可以滚动时,它将能够滚动。
    • 如果您希望 TextField 在有 3 行时开始滚动,您可以设置属性 minLines: 1 和 maxLines: 3
    【解决方案2】:

    您可以将 MaxLine 属性设置为 null 'maxLines: null` 这将使 textField 垂直扩展

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-01
      • 2021-11-28
      • 2017-01-08
      • 1970-01-01
      • 2016-09-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多