概述

#

一个新方法 void showAutocorrectionPromptRect(int start, int end) 已添加到 TextInputClient 接口。

背景

#

为了显示 iOS 自动更正高亮,iOS 文本输入插件需要一种方式来告知 Flutter 框架高亮的起始和结束位置。

变更说明

#

一个新方法 void showAutocorrectionPromptRect(int start, int end) 已添加到 TextInputClient 接口。当 iOS 在当前用户输入中找到新的潜在自动更正候选词,或先前高亮的候选词范围发生变化时,会调用此方法。

迁移指南

#

如果您的应用程序未实现或继承 TextInputClient,则无需迁移。如果您的应用程序不面向 iOS,或者实现 textInputClient 接口的类不支持自动更正,则只需为新方法添加一个空实现。

dart
class CustomTextInputClient implements TextInputClient {
  void showAutocorrectionPromptRect(int start, int end) {}
}

否则,如果您的应用面向 iOS 并支持 iOS 上的自动更正,我们建议您为 TextInputClient 子类添加 void showAutocorrectionPromptRect(int start, int end) 的合理实现。

迁移后的代码

dart
// Assume your `TextInputClient` is a `State` subclass, and it has a variable 
// `_currentPromptRectRange` that controls the autocorrection highlight.
class CustomTextInputClient extends State<...> implements TextInputClient {
  @override
  void updateEditingValue(TextEditingValue value) {
    // When the text changes, the highlight needs to be dismissed.
    if (value.text != _value.text) {
      setState(() {
        _currentPromptRectRange = null;
      });
    }
  }

  void _handleFocusChanged() {
    // When this text input loses focus, the autocorrection highlight needs
    // to be dismissed.
    if (!_hasFocus) {
      setState(() {
        _currentPromptRectRange = null;
      });
    }
  }

  @override
  void showAutocorrectionPromptRect(int start, int end) {
    // Updates the range of the highlight, as iOS requested.
    // This method isn't called when iOS decides to
    // dismiss the highlight.
    setState(() {
      _currentPromptRectRange = TextRange(start: start, end: end);
    });
  }
}

时间线

#

稳定版本中:1.20

参考资料

#

API 文档

相关议题

相关 PR