Bläddra i källkod

1.添加从父页面向子级组件刷新辅助类

tags/0.0.2+11^0
杨华轩 4 år sedan
förälder
incheckning
e79bc9910f
2 ändrade filer med 84 tillägg och 0 borttagningar
  1. +83
    -0
      lib/models/base/base_change_notifier.dart
  2. +1
    -0
      lib/zhiying_comm.dart

+ 83
- 0
lib/models/base/base_change_notifier.dart Visa fil

@@ -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<BaseChangeNotifier>(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<BaseChangeNotifier>(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();
}
}
}

+ 1
- 0
lib/zhiying_comm.dart Visa fil

@@ -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';


Laddar…
Avbryt
Spara