valueStream method

A BehaviorSubject that will update value when a new event is emitted, also emits a new event when value changes

Implementation

BehaviorSubject<TextEditingValue> valueStream() {
  final stream = BehaviorSubject<TextEditingValue>.seeded(this.value);
  bool isLock = false;

  // Set a new text to controller
  // when stream is emitted
  stream.listen((data) {
    if (isLock) return;
    isLock = true;
    this.value = data;
    isLock = false;
  });

  // Set value back to stream
  // when the text got changed in controller
  this.addListener(() async {
    if (isLock) return;
    isLock = true;
    stream.value = this.value;
    await Future.delayed(Duration(microseconds: 1));
    isLock = false;
  });
  return stream;
}