onSelectionChanged method

Disposable onSelectionChanged(
  1. void block(
    1. TextSelection selection
    )
)

Unlike addListener, this only calls block when the selection of text actually changes. This won't be invoked when the focus or text value of the text changes in TextField or TextFormField.

Returns Disposable function which can be called to dispose this listener.

Implementation

Disposable onSelectionChanged(void Function(TextSelection selection) block) {
  var previousValue = selection;
  void onSelectionChanged() {
    if (selection != previousValue) {
      block(selection);
    }
    previousValue = selection;
  }

  addListener(onSelectionChanged);
  return () => removeListener(onSelectionChanged);
}