156 lines
4.8 KiB
Dart
156 lines
4.8 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:fluro/fluro.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:yumi/modules/chat/chat_route.dart';
|
|
import 'package:yumi/modules/auth/login_route.dart';
|
|
import 'package:yumi/app/routes/sc_lk_application.dart';
|
|
|
|
/// 安卓页面切换时长
|
|
const Duration kAndroidTransitionDuration = Duration(milliseconds: 375);
|
|
|
|
/// fluro的路由跳转工具类
|
|
class SCNavigatorUtils {
|
|
static bool inChatPage = false;
|
|
static bool inLoginPage = false;
|
|
|
|
/// 登录态失效时,多个入口可能同时尝试跳登录页。
|
|
/// 统一在这里做一次幂等保护,避免堆叠多个登录页。
|
|
static Future<void> pushLoginIfNeeded(
|
|
BuildContext context, {
|
|
bool replace = false,
|
|
bool clearStack = false,
|
|
TransitionType? transition,
|
|
Duration? transitionDuration,
|
|
RouteTransitionsBuilder? transitionBuilder,
|
|
}) async {
|
|
if (inLoginPage) {
|
|
return;
|
|
}
|
|
await push(
|
|
context,
|
|
LoginRouter.login,
|
|
replace: replace,
|
|
clearStack: clearStack,
|
|
transition: transition,
|
|
transitionDuration: transitionDuration,
|
|
transitionBuilder: transitionBuilder,
|
|
);
|
|
}
|
|
|
|
//不需要页面返回值的跳转
|
|
static Future push(
|
|
BuildContext context,
|
|
String path, {
|
|
bool replace = false,
|
|
bool clearStack = false,
|
|
TransitionType? transition,
|
|
Duration? transitionDuration,
|
|
RouteTransitionsBuilder? transitionBuilder,
|
|
}) async {
|
|
FocusScope.of(context).unfocus();
|
|
inLoginPage = path.startsWith(LoginRouter.login);
|
|
inChatPage = path.startsWith(SCChatRouter.chat);
|
|
final result = await SCLkApplication.router.navigateTo(
|
|
context,
|
|
path,
|
|
replace: replace,
|
|
clearStack: clearStack,
|
|
|
|
///在系统语言为ar语会有问题
|
|
transition: (Platform.isAndroid ? null : TransitionType.cupertino),
|
|
// transitionDuration: transitionDuration ??
|
|
// (Platform.isAndroid ? kAndroidTransitionDuration : null),
|
|
// transitionBuilder: transitionBuilder ??
|
|
// (Platform.isAndroid ? (context, animation, secondaryAnimation, child) {
|
|
// return ScaleTransition(
|
|
// scale: Tween(begin: 1.0,end: 0.9).animate(CurvedAnimation(parent: secondaryAnimation, curve: Curves.ease)),
|
|
// child: SlideTransition(
|
|
// position: Tween<Offset>(
|
|
// begin: const Offset(1.0, 0.0),
|
|
// end: const Offset(0.0, 0.0),
|
|
// ).animate(CurvedAnimation(parent: animation, curve: Curves.ease)),
|
|
// child: child,
|
|
// ),
|
|
// );
|
|
// } : null)
|
|
);
|
|
|
|
return result;
|
|
}
|
|
|
|
//需要页面返回值的跳转
|
|
static pushResult(
|
|
BuildContext context,
|
|
String path,
|
|
Function(Object) function, {
|
|
bool replace = false,
|
|
bool clearStack = false,
|
|
TransitionType? transition,
|
|
}) {
|
|
FocusScope.of(context).unfocus();
|
|
inLoginPage = path == LoginRouter.login;
|
|
inChatPage = path.startsWith(SCChatRouter.chat);
|
|
SCLkApplication.router
|
|
.navigateTo(
|
|
context,
|
|
path,
|
|
replace: replace,
|
|
clearStack: clearStack,
|
|
// transition: transition ??
|
|
// (Platform.isAndroid
|
|
// ? TransitionType.custom
|
|
// : TransitionType.cupertino),
|
|
// transitionDuration:
|
|
// Platform.isAndroid ? kAndroidTransitionDuration : null,
|
|
// transitionBuilder:
|
|
// Platform.isAndroid ? (context, animation, secondaryAnimation, child) {
|
|
// return ScaleTransition(
|
|
// scale: Tween(begin: 1.0,end: 0.9).animate(CurvedAnimation(parent: secondaryAnimation, curve: Curves.ease)),
|
|
// child: SlideTransition(
|
|
// position: Tween<Offset>(
|
|
// begin: const Offset(1.0, 0.0),
|
|
// end: const Offset(0.0, 0.0),
|
|
// ).animate(CurvedAnimation(parent: animation, curve: Curves.ease)),
|
|
// child: child,
|
|
// ),
|
|
// );
|
|
// } : null
|
|
)
|
|
.then((result) {
|
|
// 页面返回result为null
|
|
if (result == null) {
|
|
return;
|
|
}
|
|
function(result);
|
|
})
|
|
.catchError((error) {});
|
|
}
|
|
|
|
/// 直接返回
|
|
static void goBack(BuildContext context) {
|
|
if (inChatPage) {
|
|
inChatPage = false;
|
|
}
|
|
FocusScope.of(context).unfocus();
|
|
Navigator.pop(context);
|
|
}
|
|
|
|
/// 带参数返回
|
|
static void goBackWithParams(BuildContext context, result) {
|
|
if (inChatPage) {
|
|
inChatPage = false;
|
|
}
|
|
FocusScope.of(context).unfocus();
|
|
Navigator.pop(context, result);
|
|
}
|
|
|
|
static void popUntil(BuildContext context, RoutePredicate predicate) {
|
|
if (inChatPage) {
|
|
inChatPage = false;
|
|
}
|
|
FocusScope.of(context).unfocus();
|
|
Navigator.popUntil(context, predicate);
|
|
}
|
|
}
|