|
@@ -6,12 +6,15 @@ import 'package:provider/provider.dart'; |
|
|
class BaseChangeNotifier extends ChangeNotifier { |
|
|
class BaseChangeNotifier extends ChangeNotifier { |
|
|
StreamController pageController = StreamController.broadcast(); |
|
|
StreamController pageController = StreamController.broadcast(); |
|
|
|
|
|
|
|
|
|
|
|
static Map<String, String> hasKeys = Map(); |
|
|
|
|
|
|
|
|
///刷新通知方法 |
|
|
///刷新通知方法 |
|
|
void refresh() { |
|
|
void refresh() { |
|
|
pageController.add("refresh"); |
|
|
pageController.add("refresh"); |
|
|
notifyListeners(); |
|
|
notifyListeners(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void close() { |
|
|
void close() { |
|
|
pageController.close(); |
|
|
pageController.close(); |
|
|
} |
|
|
} |
|
@@ -29,8 +32,8 @@ enum EventType { |
|
|
///页面刷新辅助类 |
|
|
///页面刷新辅助类 |
|
|
class RefreshListener { |
|
|
class RefreshListener { |
|
|
bool isListen = false; |
|
|
bool isListen = false; |
|
|
StreamSubscription streamSubscription; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Map<String, String> hasKeys = Map(); |
|
|
|
|
|
StreamSubscription streamSubscription; |
|
|
///监听方法 |
|
|
///监听方法 |
|
|
///已废弃 |
|
|
///已废弃 |
|
|
void _listen(BuildContext context, Function(String) refresh) { |
|
|
void _listen(BuildContext context, Function(String) refresh) { |
|
@@ -54,19 +57,18 @@ class RefreshListener { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
///监听构造 |
|
|
///监听构造 |
|
|
RefreshListener.listen(BuildContext context, Function(String) refresh) { |
|
|
|
|
|
|
|
|
void listen(BuildContext context, Function(String) refresh) { |
|
|
|
|
|
|
|
|
BaseChangeNotifier baseChangeNotifier; |
|
|
BaseChangeNotifier baseChangeNotifier; |
|
|
try { |
|
|
try { |
|
|
baseChangeNotifier = Provider.of<BaseChangeNotifier>(context); |
|
|
baseChangeNotifier = Provider.of<BaseChangeNotifier>(context); |
|
|
} catch (e, s) { |
|
|
} catch (e, s) { |
|
|
print(e); |
|
|
print(e); |
|
|
print(s); |
|
|
print(s); |
|
|
throw Exception( |
|
|
|
|
|
"父页面或父控件必须添加 BaseChangeNotifier 的 Provide值才能使用RefreshListener对象"); |
|
|
|
|
|
} |
|
|
} |
|
|
if (!isListen && baseChangeNotifier != null) { |
|
|
if (!isListen && baseChangeNotifier != null) { |
|
|
isListen = true; |
|
|
isListen = true; |
|
|
baseChangeNotifier.stream.listen((event) { |
|
|
|
|
|
|
|
|
streamSubscription= baseChangeNotifier.stream.listen((event) { |
|
|
if (refresh != null) { |
|
|
if (refresh != null) { |
|
|
refresh(event); |
|
|
refresh(event); |
|
|
} |
|
|
} |
|
@@ -77,7 +79,7 @@ class RefreshListener { |
|
|
///关闭监听 |
|
|
///关闭监听 |
|
|
void close() { |
|
|
void close() { |
|
|
if (streamSubscription != null) { |
|
|
if (streamSubscription != null) { |
|
|
streamSubscription.cancel(); |
|
|
|
|
|
|
|
|
streamSubscription?.cancel(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |