在Flutter中的多行文本输入中为每行添加前缀? [英] Add a prefix to every line in a multiline Text Input in Flutter?

查看:58
本文介绍了在Flutter中的多行文本输入中为每行添加前缀?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以在Flutter的多行文本输入中为每行添加前缀(如-").

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

例如:

你好

世界!

将成为:

-你好

-世界!

这是我的代码:

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

推荐答案

每次创建新行时,U都可以添加-.

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

将此添加到您的initState()中,

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');

这篇关于在Flutter中的多行文本输入中为每行添加前缀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