Skip to main content

easy_split_go_router 0.2.0 copy "easy_split_go_router: ^0.2.0" to clipboard
easy_split_go_router: ^0.2.0 copied to clipboard

GoRouter adapter for EasySplit: EasySplitGoRoute turns a GoRoute into a wide-screen split pane with built-in duplicate-route deduplication.

easy_split_go_router #

English | 简体中文

GoRouter adapter for EasySplit: replace a plain GoRoute with EasySplitGoRoute to get automatic side-by-side split on wide screens and full screen on narrow ones, with built-in duplicate-route deduplication. No page code changes: context.push(...) and context.pop() keep working as usual.

  • Depends only on easy_split and go_router; it does not change how you configure GoRouter.
  • Deduplication is on by default: pushing a page that is already in the stack converges to that entry and keeps its state; can be disabled globally or per route.
  • Works with go_router 13 and later (tested on 16.3.0 and 18.1.4); requires Flutter 3.27+.

Install #

dependencies:
  easy_split: ^0.2.1
  easy_split_go_router: ^0.2.0
  go_router: ">=15.0.0 <19.0.0"

Quick start #

import 'package:easy_split/easy_split.dart';
import 'package:easy_split_go_router/easy_split_go_router.dart';
import 'package:go_router/go_router.dart';

final router = GoRouter(
  initialLocation: '/',
  routes: [
    // First screen: a plain GoRoute, always full screen.
    GoRoute(path: '/', builder: (_, __) => const AdPage()),

    // Home page = left pane: wrap it in EasySplitScope explicitly, so it is
    // obvious which page is the left pane.
    GoRoute(
      path: '/home',
      builder: (_, __) => EasySplitScope(child: const HomePage()),
    ),

    // Secondary page: EasySplitGoRoute = right pane on wide, full screen on narrow.
    EasySplitGoRoute(path: '/detail', builder: (_, __) => const DetailPage()),
  ],
);

MaterialApp.router(routerConfig: router);

If the home page is not the initial route (splash/ad -> context.go('/home')), add it to the fullscreen list so it behaves as a native full-screen page and creates the panes itself:

EasySplit.config.fullscreenRoutes.add('/home');

Using pageBuilder #

EasySplitGoRoute accepts either builder or pageBuilder, exactly one of the two, just like GoRoute. Use pageBuilder when you already build a Page (or need the final GoRouterState while constructing one):

EasySplitGoRoute(
  path: '/login',
  pageBuilder: (context, state) => MaterialPage<void>(
    arguments: state.extra ??
        <String, dynamic>{
          ...state.pathParameters,
          ...state.uri.queryParameters,
        },
    child: const LoginPage(),
  ),
)

The returned page is adapted to the split-aware EasySplitPage:

  • MaterialPage / CupertinoPage: child, key, name, arguments, restorationId, maintainState, fullscreenDialog and allowSnapshotting are kept. When name / arguments are omitted they fall back to state.uri / state.extra.
  • CustomTransitionPage (including NoTransitionPage): additionally keeps transitionsBuilder, transitionDuration and reverseTransitionDuration, so custom transitions play inside the right pane.
  • EasySplitPage: returned untouched, so its own fullscreen and other pane options win.
  • Any other Page subclass: throws ArgumentError, because its child cannot be extracted. Use one of the types above, return an EasySplitPage, or switch back to builder.

The route-level fullscreen: true still applies to adapted pages, and EasySplit.config.fullscreenRoutes is matched against the route path even when the returned page sets its own name.

Duplicate-route deduplication #

On by default: "the same page" means the same name and structurally equal arguments (Map / List compared by content, up to 12 levels / 256 nodes). After /home -> /detail(a) -> /detail(b) -> /detail(a) the stack only contains [home, detail(a)]; going back returns to the home page directly and detail(b) is not pushed twice.

// Disable for a single route.
EasySplitGoRoute(path: '/detail', dedupe: false, builder: ...)

// Disable globally, or customize what counts as "the same page"
// (shared with the other easy_split integration schemes).
// The default already compares `arguments` by content; use `sameRoute` when the
// match should follow a business id instead.
EasySplit.config
  ..dedupeRepeatedRoutes = false
  ..sameRoute = (a, b) =>
      a.name == b.name && (a.arguments as Map?)?['id'] == (b.arguments as Map?)?['id'];

