57 lines
1.1 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
2022-03-20 17:17:06 +08:00
abstract class Comparable<T> {
bool compare(T? previous, T? current);
}
class ObjectComparable<T> extends Comparable<T> {
@override
bool compare(T? previous, T? current) {
return previous == current;
}
}
class PublishNotifier<T> extends ChangeNotifier {
T? _value;
2022-03-20 17:17:06 +08:00
Comparable<T>? comparable = ObjectComparable();
PublishNotifier({this.comparable});
set value(T newValue) {
2022-03-20 17:17:06 +08:00
if (comparable != null) {
if (comparable!.compare(_value, newValue)) {
_value = newValue;
notifyListeners();
}
} else {
2021-11-11 19:56:30 +08:00
_value = newValue;
notifyListeners();
}
}
T? get currentValue => _value;
2022-08-30 20:54:11 +08:00
void addPublishListener(void Function(T) callback,
{bool Function()? listenWhen}) {
2021-11-11 19:56:30 +08:00
super.addListener(
() {
2022-04-16 20:20:00 +08:00
if (_value == null) {
return;
} else {}
2022-04-16 20:20:00 +08:00
if (listenWhen != null && listenWhen() == false) {
return;
}
callback(_value as T);
2021-11-11 19:56:30 +08:00
},
);
}
}
2022-08-30 20:54:11 +08:00
class Notifier extends ChangeNotifier {
void notify() {
notifyListeners();
}
}