Skip to content

Instantly share code, notes, and snippets.

@iota9star
Created October 30, 2022 09:46
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iota9star/d997032670bb6076efb7152062b54951 to your computer and use it in GitHub Desktop.
Save iota9star/d997032670bb6076efb7152062b54951 to your computer and use it in GitHub Desktop.
ValueListenable notifiers.
import 'package:flutter/foundation.dart';
class NewValueNotifier<T> extends ChangeNotifier implements ValueListenable<T> {
NewValueNotifier(this._value);
@override
T get value => _value;
T _value;
set value(T value) {
newValue(value, false);
}
void newValue(T newValue, [bool forceUpdate = true]) {
if (_value == newValue && !forceUpdate) {
return;
}
_value = newValue;
notifyListeners();
}
@override
String toString() => '${describeIdentity(this)}($value)';
}
class CombineNotifier<T> extends ChangeNotifier
implements ValueListenable<List<T>> {
CombineNotifier(this.listenables) {
for (final value in listenables) {
value.addListener(notifyListeners);
}
}
@override
void dispose() {
super.dispose();
for (final value in listenables) {
value.removeListener(notifyListeners);
}
}
final Iterable<ValueListenable<T>> listenables;
@override
late final List<T> value =
listenables.map((e) => e.value).toList(growable: false);
@override
String toString() {
return listenables.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment