minLines and maxLines must be null when expands is true in flutter

91 Views Asked by At

in this textfiled i use expands and maxline both but show error how to set maxline 1. this textfiled i used in resizable container when the container size go small the text font go to new line but i have set text in same line it is adjust using when the size of container decrease the font adjust using of the font size decrease.

child: TextField(
                textAlign: TextAlign.center,
                textAlignVertical: TextAlignVertical.center,
                expands: true,
                maxLines: null,  //here i use 1 but error...
                key: textFieldKey,
                controller: textController,
                cursorColor: Colors.black,
                style: TextStyle(color: Colors.black, fontSize: fontSize),
                decoration: InputDecoration(
                  filled: true,
                  fillColor: Colors.yellow,
                  hintText: "Enter Your Text",
                  helperStyle: TextStyle(color: Colors.black),
                  border: InputBorder.none,
                  contentPadding: EdgeInsets.symmetric(
                      vertical: height / 20), // Adjust the vertical padding
                ),
                onChanged: (text) {
                  updateDimensions();
                },
                onSubmitted: (text) {
                  setState(() {
                    _isSelected = false;
                  });
                },
              ),
1

There are 1 best solutions below

0
Ivo On

I think what you fail to realize is that maxLines is not meant for limiting how many lines of text you allow. It's maybe a bit of deceiving name. As per documentation about maxLines:

/// This affects the height of the field itself and does not limit the number

/// of lines that can be entered into the field.

For that you can for example use an inputFormatter like

inputFormatters: [
  FilteringTextInputFormatter.deny(RegExp(r"\n"))
],

EDIT:

I looked further into it and it seems that putting

keyboardType : TextInputType.text,

might be sufficient. There's actually code inside TextField that does

keyboardType = keyboardType ?? (maxLines == 1 ? TextInputType.text : TextInputType.multiline),

Meaning that if you don't specify a keyboardType it makes it a multline one when maxLines isn't 1