您好,登錄后才能下訂單哦!
route_generator是什么
這是一個簡單的 Flutter 路由生成庫,只需要少量的代碼,然后利用注解配合源代碼生成,自動生成路由表,省去手工管理路由代碼的煩惱。
特性
依賴
dependencies: # Your other regular dependencies here route_annotation: ^0.1.0 dev_dependencies: # Your other dev_dependencies here build_runner: ^1.5.0 route_generator: ^0.1.2
生成代碼
單次構建
在項目根目錄中運行flutter pub run build_runner build,可以在需要時為項目生成路由代碼。這會觸發一次性構建,該構建遍歷源文件,選擇相關文件,并為它們生成必要的路由代碼。雖然這很方便,但如果您不必每次在模型類中進行更改時都必須手動構建,那么你可以選擇持續構建。
持續構建
在項目根目錄中運行flutter pub run build_runner watch來啟動watcher,它可以使我們的源代碼生成過程更加方便。它會監視項目文件中的更改,并在需要時自動構建必要的文件。
route_annotation
annotation | description |
---|---|
Router | 此注解用來標志某個為 Flutter App 的類,并以此生成相應的路由代碼 |
RoutePage | 此注解用來注解一個路由頁面 |
RouteParameter | 一個用來標志頁面參數的注解,只為可選參數設計。用于 RoutePage 。 |
RouteField | 此注解用來標志一個完全自定義的路由,被注解的對象必須作為路由頁面類靜態字段 |
PageRouteBuilderFuntcion | 這個注解用來標識一個路由頁面的 RouteFactory 靜態方法 |
RoutePageBuilderFunction | 這個注解用來標識一個路由頁面的 RoutePageBuilder靜態方法 |
RouteTransitionBuilderFunction | 這個注解用來標識一個路由頁面的 TransitionBuilder 靜態方法 |
RouteTransitionDurationField | 這個注解用來標識一個自定義路由頁面的過渡時長 |
代碼示例
定義路由 App
@Router() class DemoApp extends StatefulWidget { @override _DemoAppState createState() => _DemoAppState(); } class _DemoAppState extends State<DemoApp> { @override Widget build(BuildContext context) { return MaterialApp( initialRoute: "/", onGenerateRoute: onGenerateRoute, ); } }
定義路由頁面
// isInitialRoute為true表示它將作為initial page @RoutePage(isInitialRoute: true) class HomePage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold(); } }
定義路由頁面參數
對于單個參數
@RoutePage(params: [RouteParameter("title")]) class OneArgumentPage extends StatelessWidget { final String title; const OneArgumentPage({Key key, this.title}) : super(key: key); @override Widget build(BuildContext context) { return Container(); } }
導航
Navigator.of(context).pushNamed( ROUTE_ONE_ARGUMENT_PAGE, arguments: "title is empty", );
注意事項:
對于單個參數的路由,利用Navigator進行導航的時候arguments即為原始參數。
對于多個參數
@RoutePage(params: [RouteParameter("title"), RouteParameter("subTitle")]) class TwoArgumentPage extends StatelessWidget { final String title; final String subTitle; TwoArgumentPage({this.title, Key key, this.subTitle}) : super(key: key); @override Widget build(BuildContext context) { return Scaffold(); } }
導航
Navigator.of(context).pushNamed( ROUTE_TWO_ARGUMENT_PAGE, arguments: { "title": _titleController.text.isNotEmpty ? _titleController.text : "title is empty", "subTitle": _subTitleController.text.isNotEmpty ? _subTitleController.text : "sub title is empty", }, );
注意事項:
對于多個參數的路由,利用Navigator進行導航的時候arguments必須為Map<string,dynamic>。
如果你不需要自定義路由,以下部分,你可以什么都不用添加,就讓route_generator為你自動生成相關代碼吧!
自定義路由(優先級:3)
這種方法自定義路由的優先級最高,如果同時存在多種自定義路由選擇,該種方案最先被選擇。
@RoutePage() class CustomRoutePage extends StatelessWidget { @RouteField() static Map<String, RouteFactory> route = <String, RouteFactory>{ 'custom_route': (RouteSettings settings) => MaterialPageRoute(builder: (BuildContext context) => CustomRoutePage()), 'alias_route': (RouteSettings settings) => PageRouteBuilder( pageBuilder: (BuildContext context, Animation animation, Animation secondaryAnimation) => CustomRoutePage(), ), }; ... }
它會生成如下代碼:
Map<String, RouteFactory> _customRoutePage = CustomRoutePage.route;
自定義路由(優先級:2)
這種方法自定義路由的優先級較低,如果同時存在多種自定義路由選擇,則按優先級從大到小選擇。
@RoutePage() class CustomRoutePage extends StatelessWidget { @PageRouteBuilderFuntcion() static Route buildPageRoute(RouteSettings settings) => PageRouteBuilder( pageBuilder: (BuildContext context, Animation animation, Animation secondaryAnimation) => CustomRoutePage(), ); ... }
它會生成如下代碼:
Map<String, RouteFactory> _customRoutePage = <String, RouteFactory>{ 'custom_route_page': CustomRoutePage.buildPageRoute, };
自定義路由(優先級:1)
這種方法自定義路由的優先級最低,如果同時存在多種自定義路由選擇,則按優先級從大到小選擇。
@RoutePage() class CustomRoutePage extends StatelessWidget { // RoutePageBuilderFunction注解表明這個方法用來定義如何返回RoutePage // 它是可選的 @RoutePageBuilderFunction() static Widget buildPage(BuildContext context, Animation animation, Animation secondaryAnimation, RouteSettings settings) => CustomRoutePage(); // RouteTransitionBuilderFunction注解表明這個方法用來定義如何應用動畫過渡 // 它是可選的 @RouteTransitionBuilderFunction() static Widget buildTransitions( BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child, RouteSettings settings) => child; // RouteTransitionDurationField注解表明這個字段用來定義頁面過渡時常長,默認值為300 milliseconds // 它是可選的 @RouteTransitionDurationField() static Duration transitionDuration = Duration(milliseconds: 400); ... }
它會生成如下代碼:
Map<String, RouteFactory> _customRoutePage = <String, RouteFactory>{ 'custom_route_page': (RouteSettings settings) => PageRouteBuilder( pageBuilder: (context, animation, secondaryAnimation) => CustomRoutePage(), transitionsBuilder: (context, animation, secondaryAnimation, child) => CustomRoutePage.buildTransitions( context, animation, secondaryAnimation, child, settings), transitionDuration: CustomRoutePage.transitionDuration, ), };
注意事項
最終生成代碼
最終生成的文件名為FILENAME.route.dart
其中FILENAME是被Router注解的App類所在的文件名。
// GENERATED CODE - DO NOT MODIFY BY HAND // ************************************************************************** // RouteGenerator // ************************************************************************** import 'package:flutter/material.dart'; import 'home_page.dart'; import 'custom_route_page.dart'; import 'custom_route_name_page.dart'; import 'second_page.dart'; import 'one_arguement_page.dart'; import 'two_arguement_page.dart'; const ROUTE_HOME = '/'; const ROUTE_CUSTOM_ROUTE_PAGE = 'custom_route_page'; const ROUTE_CUSTOM = 'custom'; const ROUTE_SECOND_PAGE = 'second_page'; const ROUTE_ONE_ARGUMENT_PAGE = 'one_argument_page'; const ROUTE_TWO_ARGUMENT_PAGE = 'two_argument_page'; RouteFactory onGenerateRoute = (settings) => Map.fromEntries([ ..._home.entries, ..._customRoutePage.entries, ..._custom.entries, ..._secondPage.entries, ..._oneArgumentPage.entries, ..._twoArgumentPage.entries, ])[settings.name](settings); Map<String, RouteFactory> _home = <String, RouteFactory>{ '/': (RouteSettings settings) => MaterialPageRoute( builder: (BuildContext context) => HomePage(), ), }; Map<String, RouteFactory> _customRoutePage = <String, RouteFactory>{ 'custom_route_page': (RouteSettings settings) => PageRouteBuilder( pageBuilder: (context, animation, secondaryAnimation) => CustomRoutePage.buildPage( context, animation, secondaryAnimation, settings), transitionsBuilder: (context, animation, secondaryAnimation, child) => CustomRoutePage.buildTransitions( context, animation, secondaryAnimation, child, settings), transitionDuration: CustomRoutePage.transitionDuration, ), }; Map<String, RouteFactory> _custom = <String, RouteFactory>{ 'custom': (RouteSettings settings) => MaterialPageRoute( builder: (BuildContext context) => CustomRoutePageName(), ), }; Map<String, RouteFactory> _secondPage = <String, RouteFactory>{ 'second_page': (RouteSettings settings) => MaterialPageRoute( builder: (BuildContext context) => SecondPage(), ), }; Map<String, RouteFactory> _oneArgumentPage = <String, RouteFactory>{ 'one_argument_page': (RouteSettings settings) => MaterialPageRoute( builder: (BuildContext context) => OneArgumentPage(title: settings.arguments), ), }; Map<String, RouteFactory> _twoArgumentPage = <String, RouteFactory>{ 'two_argument_page': (RouteSettings settings) => MaterialPageRoute( builder: (BuildContext context) => TwoArgumentPage( title: (settings.arguments as Map<String, dynamic>)['title'], subTitle: (settings.arguments as Map<String, dynamic>)['subTitle'], ), ), };
常見問題
沒有生成路由文件
請檢查是否添加了Router注解
Example
獲取更詳細信息,請參閱example
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。