TextInputRenderViewModel constructor

TextInputRenderViewModel(
  1. int id,
  2. int instanceId,
  3. String className,
  4. RenderContext<LoadInstanceContext> context,
)

Implementation

TextInputRenderViewModel(
  int id,
  int instanceId,
  String className,
  RenderContext context,
) : super(id, instanceId, className, context) {
  dispatcher = TextInputDispatcher(context, rootId, id, controller);

  controller.addListener(() {
    var curSelStart = controller.selection.start;
    var curSelEnd = controller.selection.end;

    // 处理光标变化
    if (_selStart != curSelStart || _selEnd != curSelEnd) {
      _selStart = curSelStart;
      _selEnd = curSelEnd;
      dispatcher.onSelectionChanged(_selStart, _selEnd);
    }

    // 处理input内容变化
    var curText = controller.text;
    var v = controller.controller?.value;
    // 这里要考虑iOS原生中文输入法中存在候选词的情况,候选词变更不触发前端change事件
    if (v != null && !v.isComposingRangeValid) {
      if (_text != curText) {
        _text = curText;
        dispatcher.onChangeText(_text);
      }
    }
  });
}