Created
October 30, 2022 09:46
-
-
Save iota9star/d997032670bb6076efb7152062b54951 to your computer and use it in GitHub Desktop.
ValueListenable notifiers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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