Add a prefix to every line in a multiline Text Input in Flutter?

北城余情 提交于 2021-01-28 18:54:53

问题


I wanted to know if there was a way of adding a prefix (like "- ") to every line in a multiline Text Input in Flutter.

For example:

Hello

World!

Would become:

-Hello

-World!

This is my code:

TextField(
  maxLines: null,
  controller: _elementsController,
  textCapitalization: TextCapitalization.sentences,
  style: TextStyle(
    fontSize: 18.0,
  ),
  decoration: InputDecoration(
    contentPadding: EdgeInsets.all(0.0),
    labelText: 'Elements',
  ),
),

回答1:


U can add a - everytime a new line is created.

Add this in your initState(),

final prefix = '-';
_elementsContoller.addListener(() {
  if(_elementsController.text.endsWith('\n')) {
    // Add the prefix everytime a new line is created
    _elementsController.text +=  prefix;
  }
}

If these changes should be made after the input,

text.replaceAll('\n', '\n$prefix');


来源:https://stackoverflow.com/questions/63211954/add-a-prefix-to-every-line-in-a-multiline-text-input-in-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!