如何在文本字段中flutter的值的末尾设置光标位置?

时间:2019-07-02 11:44:05

标签: flutter

问题是我想在文本字段的值末尾添加“,00”,在iOS设备中效果很好,但是在android中,光标移动到文本字段中值的起点。因此,我无法添加“,00”。

13 个答案:

答案 0 :(得分:4)

这对我有用:

_inputCtrl.value = TextEditingValue(
  text: suggestion,
  selection: TextSelection.collapsed(offset: suggestion.length),
);

https://github.com/flutter/flutter/issues/11416#issuecomment-507040665

答案 1 :(得分:4)

这对我来说就像一个魅力。

myController.text = newText;
myController.selection = TextSelection(
    baseOffset: newText.length,
    extentOffset: newText.length)

答案 2 :(得分:3)

检查了setter的{​​{1}}的{​​{1}}的注释,它说[text]和[selection]不应同时更改,但是对于电话输入时,我们希望对输入应用某种格式(例如'131 1111 1111'),同时将光标始终保持在末尾。

所以我只使用自定义的TextEditingController,就可以了!

text

答案 3 :(得分:1)

我通过使用此问题解决了

这会将光标置于文本字段的末尾。

您可以随时随地调用代码,它会在调用时设置光标位置。

或简单地将此代码放在textfield的onChanged()方法中

final val = TextSelection.collapsed(offset: _textTEC.text.length); 
 _textTEC.selection = val;

答案 4 :(得分:1)

以下代码对我来说很完美。

_controller.value = _controller.value.copyWith(
  text: newText,
  selection: TextSelection.fromPosition(
    TextPosition(offset: newText.length),
  ),
);

答案 5 :(得分:0)

您需要一个FocusNode并设置TextSelection来放置光标。

此处的代码可能是一个开始:Position cursor at end of TextField when focused?

答案 6 :(得分:0)

似乎可行的解决方案如下:

  1. 为您的文本字段的构造函数提供TextEditingController参数:

    var myTextEditingController = TextEditingController();
    var myTextField = TextField(..., controller: myTextEditingController);
    
  2. 在文本字段中设置新文本时,使用TextEditingController就像这样:

    myTextEditingController.value.copyWith(
      text: newText,
      selection: TextSelection(
        baseOffset: newText.length,
        extentOffset: newText.length
      )
    )
    

这似乎很复杂,但它似乎是解决Flutter文本字段中未更新光标问题的唯一解决方案。

答案 7 :(得分:0)

我在设置TextEditingController时遇到了同样的问题,这对我有用。

controller.text = someString;
controller.selection = TextSelection.fromPosition(TextPosition(offset: controller.text.length));

TextSelection.fromPosition()执行以下操作(来自文档):

  

在给定的文本位置创建一个折叠的选择。崩溃了   选择以相同的偏移量开始和结束,这意味着它包含   零个字符,但用作文本中的插入点。

答案 8 :(得分:0)

如果您想在文本中间添加特殊字符(在您的情况下为_textController.text.length,请使用00作为光标的基本偏移量)。在这种情况下,以下解决方案应该起作用:

String specialChars = '00';
int length = specialChars.length;
// current cursor position, where you want to add your special characters
int cursorPos = _textController.selection.base.offset;
// the text before the point you want to add the special characters
String prefixText = _textController.text.substring(0, cursorPos);
// the text after the point ...
String suffixText = _textController.text.substring(cursorPos);

_textController.text = prefixText + specialChars + suffixText;
// set the cursor position right after the special characters
_textController.selection = TextSelection(baseOffset: cursorPos + length, extentOffset: cursorPos + length);

或者您可以这样做,这是同一回事:

int cursorPos = _textController.selection.base.offset;
_textController.value = _textController.value.copyWith(
  text: _textController.text.replaceRange(cursorPos, cursorPos, '00'),
  selection: TextSelection.fromPosition(TextPosition(offset: cursorPos + 2))
);

答案 9 :(得分:0)

创建一个新的Dart类,扩展TextEditingController,然后可以使用行为如下的自定义TextController:

class TextController extends TextEditingController {

  TextController({String text}) {
    this.text = text;
  }

  set text(String newText) {
    value = value.copyWith(
      text: newText,
      selection: TextSelection.collapsed(offset: newText.length),
      composing: TextRange.empty
    );
  }
}

答案 10 :(得分:0)

根据 documentation 将文本和位置设置在一起的可能方法是使用 value... 像这样:

_controller.value = _controller.value.copyWith(text: newText, selection: newSelection)

答案 11 :(得分:0)

有很多解决方案,但在下面的代码中,我合并了 Github 和 SO 的最佳答案。

以下对 Dart >= 2.12 具有 null-safety 并使用最新的 @client.command() async def playyt(ctx, url): song_there = os.path.isfile("song.mp3") try: if song_there: os.remove("song.mp3") except PermissionError: em8 = discord.Embed(title = "Music Is Currently Playing", description = 'Please wait for the current playing music to end or use %leave <:_Paimon6:827074349450133524>.\nMusic provided by {ctx.author.mention} <:_Paimon6:827074349450133524>',color = ctx.author.color) await ctx.send(embed = em8) return voiceChannel = discord.utils.get(ctx.guild.voice_channels) await voiceChannel.connect() voice = discord.utils.get(client.voice_clients, guild=ctx.guild) em6 = discord.Embed(title = "Downloading Youtube Music", description = f'{url}\n\nPlease wait for paimon to setup the music you provide.\nMusic provided by {ctx.author.mention} <:_Paimon6:827074349450133524>',color = ctx.author.color) await ctx.send(embed = em6, delete_after = 2) await ctx.message.delete() ydl_opts = { 'format': 'bestaudio/best', 'postprocessors': [{ 'key': 'FFmpegExtractAudio', 'preferredcodec': 'mp3', 'preferredquality': '196', }], } with youtube_dl.YoutubeDL(ydl_opts) as ydl: ydl.download([url]) for file in os.listdir("./"): if file.endswith(".mp3"): os.rename(file, "song.mp3") voice.play(discord.FFmpegPCMAudio("song.mp3")) em1 = discord.Embed(title = "Now Listening Youtube Music", description = f'{url}\n\nPlease use %leave first to change music.\nMusic provided by {ctx.author.mention} <:_Paimon6:827074349450133524>',color = ctx.author.color) videoID = url.split("watch?v=")[1].split("&")[0] em1.set_thumbnail(url = f'https://img.youtube.com/vi/{videoID}/default.jpg'.format(videoID = videoID)) await ctx.send(embed = em1) API

TextSelection

答案 12 :(得分:0)

您可以使用..功能如果您在控制器中设置值如下:

final _controller = TextEditingController()..text = txtValue
      ..selection = TextSelection.collapsed(offset: txtValue.length);

)

我认为在设置值运行时非常有用。

相关问题