基础库
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.
 
 
 
 
 

83 rivejä
2.0 KiB

  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. ///刷新通知方法
  8. void refresh() {
  9. pageController.add("refresh");
  10. notifyListeners();
  11. }
  12. void close() {
  13. pageController.close();
  14. }
  15. Stream get stream => pageController.stream;
  16. }
  17. enum EventType {
  18. refresh,
  19. ///刷新事件
  20. }
  21. ///页面刷新辅助类
  22. class RefreshListener {
  23. bool isListen = false;
  24. StreamSubscription streamSubscription;
  25. ///监听方法
  26. ///已废弃
  27. void _listen(BuildContext context, Function(String) refresh) {
  28. BaseChangeNotifier baseChangeNotifier;
  29. try {
  30. baseChangeNotifier = Provider.of<BaseChangeNotifier>(context);
  31. } catch (e, s) {
  32. print(e);
  33. print(s);
  34. throw Exception(
  35. "父页面或父控件必须添加 BaseChangeNotifier 的 Provide值才能使用RefreshListener对象");
  36. }
  37. if (!isListen && baseChangeNotifier != null) {
  38. isListen = true;
  39. streamSubscription = baseChangeNotifier.stream.listen((event) {
  40. if (refresh != null) {
  41. refresh(event);
  42. }
  43. });
  44. }
  45. }
  46. ///监听构造
  47. RefreshListener.listen(BuildContext context, Function(String) refresh) {
  48. BaseChangeNotifier baseChangeNotifier;
  49. try {
  50. baseChangeNotifier = Provider.of<BaseChangeNotifier>(context);
  51. } catch (e, s) {
  52. print(e);
  53. print(s);
  54. throw Exception(
  55. "父页面或父控件必须添加 BaseChangeNotifier 的 Provide值才能使用RefreshListener对象");
  56. }
  57. if (!isListen && baseChangeNotifier != null) {
  58. isListen = true;
  59. 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. }