基础库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

base_change_notifier.dart 1.9 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import 'package:flutter/material.dart';
  2. import 'dart:async';
  3. import 'package:provider/provider.dart';
  4. ///页面刷新通知
  5. class BaseChangeNotifier extends ChangeNotifier {
  6. StreamController pageController = StreamController.broadcast();
  7. static Map<String, String> hasKeys = Map();
  8. ///刷新通知方法
  9. void refresh() {
  10. pageController.add("refresh");
  11. notifyListeners();
  12. }
  13. void close() {
  14. pageController.close();
  15. }
  16. Stream get stream => pageController.stream;
  17. }
  18. enum EventType {
  19. refresh,
  20. ///刷新事件
  21. }
  22. ///页面刷新辅助类
  23. class RefreshListener {
  24. bool isListen = false;
  25. Map<String, String> hasKeys = Map();
  26. StreamSubscription streamSubscription;
  27. ///监听方法
  28. ///已废弃
  29. void _listen(BuildContext context, Function(String) refresh) {
  30. BaseChangeNotifier baseChangeNotifier;
  31. try {
  32. baseChangeNotifier = Provider.of<BaseChangeNotifier>(context);
  33. } catch (e, s) {
  34. print(e);
  35. print(s);
  36. throw Exception(
  37. "父页面或父控件必须添加 BaseChangeNotifier 的 Provide值才能使用RefreshListener对象");
  38. }
  39. if (!isListen && baseChangeNotifier != null) {
  40. isListen = true;
  41. streamSubscription = baseChangeNotifier.stream.listen((event) {
  42. if (refresh != null) {
  43. refresh(event);
  44. }
  45. });
  46. }
  47. }
  48. ///监听构造
  49. void listen(BuildContext context, Function(String) refresh) {
  50. BaseChangeNotifier baseChangeNotifier;
  51. try {
  52. baseChangeNotifier = Provider.of<BaseChangeNotifier>(context);
  53. } catch (e, s) {
  54. print(e);
  55. print(s);
  56. }
  57. if (!isListen && baseChangeNotifier != null) {
  58. isListen = true;
  59. streamSubscription= baseChangeNotifier.stream.listen((event) {
  60. if (refresh != null) {
  61. refresh(event);
  62. }
  63. });
  64. }
  65. }
  66. ///关闭监听
  67. void close() {
  68. if (streamSubscription != null) {
  69. streamSubscription?.cancel();
  70. }
  71. }
  72. }