提交 142949be authored 作者: 张国庆's avatar 张国庆

修改通依赖注入初始化控制器

上级 946b4ca0
import 'package:clx_flutter_message/util/string_util.dart'; import 'package:clx_flutter_message/util/string_util.dart';
import 'package:clx_flutter_message/util/toast_util.dart'; import 'package:clx_flutter_message/util/toast_util.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'clx_flutter_message_platform_interface.dart'; import 'clx_flutter_message_platform_interface.dart';
import 'common/constant.dart'; import 'common/constant.dart';
import 'core/model/message_config.dart'; import 'core/model/message_config.dart';
import 'core/model/message_data.dart'; import 'core/model/message_data.dart';
import 'core/notice/notice_dialog_widget.dart';
import 'core/notice/notice_manager.dart'; import 'core/notice/notice_manager.dart';
import 'core/notification/notification_layout/notification_layout_widget.dart';
import 'core/notification/notification_manager.dart'; import 'core/notification/notification_manager.dart';
import 'core/socket/socket_io.dart'; import 'core/socket/socket_io.dart';
...@@ -123,6 +126,14 @@ abstract class BaseMessageConfig with NotificationManager, NoticeManager { ...@@ -123,6 +126,14 @@ abstract class BaseMessageConfig with NotificationManager, NoticeManager {
onJumpToMessagePage(page, arguments); onJumpToMessagePage(page, arguments);
} }
// 构造方法
BaseMessageConfig() {
//通过getx 注入 NotificationLayoutController
Get.put(NotificationLayoutController());
//通过getx 注入 NoticeDialogWidgetController
Get.put(NoticeDialogWidgetController());
}
//刷新消息、获取未处理消息,重新连接websocket //刷新消息、获取未处理消息,重新连接websocket
Future<void> refreshMessage(BuildContext context) async { Future<void> refreshMessage(BuildContext context) async {
// 校验消息相关配置字段 // 校验消息相关配置字段
...@@ -160,6 +171,5 @@ abstract class BaseMessageConfig with NotificationManager, NoticeManager { ...@@ -160,6 +171,5 @@ abstract class BaseMessageConfig with NotificationManager, NoticeManager {
setNotification(nList); setNotification(nList);
// 处理公告 // 处理公告
setNotice(aList); setNotice(aList);
} }
} }
...@@ -14,7 +14,5 @@ class MessageConfig { ...@@ -14,7 +14,5 @@ class MessageConfig {
String accessToken = ""; String accessToken = "";
BaseMessageConfig? baseMessageConfig; BaseMessageConfig? messageManagement;
} }
...@@ -176,7 +176,6 @@ class NoticeDialogWidget extends GetView<NoticeDialogWidgetController> { ...@@ -176,7 +176,6 @@ class NoticeDialogWidget extends GetView<NoticeDialogWidgetController> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return GetBuilder<NoticeDialogWidgetController>( return GetBuilder<NoticeDialogWidgetController>(
init: NoticeDialogWidgetController(),
id: "NoticeDialogWidget", id: "NoticeDialogWidget",
builder: (_) { builder: (_) {
return _buildView(); return _buildView();
...@@ -187,7 +186,7 @@ class NoticeDialogWidget extends GetView<NoticeDialogWidgetController> { ...@@ -187,7 +186,7 @@ class NoticeDialogWidget extends GetView<NoticeDialogWidgetController> {
class NoticeDialogWidgetController extends GetxController { class NoticeDialogWidgetController extends GetxController {
NoticeDialogWidgetController(); NoticeDialogWidgetController();
BaseMessageConfig? messageDeal = messageConfig.baseMessageConfig; BaseMessageConfig? messageDeal = messageConfig.messageManagement;
_initData() { _initData() {
update(["NoticeDialogWidget"]); update(["NoticeDialogWidget"]);
} }
......
...@@ -14,7 +14,6 @@ class NotificationLayoutWidget extends GetView<NotificationLayoutController> { ...@@ -14,7 +14,6 @@ class NotificationLayoutWidget extends GetView<NotificationLayoutController> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return GetBuilder<NotificationLayoutController>( return GetBuilder<NotificationLayoutController>(
init: NotificationLayoutController(),
id: "NotificationLayoutWidget", id: "NotificationLayoutWidget",
builder: (_) { builder: (_) {
if (controller.headNode == null) { if (controller.headNode == null) {
...@@ -104,7 +103,7 @@ class NotificationLayoutController extends GetxController { ...@@ -104,7 +103,7 @@ class NotificationLayoutController extends GetxController {
MessageNode? headNode; MessageNode? headNode;
BaseMessageConfig? messageDeal=messageConfig.baseMessageConfig; BaseMessageConfig? messageDeal = messageConfig.messageManagement;
_initData() { _initData() {
update(["NotificationLayoutWidget"]); update(["NotificationLayoutWidget"]);
...@@ -174,11 +173,9 @@ class NotificationLayoutController extends GetxController { ...@@ -174,11 +173,9 @@ class NotificationLayoutController extends GetxController {
update(); update();
} }
@override @override
void onReady() { void onReady() {
super.onReady(); super.onReady();
_initData(); _initData();
} }
} }
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import '../../util/extension.dart';
import '../model/message_data.dart'; import '../model/message_data.dart';
import 'notification_layout/notification_layout_widget.dart'; import 'notification_layout/notification_layout_widget.dart';
...@@ -7,22 +8,22 @@ import 'notification_layout/notification_layout_widget.dart'; ...@@ -7,22 +8,22 @@ import 'notification_layout/notification_layout_widget.dart';
mixin NotificationManager { mixin NotificationManager {
OverlayEntry? _overlayEntry; OverlayEntry? _overlayEntry;
final NotificationLayoutWidget _notificationLayoutWidget = final NotificationLayoutController? _controller =
const NotificationLayoutWidget(); findOtherLogic<NotificationLayoutController>();
/// 显示悬浮布局 /// 显示悬浮布局
void showNotification(BuildContext? context) { void showNotification(BuildContext? context) {
if (context == null) return; if (context == null) return;
if (_overlayEntry != null) return; // 防止重复显示 if (_overlayEntry != null) return; // 防止重复显示
if (_notificationLayoutWidget.controller.headNode == null) { if (_controller?.headNode == null) {
return; // 防止没有消息时显示 return; // 防止没有消息时显示
} }
_overlayEntry = OverlayEntry( _overlayEntry = OverlayEntry(
builder: (context) => Positioned( builder: (context) => const Positioned(
top: 0, top: 0,
left: 0, left: 0,
right: 0, right: 0,
child: _notificationLayoutWidget, child: NotificationLayoutWidget(),
), ),
); );
...@@ -37,16 +38,16 @@ mixin NotificationManager { ...@@ -37,16 +38,16 @@ mixin NotificationManager {
/// 缓存之前的消息列表 /// 缓存之前的消息列表
void setNotification(List<dynamic> data) { void setNotification(List<dynamic> data) {
_notificationLayoutWidget.controller.setCashMessage( _controller?.setCashMessage(
data.map<MessageData>((e) => MessageData.fromJson(e)).toList(), data.map<MessageData>((e) => MessageData.fromJson(e)).toList(),
); );
} }
void insertNotification(MessageData data) { void insertNotification(MessageData data) {
_notificationLayoutWidget.controller.insertNotification(data); _controller?.insertNotification(data);
} }
void clear() { void clear() {
_notificationLayoutWidget.controller.clear(); _controller?.clear();
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论