首页 > 解决方案 > 可水平滚动的 Multine `TextFormField`

问题描述

以下将多行TextFormField换行长行。

TextFormField(
  keyboardType: TextInputType.multiline,
  maxLines: null,
)

但是,我试图避免换行,而是让它水平滚动。所以,我在SingleChildScrollView周围添加了一个TextFormField

SingleChildScrollView(
  scrollDirection: Axis.horizontal,
  child: TextFormField(
    keyboardType: TextInputType.multiline,
    maxLines: null,
  ),
)

这产生

══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
The following assertion was thrown during performLayout():
Assertion failed:
file:///..../flutter/lib/src/material/input_decorator.dart:948:7
layoutConstraints.maxWidth < double.infinity
"An InputDecorator, which is typically created by a TextField, cannot have an unbounded width.\nThis
happens when the parent widget does not provide a finite width constraint. For example, if the
InputDecorator is contained by a Row, then its width must be constrained. An Expanded widget or a
SizedBox can be used to constrain the width of the InputDecorator or the TextField that contains
it."

标签: flutter

解决方案


// Use ConstrainedBox as a Child of SingleChildScrollView with width and height constraint so you TextFormFeild can become horizontally scrollable . 

 SingleChildScrollView(
          scrollDirection: Axis.horizontal,
          child: ConstrainedBox(
            constraints: BoxConstraints.expand(width: 2000,height: 200),
            child: TextFormField(
              maxLines: null,
             
            ),
          ),
        )

推荐阅读