digita-router

Pub Version Platform License: MIT

A lightweight, context-free navigation package for Flutter, written entirely in Dart. Navigate between pages without needing BuildContext.

Works with any architecture

Whether you're using:

  • BLoC
  • Riverpod
  • Provider
  • MVVM
  • Clean Architecture
    digita_router lets you navigate globally with no BuildContext, improving readability, functionality and testability.

Features

  • No BuildContext needed
  • Simple and declarative navigation API
  • Seamlessly integrates with Flutter's built-in routing
  • Compatible with architectures like BLoC, Riverpod, MVVM, and plain Flutter
  • Designed for testability, maintainability, and scalability

You are invited to contribute!

If you are a Flutter developer eager to contribute, you're more than welcome!
Check out the contributing guide to get started.


Installation

Add this to your pubspec.yaml:

dependencies:
  digita_router: ^0.2.0

Then run

flutter pub get

Or install directly with:

flutter pub add digita_router

Getting Started

digita is a global instance providing navigation methods without context

1. Import digita_router and inject the navigatorKey and use it on the go

import 'package:flutter/material.dart';

// Import digita_router
import 'package:digita_router/digita_router.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // Just add this one line and digita_router is ready to go!
      navigatorKey: digita.navigatorKey, // <--- Must add this line
      routes: {
        '/': (_) => const HomePage(),
        '/details': (_) => const DetailsPage(),
        '/another': (_) => const AnotherPage(),
      },
    );
  }
}

2. Use Anywhere — Without Context

// Push a widget-based page
digita.openPage(const DetailsPage());

// Close the current page
digita.closePage()

// Close all the pages and back to home
digita.closeAllPages()

// Push a named route
digita.openRoute('/details');

// Close route
// Alias of digita.closePage()
digita.closeRoute()

// Replace current with widget
digita.replacePageWith(const AnotherPage());

// Replace current with named route
digita.replaceRouteWith('/another');

// Go back
// Alias of digita.closePage()
digita.goBack();

// Pop to a specific route
digita.goBackUntil('/');

Example uses of digita_router

// Push a new widget-based page onto the stack
ElevatedButton(
  onPressed: () => digita.openPage(const DetailsPage()),
  child: const Text('View Details'),
);

// Close the current page (pops the top route from the navigation stack)
ElevatedButton(
  onPressed: () => digita.closePage(),
  child: const Text('Go Back'),
);

// Close all pages and return to the root ("/") route
ElevatedButton(
  onPressed: () => digita.closeAllPages(),
  child: const Text('Back to Home'),
);

// Navigate to a named route (pushes '/details' onto the stack)
ElevatedButton(
  onPressed: () => digita.openRoute('/details'),
  child: const Text('View Details (Named)'),
);

// Close the current named route (same as closePage, but semantically named)
ElevatedButton(
  onPressed: () => digita.closeRoute(),
  child: const Text('Go Back'),
);

// Replace the current page with a new widget-based page (no back stack)
ElevatedButton(
  onPressed: () => digita.replacePageWith(const AnotherPage()),
  child: const Text('Replace with Another Page'),
);

// Replace the current page with a named route (no back stack)
ElevatedButton(
  onPressed: () => digita.replaceRouteWith('/another'),
  child: const Text('Replace with Named Route'),
);

// Alias for closePage(); used to go back to previous screen
ElevatedButton(
  onPressed: () => digita.goBack(),
  child: const Text('Go Back'),
);

Testing Support

The package supports Flutter’s widget testing out of the box. Inject digita.navigatorKey into MaterialApp, and control navigation easily in your widget tests.

API Reference

Method Description
openPage(Widget page) Pushes a new widget-based page onto the navigation stack
closePage() Closes the current page (pops the top route)
closeAllPages() Closes all routes and returns to the root ("/")
openRoute(String routeName, {Object? arguments}) Pushes a named route onto the stack
closeRoute() Closes the current named route (alias of closePage())
replacePageWith(Widget page) Replaces the current page with a new widget-based page (no back stack)
replaceRouteWith(String routeName, {Object? arguments}) Replaces the current page with a named route (no back stack)
goBack() Alias for closePage(); used to go back to the previous screen
goBackUntil(String routeName) Pops routes until the given named route is reached
goTo(Widget page) Alias for openPage()
goToRoute(String routeName, {Object? arguments}) Alias for openRoute()
replaceWith(Widget page) Deprecated: use replacePageWith()
replaceNamed(String routeName) Deprecated: use replaceRouteWith()

Repository

License

MIT © DIGITA BANK

Libraries

digita_router