route_pattern_generator
所属分类:自动编程
开发工具:Dart
文件大小:18KB
下载次数:0
上传日期:2021-05-06 18:05:39
上 传 者:
sh-1993
说明: 一个Dart静态代码生成器,从路由uri模式生成匹配器和生成器。
(A Dart static code generator that produces matchers and builders from route uri patterns.)
文件列表:
.vscode (0, 2021-05-07)
.vscode\launch.json (406, 2021-05-07)
publish.dart (1950, 2021-05-07)
route_pattern (0, 2021-05-07)
route_pattern\LICENSE (1069, 2021-05-07)
route_pattern\lib (0, 2021-05-07)
route_pattern\lib\route_pattern.dart (82, 2021-05-07)
route_pattern\lib\src (0, 2021-05-07)
route_pattern\lib\src\annotations.dart (207, 2021-05-07)
route_pattern\lib\src\route.dart (2718, 2021-05-07)
route_pattern\lib\src\router.dart (314, 2021-05-07)
route_pattern\pubspec.yaml (276, 2021-05-07)
route_pattern\test (0, 2021-05-07)
route_pattern\test\route.dart (4605, 2021-05-07)
route_pattern\test\router.dart (3135, 2021-05-07)
route_pattern_generator (0, 2021-05-07)
route_pattern_generator\LICENSE (1069, 2021-05-07)
route_pattern_generator\build.yaml (514, 2021-05-07)
route_pattern_generator\lib (0, 2021-05-07)
route_pattern_generator\lib\builder.dart (249, 2021-05-07)
route_pattern_generator\lib\src (0, 2021-05-07)
route_pattern_generator\lib\src\builders.dart (7907, 2021-05-07)
route_pattern_generator\lib\src\generator.dart (2037, 2021-05-07)
route_pattern_generator\lib\src\parsing.dart (1905, 2021-05-07)
route_pattern_generator\pubspec.yaml (485, 2021-05-07)
route_pattern_generator\test (0, 2021-05-07)
route_pattern_generator\test\parsing.dart (2252, 2021-05-07)
# route_pattern_generator
A Dart static code generator that produces matchers and builders from route URI patterns.
## Quickstart
Define your patterns as constant strings annoted with `@route`.
```dart
import 'package:route_pattern/route_pattern.dart';
import 'package:flutter/widgets.dart';
part 'routes.g.dart';
@RoutePattern("/?tab&[int]scroll")
Route home(RouteSettings settings, HomeRouteArguments arguments) {
// returns a Route
}
@RoutePattern("/article/:[int]id")
Route article(RouteSettings settings, ArticleRouteArguments arguments) {
// returns a Route
}
```
Each constant will generate a `Route` with a `build` and `match` method, and a associated `Argument` class ([an example of the generate sources is available in the sample](https://github.com/aloisdeniel/route_pattern_generator/blob/master/sample/lib/routes.g.dart)).
```dart
// Build specific routes
final path = Routes.home.build(HomeRouteArguments(tab: "users"));
expect(path, "/?tab=users");
final path = Routes.article.build(ArticleRouteArguments(id: "12345"));
expect(path, "/article/12345");
// Match specific routes
final match = Routes.home.match("/?tab=users");
expect(match.isSuccess, true);
expect(match.arguments.tab, 'users');
final match = Routes.article.match("/article/12345");
expect(match.isSuccess, true);
expect(match.arguments.id, '12345');
// Or get the first matching route
final match = Routes.match("/article/12345");
if(match is MatchResult
) {
expect(match.arguments.id, '12345');
}
```
A `Routes.onGenerateRoute` method is also generated to use in your app or navigator. It will match the first route which settings name matches a pattern and call your declared function with corresponding arguments.
```dart
import 'package:sample/routes.dart';
import 'package:flutter/material.dart';
class ExampleApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
onGenerateRoute: Routes.onGenerateRoute,
// ...
);
}
}
```
To navigate to a given route, a `Routes.push` method is also available :
```dart
await Routes.push(context, ArticleRouteArguments(id: '12345'));
```
## How to use
### Install
There are a few separate packages you need to install:
```yaml
dependencies:
route_pattern:
dev_dependencies:
route_pattern_generator:
build_runner:
```
### Pattern format
A route pattern is composed of static segments separated with `/`, **required** parameters as dynamic segments (starting with `:`), and **optional** parameters as query parameters (starting with `?` and separated by `&`).
### Typing parameters
By default, arguments are of type `String`, but a custom type surrounded with `[` and `]` can be added at the beginning of a required or optional parameter. This type must have static `T parse(String value)` and `String toString()` methods to serialize and deserialize arguments from path.
#### Example
`/article/:[int]id/details?tab&[int]scroll`
* `article` : static segment
* `id` : required dynamic segment of type `int`
* `details` : static segment
* `tab` : optionnal query parameter of type `String`
* `scroll` : optionnal query parameter of type `int`
This example will match those URIs :
* `/article/2***36/details`
* `/article/1234/details?tab=second`
* `/article/***904/details?tab=first&scroll=8`
### Run the generator
To run the generator, you must use `build_runner` cli:
```sh
flutter pub pub run build_runner watch
```
近期下载者:
相关文件:
收藏者: