value_notifier_ext.dart 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import 'package:flutter/material.dart';
  2. /// 万物皆可Notifier
  3. extension ObjectNotifierExt on Object? {
  4. ValueNotifier<T> notifier<T>() => ValueNotifier(this as T);
  5. }
  6. /// 拓展ValueNotifier
  7. /// 快捷调用builder来编写组件
  8. extension NotifierObjectExt<T> on ValueNotifier<T> {
  9. /// 创建Builder
  10. ValueListenableBuilder<T> builder(Widget Function(T v) build) {
  11. return ValueListenableBuilder<T>(
  12. valueListenable: this,
  13. builder: (context, v, child) => build.call(v),
  14. );
  15. }
  16. /// ==
  17. bool equals(T other) => value == other;
  18. /// 刷新数据
  19. update(T other) => value = other;
  20. }
  21. /// num相关拓展
  22. extension NotifierNumExt on ValueNotifier<num> {
  23. ValueListenableBuilder<num> builder(Widget Function(num v) build) {
  24. return ValueListenableBuilder(
  25. valueListenable: this,
  26. builder: (context, v, child) => build.call(v),
  27. );
  28. }
  29. num operator +(num other) => value += other;
  30. num operator -(num other) => value -= other;
  31. num operator *(num other) => value *= other;
  32. double operator /(num other) => value = value / other;
  33. int operator ~/(num other) => value = value ~/ other;
  34. num operator -() => -value;
  35. /// 取模
  36. num operator %(num other) => value % other;
  37. /// 取余
  38. num remainder(num other) => value.remainder(other);
  39. /// 是否相等
  40. bool equals(num other) => value == other;
  41. bool operator <(num other) => value < other;
  42. bool operator <=(num other) => value <= other;
  43. bool operator >(num other) => value > other;
  44. bool operator >=(num other) => value >= other;
  45. /// 赋值 通知更新
  46. update(num other) => value = other;
  47. }