How it works: GoRouter handles push inside its own RouteInformationProvider, which cannot be intercepted from outside. EasySplitGoRoute therefore checks, while building the page, whether the new page is already in the stack; if so, it collapses the intermediate pages with router.pop() before the next frame. The result is the same as intercepting Navigator.pushNamed: the original page key stays and its state is not rebuilt.

Taking over deduplication with go_router 16.3+ onEnter #

If your project already uses go_router 16.3.0+, you can use onEnter to detect duplicates before the page is pushed and bring the existing page back to the top. The library provides both the check and the convergence:

GoRouter(
  redirect: myAuthGuard,
  onEnter: (context, current, next, router) {
    final settings = RouteSettings(
      name: next.matchedLocation,
      arguments: next.extra,
    );
    final config = router.routerDelegate.currentConfiguration;
    if (!easySplitIsDuplicate(config, settings)) {
      return const Allow();
    }
    // Already on top: block the push entirely.
    final action = easySplitDedupeAction(router, settings);
    if (action == null) {
      return const Block.stop();
    }
    // In the stack but not on top: allow the push, then drop the pages above
    // the existing one, so it becomes the top page again.
    return Allow(then: action);
  },
  routes: [
    // Deduplication is handled by onEnter; turn the built-in one off.
    EasySplitGoRoute(path: '/detail/:index', dedupe: false, builder: ...),
  ],
)

The three public helpers:

  • easySplitDuplicateIndexOf(configuration, settings): index of the same page in the stack (-1 if absent);
  • easySplitIsDuplicate(configuration, settings): boolean shortcut honoring EasySplit.config.dedupeRepeatedRoutes / sameRoute;
  • easySplitDedupeAction(router, settings): callback that removes the intermediate pages and brings the existing one to the top; use it with Allow(then: ...). Returns null when the page is already on top or no duplicate exists.

Notes:

  • The callback runs after the navigation is committed, so there is a short "pushed, then removed on the next frame" intermediate state. For a state with no intermediate step, use the default dedupe: true (the page builder converges at the end of the frame, with the same result).
  • Do not call pop directly inside Block.then(...): popping during go_router's resolution phase collapses the whole imperative stack back to its root.
  • onEnter is a GoRouter constructor parameter (not a GoRoute one), so the router must opt in as a whole.

A complete runnable example lives in example/lib/main_on_enter.dart (flutter run -t lib/main_on_enter.dart, requires go_router >= 16.3.0).

Route parameters #

Parameter Default Description
builder / pageBuilder one required same as GoRoute; provide exactly one of the two
name / path / routes / parentNavigatorKey - forwarded to GoRoute
fullscreen false keep this page full screen (never split), e.g. login or settings
dedupe true whether this route participates in deduplication
redirect / onExit / caseSensitive - same meaning as on GoRoute, forwarded as-is; split pages support route-level guards and leave confirmation

The appearance parameters of a pane route (maintainState, fullscreenDialog, allowSnapshotting, requestFocus) come from the internal EasySplitPage and default to the same behavior as MaterialPageRoute. Pages returned from pageBuilder keep the corresponding values they were built with. To customize them further (including transitionsBuilder / transition durations), use EasySplitPage(child: ..., name: ..., maintainState: ...) directly as the pageBuilder of a GoRoute.

Login guard (example) #

Let GoRouter's own redirect handle the authentication check, and call context.push directly from pages, with no hand-written "login first, then navigate" logic:

final router = GoRouter(
  refreshListenable: auth.currentUser, // re-parse routes when the session changes
  redirect: (context, state) {
    if (!auth.isLoggedIn && state.matchedLocation.startsWith('/detail')) {
      return '/login?from=${Uri.encodeComponent(state.uri.toString())}';
    }
    return null;
  },
  routes: [
    GoRoute(path: '/login', builder: (_, __) => const LoginPage()),
    EasySplitGoRoute(path: '/detail/:index', builder: (_, __) => const DetailPage()),
  ],
);

When login completes, use router.pushReplacement(from) to replace the login page with the target page (keeping the pages below it). Note that redirect can only return a location string, so the target parameters must be path parameters (/detail/:index) rather than extra, otherwise they are lost when the location is rewritten.

