regex_router 0.1.0+2

regex_router #

Material router with regex path support.

Usage #

Just define router instance and pass router.generateRoute to MaterialApp or any other Navigator.

final router = RegexRouter.create({
    "/": (context, _) => SamplePage(title: "BLoC showcase"),
    "/post/:id": (context, args) => PostPage(
        id: args["id"]), // Access path arguments with their names.
    "/post/:id/attachment/:attachmentId": (context, args) => PostAttachmentPage(
        id: args["id"],
        attachmentId: args["attachmentId"],
        body: args.body as Map<String, String>), // Access "object" arguments from `NavigatorState.pushNamed`.
});

return MaterialApp(
    onGenerateRoute: router.generateRoute,
    initialRoute: "/",
    title: 'Flutter Demo',
    theme: ThemeData(primarySwatch: Colors.blue),
);

// Somewhere inside `MaterialApp` container:

Navigator.of(context).pushNamed("/post/7"); // Navigate to path with argument.
Navigator.of(context).pushNamed("/post/7/attachment/8", argument: {"payload": "data"}); // Navigate to path with argument and body.

Samples #

You can experiment with a sample application hosted here: https://gitlab.com/marcin.jelenski/bloc-showcase

Also test directory contains all router use cases.

[0.1.0+2] - 2.12.2019

  • Add example.

[0.1.0+1] - 2.12.2019

  • Promote to 0.1.0.

[0.0.2] - 1.12.2019

  • Fix documentation typos.

[0.0.1] - 1.12.2019

  • Initial release.

example/main.dart

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:regex_router/regex_router.dart';

void main() {
  // Create router instance
  final router = RegexRouter.create({
    "/": (context, _) => HomePage(),
    "/second/:id/": (context, args) => AnotherPage(id: args["id"]),
    "/withBody": (context, args) => PageWithBody(body: args.body),
  });

  // Run material app with router
  runApp(MaterialApp(
    onGenerateRoute: router.generateRoute,
    initialRoute: "/",
    theme: ThemeData(
      primarySwatch: Colors.blue,
    ),
  ));

  // ... or use it with any Navigator
  Navigator(
    onGenerateRoute: router.generateRoute,
    initialRoute: "/",
  );
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        MaterialButton(
          onPressed: () => Navigator.of(context).pushNamed("/second/7"),
          child: Text("Second"),
        ),
        MaterialButton(
          onPressed: () => Navigator.of(context)
              .pushNamed("/withBody", arguments: {"key": "value"}),
          child: Text("Second"),
        ),
      ],
    );
  }
}

class AnotherPage extends StatelessWidget {
  final String id;

  const AnotherPage({Key key, @required this.id}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Text("Id: $id");
  }
}

class PageWithBody extends StatelessWidget {
  final Object body;

  const PageWithBody({Key key, @required this.body}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Text("Body: $body");
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  regex_router: ^0.1.0+2

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:regex_router/regex_router.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
35
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
67
Learn more about scoring.

We analyzed this package on Feb 13, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.5
  • Flutter: 1.12.13+hotfix.7

Health suggestions

Fix lib/regex_router.dart. (-0.50 points)

Analysis of lib/regex_router.dart reported 1 hint:

line 56 col 27: The value of the field '_routeRegexMap' isn't used.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.2 <3.0.0
collection ^1.0.0 1.14.11 1.14.12
flutter 0.0.0
Transitive dependencies
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
effective_dart ^1.1.0
flutter_test
mockito ^4.1.1
test ^1.3.0