import 'dart:convert';
import 'package:ecoyia_zgs/business/college/college_page.dart';
import 'package:ecoyia_zgs/business/column/big_brand/big_brand_page.dart';
import 'package:ecoyia_zgs/business/column/high_commission/high_commission_page.dart';
import 'package:ecoyia_zgs/business/column/rank/rank_page.dart';
import 'package:ecoyia_zgs/business/column/tens_billions/tens_billions_page.dart';
import 'package:ecoyia_zgs/business/favorite/favorite_list_page.dart';
import 'package:ecoyia_zgs/business/goods_detail/goods_detail_page.dart';
import 'package:ecoyia_zgs/business/income/withdraw/withdraw_record_page.dart';
import 'package:ecoyia_zgs/business/review/address_manager_page.dart';
import 'package:ecoyia_zgs/business/search/search_page.dart';
import 'package:ecoyia_zgs/business/settings/bind_alipay/bind_alipay_page.dart';
import 'package:ecoyia_zgs/business/settings/share_app/share_app_page.dart';
import 'package:ecoyia_zgs/business/splash/welcome_page.dart';
import 'package:ecoyia_zgs/widgets/webview/webview_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:ecoyia_zgs/helpers/router/ui_pages.dart';
import 'package:ecoyia_zgs/app/app_state.dart';
import 'package:ecoyia_zgs/business/login/login_page.dart';
import 'package:ecoyia_zgs/business/main/main_page.dart';
import 'package:ecoyia_zgs/business/settings/settings_page.dart';
import 'package:ecoyia_zgs/business/splash/splash_page.dart';
import 'package:ecoyia_zgs/business/home/line_news/line_news_page.dart';
import 'package:ecoyia_zgs/helpers/router/back_dispatcher.dart';
class ESRouterDelegate extends RouterDelegate
with ChangeNotifier, PopNavigatorRouterDelegateMixin
final List
ESBackButtonDispatcher? backButtonDispatcher;
@override
final GlobalKey
final AppState appState;
ESRouterDelegate(this.appState) : navigatorKey = GlobalKey() {
appState.addListener(() {
notifyListeners();
});
}
List
int numPages() => _pages.length;
@override
PageConfiguration get currentConfiguration =>
_pages.last.arguments as PageConfiguration;
@override
Widget build(BuildContext context) {
return MediaQuery(
//设置文字大小不随系统设置改变
data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
child: Navigator(
key: navigatorKey,
onPopPage: _onPopPage,
pages: buildPages(),
),
);
}
bool _onPopPage(Route
final didPop = route.didPop(result);
if (!didPop) {
return false;
}
if (canPop()) {
pop();
return true;
} else {
return false;
}
}
void _removePage(Page page) {
_pages.remove(page);
}
void pop({Page? page}) {
if (canPop()) {
if (page != null) {
_removePage(page);
} else {
_removePage(_pages.last);
}
}
}
bool canPop() {
return _pages.length > 1;
}
@override
Future
// 安卓硬件返回时,如果有弹窗则不执行任何操作
if (appState.isShowDialog) {
appState.dismissDialog();
return Future.value(true);
}
if (canPop()) {
pop();
notifyListeners();
return Future.value(true);
}
return Future.value(false);
}
MaterialPage _createPage(Widget child, PageConfiguration pageConfig) {
// print('key=${pageConfig.key}=${pageConfig.}=')
return MaterialPage(
child: child,
// key: ValueKey(pageConfig.key),
name: pageConfig.path,
arguments: pageConfig);
}
void _addPageData(Widget child, PageConfiguration pageConfig) {
_pages.add(
_createPage(child, pageConfig),
);
}
void addPage(PageConfiguration? pageConfig) {
if (pageConfig == null) {
return;
}
switch (pageConfig.uiPage) {
case Pages.Splash:
_addPageData(SplashPage(), SplashPageConfig);
break;
case Pages.Main:
_addPageData(MainPage(), MainPageConfig);
break;
case Pages.Login:
_addPageData(LoginPage(), LoginPageConfig);
break;
case Pages.Settings:
_addPageData(SettingsPage(), SettingsPageConfig);
break;
case Pages.SearchList:
if (pageConfig.currentPageAction?.widget != null) {
_addPageData(pageConfig.currentPageAction!.widget!, pageConfig);
} else {
_addPageData(SearchListPage(), SearchListPageConfig);
}
break;
case Pages.Details:
case Pages.ConfirmOrder:
case Pages.PhoneLogin:
case Pages.PhoneCode:
case Pages.AliWeb:
case Pages.ImgGallery:
case Pages.NetWorkImgGallery:
case Pages.WithDraw:
case Pages.Webview:
if (pageConfig.currentPageAction?.widget != null) {
_addPageData(pageConfig.currentPageAction!.widget!, pageConfig);
}
break;
case Pages.FavoriteList:
_addPageData(FavoriteListPage(), FavoriteListPageConfig);
break;
case Pages.AddressManager:
_addPageData(AddressManagerPage(), AddressManagerPageConfig);
break;
case Pages.WelCome:
_addPageData(WelComePage(), WelComePageConfig);
break;
case Pages.WithDrawRecord:
_addPageData(WithDrawRecordPage(), WihtDrawRecordPageConfig);
break;
case Pages.BindAlipay:
_addPageData(BindAlipayPage(), BindAlipayPageConfig);
break;
case Pages.ShareApp:
_addPageData(ShareAppPage(), ShareAppPageConfig);
break;
case Pages.LineNews:
_addPageData(LineNewsPage(), LineNewsPageConfig);
break;
case Pages.TensBillions:
_addPageData(TensBillionsPage(), TensBillionsPageConfig);
break;
case Pages.RankList:
_addPageData(RankPage(), rankListPageConfig);
break;
case Pages.HighCommission: //高佣必推
_addPageData(HighCommissionPage(), HighCommissionPageConfig);
break;
case Pages.BrandList:
_addPageData(BigBrandPage(), brandListPageConfig);
break;
case Pages.College:
_addPageData(CollegePage(), CollegePageConfig);
break;
default:
break;
}
}
void replace(PageConfiguration? newRoute) {
if (newRoute == null) {
return;
}
if (_pages.isNotEmpty) {
_pages.removeLast();
}
addPage(newRoute);
}
void setPath(List
_pages.clear();
_pages.addAll(path);
}
void replaceAll(PageConfiguration? newRoute) {
if (newRoute == null) {
return;
}
setNewRoutePath(newRoute);
}
void push(PageConfiguration newRoute) {
addPage(newRoute);
}
void pushWidget(Widget child, PageConfiguration newRoute) {
_addPageData(child, newRoute);
}
void addAll(List
if (routes == null || routes.isEmpty) {
return;
}
_pages.clear();
routes.forEach((route) {
addPage(route);
});
}
@override
Future
_pages.clear();
addPage(configuration);
return SynchronousFuture(null);
}
void _setPageAction(PageAction action) {
if (action.page?.uiPage == null) {
return;
}
switch (action.page!.uiPage) {
case Pages.Splash:
SplashPageConfig.currentPageAction = action;
break;
case Pages.Login:
LoginPageConfig.currentPageAction = action;
break;
case Pages.Settings:
SettingsPageConfig.currentPageAction = action;
break;
case Pages.Details:
DetailsPageConfig.currentPageAction = action;
break;
case Pages.Webview:
WebviewPageConfig.currentPageAction = action;
break;
case Pages.SearchList:
SearchListPageConfig.currentPageAction = action;
break;
case Pages.PhoneLogin:
PhoneLoginPageConfig.currentPageAction = action;
break;
case Pages.PhoneCode:
PhoneCodePageConfig.currentPageAction = action;
break;
case Pages.FavoriteList:
FavoriteListPageConfig.currentPageAction = action;
break;
case Pages.ConfirmOrder:
ConfirmOrderPageConfig.currentPageAction = action;
break;
case Pages.AddressManager:
AddressManagerPageConfig.currentPageAction = action;
break;
case Pages.AliWeb:
AliWebPageConfig.currentPageAction = action;
break;
case Pages.ImgGallery:
ImgGalleryPageConfig.currentPageAction = action;
break;
case Pages.NetWorkImgGallery:
NetWorkImgGalleryPageConfig.currentPageAction = action;
break;
case Pages.WelCome:
WelComePageConfig.currentPageAction = action;
break;
case Pages.WithDraw:
WihtDrawPageConfig.currentPageAction = action;
break;
case Pages.WithDrawRecord:
WihtDrawRecordPageConfig.currentPageAction = action;
break;
case Pages.BindAlipay:
BindAlipayPageConfig.currentPageAction = action;
break;
case Pages.LineNews:
LineNewsPageConfig.currentPageAction = action;
break;
case Pages.TensBillions:
TensBillionsPageConfig.currentPageAction = action;
break;
case Pages.TensBillions: //高佣必推
HighCommissionPageConfig.currentPageAction = action;
break;
case Pages.RankList:
rankListPageConfig.currentPageAction = action;
break;
case Pages.BrandList:
brandListPageConfig.currentPageAction = action;
break;
case Pages.College:
CollegePageConfig.currentPageAction = action;
break;
default:
break;
}
}
List
if (!appState.splashFinished) {
replaceAll(SplashPageConfig);
} else {
switch (appState.currentAction.state) {
case PageState.none:
break;
case PageState.addPage:
_setPageAction(appState.currentAction);
addPage(appState.currentAction.page);
break;
case PageState.pop:
var popPages = appState.currentAction.pages;
if (popPages != null && popPages.isNotEmpty) {
popPages.forEach((element) {
for (int i = pages.length - 1; i >= 0; --i) {
var deletePage = pages[i];
if (deletePage.name == element.path) {
pop(page: deletePage);
}
}
});
} else {
pop();
}
break;
case PageState.replace:
_setPageAction(appState.currentAction);
replace(appState.currentAction.page);
break;
case PageState.replaceAll:
_setPageAction(appState.currentAction);
replaceAll(appState.currentAction.page);
break;
case PageState.addWidget:
_setPageAction(appState.currentAction);
pushWidget(
appState.currentAction.widget!, appState.currentAction.page!);
break;
case PageState.addAll:
addAll(appState.currentAction.pages);
break;
}
}
appState.resetCurrentAction();
return List.of(_pages);
}
void parseRoute(Uri uri) {
print(
'uri=${uri.pathSegments}=${uri.queryParameters}=${uri.pathSegments.isEmpty}=${uri.pathSegments.length}=');
// 不处理淘宝和微信的回调
if (uri.scheme.contains('tbopen') ||
uri.scheme.contains('wxc15014d378f06a10')) {
return;
}
if (uri.pathSegments.isEmpty) {
setNewRoutePath(SplashPageConfig);
return;
}
if (uri.pathSegments.length == 1) {
final path = uri.pathSegments[0];
switch (path) {
case 'splash':
replaceAll(SplashPageConfig);
break;
case 'login':
addPage(LoginPageConfig);
break;
case 'details':
String goodsID = uri.queryParameters['goodsid'] ?? '0';
int platform = int.parse(uri.queryParameters['platform'] ?? '1');
pushWidget(GoodsDetailPage(goodsID, platformType: platform),
DetailsPageConfig);
break;
case 'settings':
addPage(SettingsPageConfig);
break;
case 'webview':
String url = uri.queryParameters['url'] ?? '';
String title = uri.queryParameters['title'] ?? '';
pushWidget(WebviewPage(url: url, title: title), WebviewPageConfig);
break;
case 'html':
String body = uri.queryParameters['body'] ?? '';
String title = uri.queryParameters['title'] ?? '';
String urlStr = Uri.dataFromString(body,
mimeType: 'text/html', encoding: Encoding.getByName('utf-8'))
.toString();
pushWidget(WebviewPage(url: urlStr, title: title), WebviewPageConfig);
break;
case 'search_list':
addPage(SearchListPageConfig);
break;
case 'phone_login':
addPage(PhoneLoginPageConfig);
break;
case 'phone_code':
addPage(PhoneCodePageConfig);
break;
case 'favorite_list':
addPage(FavoriteListPageConfig);
break;
case 'confirm_order':
addPage(ConfirmOrderPageConfig);
break;
case 'address_manager':
addPage(AddressManagerPageConfig);
break;
case 'ali_web':
addPage(AliWebPageConfig);
break;
case 'img_gallery':
addPage(ImgGalleryPageConfig);
break;
case 'network_gallery':
addPage(NetWorkImgGalleryPageConfig);
break;
case 'welcome':
addPage(WelComePageConfig);
break;
case 'withdraw_page':
addPage(WihtDrawPageConfig);
break;
case 'withdraw_record':
addPage(WihtDrawRecordPageConfig);
break;
case 'bind_alipay':
addPage(BindAlipayPageConfig);
break;
case 'column_discount':
addPage(LineNewsPageConfig);
break;
case 'column_tens_millions':
addPage(TensBillionsPageConfig);
break;
case 'column_high_brokerage': //高佣必推
addPage(HighCommissionPageConfig);
break;
case 'column_big_brand':
addPage(brandListPageConfig);
break;
case 'column_hot':
addPage(rankListPageConfig);
break;
case 'college':
addPage(CollegePageConfig);
break;
}
}
}
}
配置方法