simplified_router 0.0.4 copy "simplified_router: ^0.0.4" to clipboard
simplified_router: ^0.0.4 copied to clipboard

A simpler API for RouterDelegate and Router

simplified_router #

(Experimental)

A simpler routing API for Flutter's Router and related classes.

  • Implements RouterDelegate and RouteInformationParser for you.
  • Handles the router-related state for you. The app state is stored as a String containing the current path (/users/123).
  • Change the URL using a single method - navigateTo(path).
  • Leaves the route parsing to you.
  • Validate routes coming from the engine using shouldNavigateTo(String path)
  • TODO: Provide a SimplifiedRouterMixin with builder and shouldNavigateTo methods.
  Widget _appBuilder(BuildContext context, String routePath) {
    var uri = Uri.parse(routePath);
    var segments = uri.pathSegments;
    return Navigator(
      key: navigatorKey,
      pages: [
        if (segments.length == 0)
          MaterialPage(
            // ...

Usage #

Import this package and create a SimplifiedRouter class in a State object at the root of your application. Then provide a builder method that takes the BuildContext and String contining the current route path:

import 'package:flutter/material.dart';
import 'package:simplified_router/simplified_router.dart';

void main() {
  runApp(ExampleApp());
}

class ExampleApp extends StatefulWidget {
  @override
  _ExampleAppState createState() => _ExampleAppState();
}

class _ExampleAppState extends State<ExampleApp> {
  final navigatorKey = GlobalKey<NavigatorState>();
  late final SimplifiedRouter _router;

  _ExampleAppState() {
    _router =
        SimplifiedRouter(builder: _appBuilder, navigatorKey: navigatorKey);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      routerDelegate: _router.delegate,
      routeInformationParser: _router.parser,
    );
  }

  Widget _appBuilder(BuildContext context, String routePath) {
    var uri = Uri.parse(routePath);
    var segments = uri.pathSegments;
    return Navigator(
      key: navigatorKey,
      pages: [
        if (segments.length == 0)
          MaterialPage(
            child: Scaffold(
              body: Center(
                child: Text('Home Page'),
              ),
            ),
          ),
      ],
    );
  }
}

Validation #

1
likes
110
pub points
0%
popularity

Publisher

unverified uploader

A simpler API for RouterDelegate and Router

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

flutter

More

Packages that depend on simplified_router