Mixed navigation (raw pushes, parameterized pages, overlays) #

go_router only manages pages that go through the route table. Anything pushed with Navigator.push(MaterialPageRoute(...)), resolved by onGenerateRoute, or inserted straight into the root Overlay is invisible to EasySplit - on a wide screen it covers the whole window. Three patterns keep those entry points under control:

  1. Audit in debug builds. Register EasySplit.auditObserver; every route EasySplit cannot pane (bare PageRoute, not EasySplitPage, not in EasySplit.fullscreenRoutes) logs one warning with its name and type, so leaks show up while you navigate instead of in QA:

    final router = GoRouter(
      routes: [...],
      observers: [EasySplit.auditObserver],
    );
    
  2. One parameterized route instead of N copies. matchedLocation contains no query string, and dedupe compares route name + arguments, so pass the payload in extra:

    class WebViewArgs {
      const WebViewArgs({required this.url, this.headers = const {}, this.fullscreen = false});
      final String url;
      final Map<String, String> headers;
      final bool fullscreen;
      // value equality is what makes "same url + same headers" dedupe work
      @override
      bool operator ==(Object other) => ...;
      @override
      int get hashCode => ...;
    }
    
    EasySplitGoRoute(
      path: '/webview',
      pageBuilder: (context, state) => EasySplitPage(
        fullscreen: WebViewArgs.fromExtra(state.extra).fullscreen,
        child: (_) => WebViewPage(args: WebViewArgs.fromExtra(state.extra)),
      ),
    )
    

    Call sites then share one helper (openWebView) and cannot forget the fullscreen flag.

  3. Overlays per pane. EasySplit.showPaneOverlay(context, side: ..., builder: ...) returns an OverlayEntry already constrained to the left/right pane and re-laid out on resize or fold; EasySplit.paneOverlayEntry does the same without inserting. Attach overlays that must sit between pages (e.g. under the "second floor" page) to that page's own Stack instead.

example/lib/main_mixed.dart runs all three (flutter run -t lib/main_mixed.dart), and test/mixed_navigation_test.dart asserts the pane rect, the dedupe and the overlay geometry.

Known limitations #

  • Pages inside ShellRoute / StatefulShellRoute do not participate in deduplication yet and keep their native behavior.
  • Deduplication only handles pushing a new page; context.go(...) (whole-stack replacement) and context.replace(...) keep GoRouter's native semantics.

Examples and tests #

cd example
flutter run                          # ad -> home -> detail -> dialogs
flutter run -t lib/main_mixed.dart   # mixed navigation: parameterized /webview + overlays + audit

example/lib/main.dart is the full ad page -> home -> detail -> dialogs demo. The login guard uses GoRouter's redirect + refreshListenable: unauthenticated access to /detail/:index, /secondary or /dialogs is redirected to /login?from=... and, after a successful login, the user lands back on the original target (see _guard in example/lib/main.dart and DemoAuth / LoginPage._finish in pages.dart). The "split settings" card switches between side-by-side, content-on-the-left, stacked and floating layouts and adjusts the pane ratio at runtime (via EasySplit.updateLayout(), independent of the routing scheme); test/go_route_dedupe_test.dart covers deduplication on wide and narrow screens, the global switch, a custom sameRoute and per-route opt-out; example/test/on_enter_dedupe_test.dart covers the onEnter interception variant; example/test/mixed_navigation_test.dart covers the mixed-navigation entry (pane rect, content-based dedupe, pane-constrained overlays).

License #

MIT

☕ Buy Me a Coffee #

If easy_split_go_router has been helpful to you, you are welcome to buy me a coffee. Thank you for your support!

Alipay WeChat
Alipay QR code WeChat QR code
1
likes
160
points
549
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

GoRouter adapter for EasySplit: EasySplitGoRoute turns a GoRoute into a wide-screen split pane with built-in duplicate-route deduplication.

Repository (GitHub)
View/report issues

Topics

#go-router #split-screen #foldable #adaptive-layout

License

MIT (license)

Dependencies

easy_split, flutter, go_router

More

Packages that depend on easy_split_go_router