regex_router 1.0.4 copy "regex_router: ^1.0.4" to clipboard
regex_router: ^1.0.4 copied to clipboard

outdated

Router with regex support. Use names to provide arguments like in REST.

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");
  }
}
14
likes
30
points
34
downloads

Publisher

verified publisherjelenski.net

Weekly Downloads

Router with regex support. Use names to provide arguments like in REST.

License

MIT (license)

Dependencies

collection, flutter

More

Packages that depend on regex_router