spring_boot_starter_flutter_navigation

spring_boot_starter_flutter_navigation puts the screens of a Flutter app on routes. You mark a widget @ScreenRoute('/catalog/books/{isbn}'), and the starter opens it at that address, on the SDK's own Router: the browser's address bar, deep links and Android's back button all work, without go_router. Paths use Spring's patterns, the ones of @GetMapping in the web starter.

Why

Navigation in Flutter is either imperative (Navigator.push, no addresses, no deep links) or a routing package whose tables are kept by hand next to the screens. Here a screen declares its own address, the generator checks every route and writes typed constants for it, and the rest is configuration:

Setup

# pubspec.yaml
dependencies:
  spring_boot_starter_flutter_navigation: ^0.9.0

dev_dependencies:
  build_runner: ^2.17.0
  spring_core_dart_generator: ^0.9.0

The starter brings and re-exports spring_boot_starter_flutter; an app lists only this package (spring_boot_starter_flutter_security brings it too). One import is enough:

import 'package:spring_boot_starter_flutter_navigation/spring_boot_starter_flutter_navigation.dart';

The router imports neither Material nor Cupertino, so an app of spring.flutter.app.type: widgets (the type without a design starter) has no design library in its build. An app that shows Material widgets, as the example below does, also lists spring_boot_starter_flutter_material and material_ui, and imports package:material_ui/material_ui.dart for the widgets:

# pubspec.yaml
dependencies:
  material_ui: ^1.11.0
  spring_boot_starter_flutter_material: ^0.9.0
  spring_boot_starter_flutter_navigation: ^0.9.0

A Cupertino app lists spring_boot_starter_flutter_cupertino and cupertino_ui instead. The starter needs Flutter 3.50 or later (Dart 3.14). See Design libraries.

Example

An app with two tabs, a book screen with a path variable, a parameter and an extra object, and a not-found screen:

// lib/main.dart
import 'package:material_ui/material_ui.dart';
import 'package:spring_boot_starter_flutter_navigation/spring_boot_starter_flutter_navigation.dart';

import 'spring_gen/main.spring.dart';

// No home: the app widget is built on the router.
@SpringBootApplication()
void main() => runApp(
  SpringApplicationWidget(application: () => springApplication),
);

class BookSummary {
  const BookSummary(this.title);
  final String title;
}

// The tabs; each branch keeps its own stack of screens.
@RouteShell(branches: ['/catalog', '/profile'])
class MainTabs extends StatelessWidget {
  const MainTabs(this.shell, {super.key});
  final ShellState shell;

  @override
  Widget build(BuildContext context) => Scaffold(
    body: shell.child,
    bottomNavigationBar: NavigationBar(
      selectedIndex: shell.index,
      onDestinationSelected: shell.select,
      destinations: const [
        NavigationDestination(icon: Icon(Icons.book), label: 'Catalog'),
        NavigationDestination(icon: Icon(Icons.person), label: 'Profile'),
      ],
    ),
  );
}

@ScreenRoute('/catalog', title: 'Catalog')
class CatalogPage extends StatelessWidget {
  const CatalogPage({super.key});

  @override
  Widget build(BuildContext context) => ListView(
    children: [
      ListTile(
        title: const Text('Dune'),
        // A generated, typed address: a renamed parameter does not compile.
        onTap: () => context.route.go(
          BookPageRoute.to(isbn: '978-0441013593', page: 1, extra: const BookSummary('Dune')),
        ),
      ),
    ],
  );
}

@ScreenRoute('/catalog/books/{isbn}', title: 'Book', params: {'page': int}, extra: BookSummary)
class BookPage extends StatelessWidget {
  const BookPage({super.key});

  @override
  Widget build(BuildContext context) {
    final route = context.route;
    final isbn = route.get(BookPageRoute.isbn);        // String
    final page = route.get(BookPageRoute.page) ?? 1;   // int?
    final summary = route.get(BookPageRoute.extra);    // BookSummary?
    return Text('${summary?.title ?? isbn}, page $page');
  }
}

@ScreenRoute('/profile', title: 'Profile')
class ProfilePage extends StatelessWidget {
  const ProfilePage({super.key});

  @override
  Widget build(BuildContext context) => const Text('Profile');
}

@ScreenRoute.notFound()
class NotFoundPage extends StatelessWidget {
  const NotFoundPage({super.key});

  @override
  Widget build(BuildContext context) => Text('Nothing at ${context.route.uri}');
}
# lib/resources/application.yml
spring:
  flutter:
    # A MaterialApp: spring_boot_starter_flutter_material, the one design
    # starter, decides the app's type (spring.flutter.app.type not needed)
    navigation:
      initial: /catalog

Running it

dart run build_runner build   # registers the screens, writes BookPageRoute and the others
flutter run -d chrome

What is inside

In Spring (Java)

Spring has no navigation for apps. The names follow Spring where there is something to follow: path patterns (@GetMapping's), RouterFunctions (Spring WebFlux), *Template for the API, scopes for beans, @PreAuthorize for guards.

See also