首页 文章

在扩展 maxLines 属性时自动滚动多行 TextFormField

提问于
浏览
3

我正在实现一个 TextFormField,其 maxLines 属性设置为 3.一旦用户以第四行开头,我怎么能让 TextFormField 向下滚动?此时光标不再可见,直到用户手动向下滚动。有没有办法自动执行此操作?

实际上,此行为在“文本字段”示例中的 flutter_gallery 应用程序中有所体现。只需在“实时故事”输入中键入一个长文本,直到它到达第四行。
我的代码的重要部分实际上是这样的:

import 'package:flutter/material.dart';

class TextFormFieldDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Form(
        child: new TextFormField(
          maxLines: 3,
        ),
      ),
    );
  }
}

到目前为止,我没有找到解决此问题的方法。
此问题会影响 iOS 和 Android。

2 回答

  • 0

    这似乎是 Flutter Framework 中缺少的功能,我提交了一个错误来解决它:https://github.com/flutter/flutter/issues/9365

  • 4

    我们的团队通过嵌套一些现有的小部件来实现这一目标:

    // create the illusion of a beautifully scrolling text box
    return new Container(
        color: Colors.gray,
      padding: new EdgeInsets.all(7.0),
    
      child: new ConstrainedBox(
        constraints: new BoxConstraints(
          minWidth: _contextWidth(),
          maxWidth: _contextWidth(),
          minHeight: AppMeasurements.isLandscapePhone(context) ? 25.0 : 25.0,
          maxHeight: 55.0,
        ),
    
        child: new SingleChildScrollView(
          scrollDirection: Axis.vertical,
          reverse: true,
    
            // here's the actual text box
            child: new TextField(
              keyboardType: TextInputType.multiline,
              maxLines: null, //grow automatically
              focusNode: mrFocus,
              controller: _textController,
              onSubmitted: currentIsComposing ? _handleSubmitted : null,
              decoration: new InputDecoration.collapsed(
                hintText: ''Please enter a lot of text',
              ),
            ),
            // ends the actual text box
    
          ),
        ),
      );
    }
    

    我们从黑人获得帮助以获取小部件排序和正确的小部件以使其工作。

相关问题