From e79bc9910f256aba40b42b46c461dd5955521f91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=8D=8E=E8=BD=A9?= <646903573@qq.com> Date: Mon, 30 Nov 2020 16:53:32 +0800 Subject: [PATCH] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0=E4=BB=8E=E7=88=B6=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=E5=90=91=E5=AD=90=E7=BA=A7=E7=BB=84=E4=BB=B6=E5=88=B7?= =?UTF-8?q?=E6=96=B0=E8=BE=85=E5=8A=A9=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/models/base/base_change_notifier.dart | 83 +++++++++++++++++++++++ lib/zhiying_comm.dart | 1 + 2 files changed, 84 insertions(+) create mode 100644 lib/models/base/base_change_notifier.dart diff --git a/lib/models/base/base_change_notifier.dart b/lib/models/base/base_change_notifier.dart new file mode 100644 index 0000000..05cbf5c --- /dev/null +++ b/lib/models/base/base_change_notifier.dart @@ -0,0 +1,83 @@ +import 'package:flutter/material.dart'; +import 'dart:async'; +import 'package:provider/provider.dart'; + +///页面刷新通知 +class BaseChangeNotifier extends ChangeNotifier { + StreamController pageController = StreamController.broadcast(); + + ///刷新通知方法 + void refresh() { + pageController.add("refresh"); + notifyListeners(); + } + + void close() { + pageController.close(); + } + + Stream get stream => pageController.stream; + +} + +enum EventType { + refresh, + + ///刷新事件 +} + +///页面刷新辅助类 +class RefreshListener { + bool isListen = false; + StreamSubscription streamSubscription; + + ///监听方法 + ///已废弃 + void _listen(BuildContext context, Function(String) refresh) { + BaseChangeNotifier baseChangeNotifier; + try { + baseChangeNotifier = Provider.of(context); + } catch (e, s) { + print(e); + print(s); + throw Exception( + "父页面或父控件必须添加 BaseChangeNotifier 的 Provide值才能使用RefreshListener对象"); + } + if (!isListen && baseChangeNotifier != null) { + isListen = true; + streamSubscription = baseChangeNotifier.stream.listen((event) { + if (refresh != null) { + refresh(event); + } + }); + } + } + + ///监听构造 + RefreshListener.listen(BuildContext context, Function(String) refresh) { + BaseChangeNotifier baseChangeNotifier; + try { + baseChangeNotifier = Provider.of(context); + } catch (e, s) { + print(e); + print(s); + throw Exception( + "父页面或父控件必须添加 BaseChangeNotifier 的 Provide值才能使用RefreshListener对象"); + } + if (!isListen && baseChangeNotifier != null) { + isListen = true; + baseChangeNotifier.stream.listen((event) { + if (refresh != null) { + refresh(event); + } + }); + } + } + + ///关闭监听 + void close() { + if (streamSubscription != null) { + streamSubscription.cancel(); + } + } +} \ No newline at end of file diff --git a/lib/zhiying_comm.dart b/lib/zhiying_comm.dart index fae63c1..ce28b05 100644 --- a/lib/zhiying_comm.dart +++ b/lib/zhiying_comm.dart @@ -34,4 +34,5 @@ export 'util/widget_creater.dart'; export 'util/widget_factory.dart'; export 'package:cached_network_image/cached_network_image.dart'; export 'util/photo_preview/photo_preview.dart'; +export 'models/base/base_change_notifier.dart';