Files
chat-flutter/lib/tools/tools_route.dart
2025-09-12 20:35:12 +08:00

60 lines
1.7 KiB
Dart
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:demo/views/msg/msg_chat_controller.dart';
import 'package:demo/views/msg/msg_chat_view.dart';
import 'package:demo/tools/tools_enum.dart';
import 'package:demo/tools/tools_storage.dart';
import 'package:navigation_history_observer/navigation_history_observer.dart';
// 路由跳转控制器
// 当存在历史页面,直接跳回历史页面,如:
// A=>B=>C=>D=>C,当又进入C页面为了不无限套娃直接跳回到B页面
// A=>B=>C
class ToolsRoute {
// 跳转到聊天页面
void chatPage({
required String chatId,
required String nickname,
String remark = '',
required String portrait,
required ChatTalk chatTalk,
}) {
// 路由页面
String routeName = MsgChatView.routeName;
// 构建对象
LocalChat localChat = LocalChat(
chatId: chatId,
title: remark.isNotEmpty ? remark : nickname,
nickname: nickname,
portrait: portrait,
chatTalk: chatTalk,
//
);
// 保存对象
ToolsStorage().chat(value: localChat);
// 是否存在当前路由
if (existRoute(routeName)) {
// 刷新
if (Get.isRegistered<MsgChatController>()) {
MsgChatController controller = Get.find<MsgChatController>();
controller.onInit();
}
// 跳转
Get.until((route) => route.settings.name == routeName);
return;
}
// 执行方法
Get.toNamed(routeName);
}
// 是否存在当前路由
bool existRoute(String routeName) {
for (Route route in NavigationHistoryObserver().history) {
if (routeName == route.settings.name) {
return true;
}
}
return false;
}
}