ActivityRouter class

URL routing implemented with Flutter Router and declarative Navigators.

Use MaterialApp.router with router.config. The gallery demonstrates nested/shell/restorable routes, async guards and unknown-route recovery. Client guards are not server authorization. Pushes update browser URLs and preserve typed results.

Gallery walkthrough · browser route /#/navigation. These examples use the current checkout; published 1.x APIs can differ.

Named parameters and query values

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

void main() => runApp(const RouteOwner());

class RouteOwner extends StatefulWidget {
  const RouteOwner({super.key});
  @override
  State<RouteOwner> createState() => _RouteOwnerState();
}

class _RouteOwnerState extends State<RouteOwner> {
  late final router = ActivityRouter(
    routes: [
      ActivityRoute(
        path: '/',
        builder:
            (context, state) => Scaffold(
              body: TextButton(
                onPressed:
                    () => ActivityRouter.of(context).goNamed(
                      'detail',
                      pathParameters: {'id': '42'},
                      queryParameters: {'tab': 'summary'},
                    ),
                child: const Text('Open detail'),
              ),
            ),
      ),
      ActivityRoute(
        path: '/detail/:id',
        name: 'detail',
        builder:
            (_, state) => Scaffold(
              body: Text(
                '${state.pathParameters['id']} / ${state.queryParameters['tab']}',
              ),
            ),
      ),
    ],
  );
  @override
  void dispose() {
    router.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) =>
      MaterialApp.router(routerConfig: router.config);
}

Return a typed detail result

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

void main() => runApp(const RouteOwner());

class RouteOwner extends StatefulWidget {
  const RouteOwner({super.key});
  @override
  State<RouteOwner> createState() => _RouteOwnerState();
}

class _RouteOwnerState extends State<RouteOwner> {
  late final router = ActivityRouter(
    routes: [
      ActivityRoute(
        path: '/',
        builder:
            (context, state) => Scaffold(
              body: TextButton(
                onPressed: () async {
                  final selected = await ActivityRouter.of(
                    context,
                  ).pushNamed<int>('pick');
                  if (context.mounted && selected != null)
                    ScaffoldMessenger.of(context).showSnackBar(
                      SnackBar(content: Text('Selected $selected')),
                    );
                },
                child: const Text('Choose'),
              ),
            ),
      ),
      ActivityRoute(
        path: '/pick',
        name: 'pick',
        builder:
            (context, state) => Scaffold(
              body: TextButton(
                onPressed: () => ActivityRouter.of(context).pop<int>(42),
                child: const Text('Return 42'),
              ),
            ),
      ),
    ],
  );
  @override
  void dispose() {
    router.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) =>
      MaterialApp.router(routerConfig: router.config);
}
Inheritance

Constructors

ActivityRouter({required List<ActivityRouteBase> routes, String initialLocation = '/', ActivityRedirect? redirect, Widget errorBuilder(BuildContext, ActivityRouteState)?, int redirectLimit = 8, ActiveController? refreshController, String? restorationScopeId, GlobalKey<NavigatorState>? navigatorKey, Codec<Object?, Object?>? extraCodec, List<NavigatorObserver> observers = const [], bool overridePlatformDefaultLocation = false, bool debugLogDiagnostics = false})

Properties

canPop → bool
no setter
config ↔ RouterConfig<ActivityRouteConfiguration>
latefinal
currentConfiguration → Uri
no setter
debugLogDiagnostics → bool
final
errorBuilder → Widget Function(BuildContext, ActivityRouteState)?
final
extraCodec → Codec<Object?, Object?>?
final
hashCode → int
The hash code for this object.
no setterinherited
hasListeners → bool
Whether any listeners are currently registered.
no setterinherited
final
observers → List<NavigatorObserver>
final
redirect → ActivityRedirect?
final
redirectLimit → int
final
restorationScopeId → String?
final
routeInformationParser ↔ ActivityRouteInformationParser
latefinal
routeInformationProvider ↔ PlatformRouteInformationProvider
latefinal
routerDelegate ↔ ActivityRouterDelegate
latefinal
routes → List<ActivityRouteBase>
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
state → ActivityRouteState
no setter

Methods

addListener(VoidCallback listener) → void
Register a closure to be called when the object changes.
inherited
dispose() → void
Discards any resources used by the object.
override
go(String location, {Object? extra}) → void
goNamed(String name, {Map<String, String> pathParameters = const {}, Map<String, dynamic> queryParameters = const {}, Object? extra, String? fragment}) → void
namedLocation(String name, {Map<String, String> pathParameters = const {}, Map<String, dynamic> queryParameters = const {}, String? fragment}) → String
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
notifyListeners() → void
Call all the registered listeners.
inherited
pop<T extends Object?>([T? result]) → bool
Requests a pop. The exit guard may asynchronously veto it.
push<T extends Object?>(String location, {Object? extra}) → Future<T?>
pushNamed<T extends Object?>(String name, {Map<String, String> pathParameters = const {}, Map<String, dynamic> queryParameters = const {}, Object? extra, String? fragment}) → Future<T?>
refresh() → void
removeListener(VoidCallback listener) → void
Remove a previously registered closure from the list of closures that are notified when the object changes.
inherited
replace<T extends Object?>(String location, {Object? extra}) → Future<T?>
setNewRoutePath(Uri configuration) → Future<void>
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Methods

of(BuildContext context) → ActivityRouter