關於flutter路由,在小項目中,就按照原生寫法,可是在大型項目中,這樣的我就不會進行推薦,我這裏使用的fluro路由管理方案,大型項目中很是nice,如今開始啥也不說了,看我操做就好了web
注:自認爲是Flutter最亮,最時髦,最酷的路由器。複製代碼
在大型項目中,你要問我什麼叫大型,我感受超過20就算了吧,具體什麼叫大型,我也不知道,哈哈哈bash
首先呢,在項目中新建一個routers文件夾,與main.dart同級,這個文件夾就是咱們的路由文件夾,建好以後,咱們開始進行建立路由app
import 'package:fluro/fluro.dart';
class Application {
static Router router;
}複製代碼
這個頁面就配置咱們的路由調轉連接模塊框架
import 'package:flutter/material.dart';
import 'package:fluro/fluro.dart';
import './router_handler.dart';
class Routes {
static String root = '/';
static String indexPage = '/indexPage';
static void configureRoutes(Router router) {
router.notFoundHandler = new Handler(
handlerFunc: (BuildContext context, Map<String, List<String>> params) {
print('ERROR====>ROUTE WAS NOT FONUND!!!'); // 找不到路由,跳轉404頁面
// Navigator.pushNamed(context, "/notFound");
// Navigator.of(context).pushNamed("NotFound");
print('找不到路由,404');
},
);
// 路由頁面配置
router.define(homePage, handler: homePageHanderl);
}
}複製代碼
handler就是每一個路由的規則,編寫handler就是配置路由規則,好比咱們要傳遞參數,參數的值是什麼,這些都須要在Handler中完成。less
import 'package:flutter/material.dart';
import 'package:fluro/fluro.dart';
import 'package:maitianshanglv_driver/pages/index_page.dart';
import 'package:maitianshanglv_driver/pages/user_module/details_system_notification.dart';
// 首頁
Handler indexPageHanderl = Handler(
handlerFunc: (BuildContext context, Map<String, List<String>> params) {
return IndexPage();
},
);
// 系統通知詳情
Handler detailsSystemNotificationHanderl = Handler(
handlerFunc: (BuildContext context, Map<String, List<String>> params) {
String id = params['id'].first;
String headTitle = params['headTitle'].first;
return DetailsSystemNotification(id: id, headTitle: headTitle);
},
);複製代碼
這個裏面主要是進行路由跳轉方法的公共書寫ide
import 'package:flutter/material.dart';
import 'package:fluro/fluro.dart';
import 'package:maitianshanglv_driver/pages/app_common_module/login.dart';
import 'package:maitianshanglv_driver/pages/index_page.dart';
import './application.dart';import './routes.dart';
class NavigatorUtil {
// 返回
static void goBack(BuildContext context) {
/// 其實這邊調用的是
Navigator.pop(context);
Application.router.pop(context);
}
// 帶參數的返回
static void goBackWithParams(BuildContext context, result) {
Navigator.pop(context, result);
}
// 路由返回指定頁面
static void goBackUrl(BuildContext context, String title) {
Navigator.popAndPushNamed(context, title);
}
// 跳轉到主頁面
static void goHomePage(BuildContext context) {
Application.router.navigateTo(context, Routes.homePage, replace: true);
}
/// 跳轉到 轉場動畫 頁面 , 這邊只展現 inFromLeft ,剩下的本身去嘗試下,
/// 框架自帶的有 native,nativeModal,inFromLeft,inFromRight,inFromBottom,fadeIn,custom
static Future jump(BuildContext context, String title) {
return Application.router.navigateTo(context, title, transition: TransitionType.inFromRight);
/// 指定了 轉場動畫
}
/// 框架自帶的有 native,nativeModal,inFromLeft,inFromRight,inFromBottom,fadeIn,custom
static Future jumpLeft(BuildContext context, String title) { return Application.router.navigateTo(context, title, transition: TransitionType.inFromLeft); /// 指定了 轉場動畫 }
static Future jumpRemove(BuildContext context) { return Navigator.of(context).pushAndRemoveUntil( MaterialPageRoute( builder: (context) => IndexPage(), ),
(route) => route == null); }
/// 自定義 轉場動畫
static Future gotransitionCustomDemoPage(BuildContext context, String title) {
var transition = (BuildContext context, Animation<double> animation,
Animation<double> secondaryAnimation, Widget child) {
return new ScaleTransition(
scale: animation,
child: new RotationTransition(
turns: animation,
child: child,
),
};
return Application.router.navigateTo(context, title,
transition: TransitionType.custom,
/// 指定是自定義動畫
transitionBuilder: transition,
/// 自定義的動畫
transitionDuration: const Duration(milliseconds: 600));
/// 時間
}
/// 使用 IOS 的 Cupertino 的轉場動畫,這個是修改了源碼的 轉場動畫
/// Fluro自己不帶,可是 Flutter自帶
static Future gotransitionCupertinoDemoPage(
BuildContext context, String title) {
return Application.router.navigateTo(context, title, transition: TransitionType.cupertino);
}
// 跳轉到主頁面IndexPage並刪除當前路由
static void goToHomeRemovePage(BuildContext context) {
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(
builder: (context) => IndexPage(),
), (route) => route == null);
}
// 跳轉到登陸頁並刪除當前路由
static void goToLoginRemovePage(BuildContext context) {
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(
builder: (context) => Login(),
), (route) => route == null);
}
/// 跳轉到 webview
static Future jumpWebView(BuildContext context, String urlParameter) {
String urlData = Uri.encodeComponent(urlParameter);
return Application.router.navigateTo(context, Routes.webView + "?url=$urlData", transition: TransitionType.inFromRight);
/// 指定了 轉場動畫
}
}複製代碼
須要到main.dart文件夾進行註冊一下動畫
import 'package:flutter/material.dart';
import 'package:fluro/fluro.dart';
import 'pages/index_page.dart';
import 'routers/routes.dart';
import 'routers/application.dart';
import 'package:fluwx/fluwx.dart' as fluwx;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
//-------------------路由主要代碼start
final router = Router();
Routes.configureRoutes(router);
Application.router = router;
//-------------------路由主要代碼end
return Container(
child: MaterialApp(
title: '網約車司機端',
//----------------路由主要代碼start
onGenerateRoute: Application.router.generator,
//----------------路由主要代碼end
debugShowCheckedModeBanner: false,
theme: ThemeData(
brightness: Brightness.light, // 總體頁面背景顏色
primaryColor: Color.fromARGB(255, 78, 79, 95), // 總體AppBar顏色
// accentColor: Colors.cyan[600],
),
home: IndexPage(),
),
);
}
}複製代碼
大功告成,到這裏關於flutter採用fluro方式進行的大型項目路由管理就算成功,ui
NavigatorUtil.jump(context, '/successfulCancellationOrder');
NavigatorUtil.jump(context, '/detailsSystemNotification?id="12332"&headTitle="asdas"');
複製代碼