From d2893ff2391a4d31c87c859b544f7d41f40d80f3 Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Mon, 26 Jun 2023 18:29:02 +0200 Subject: [PATCH] neon: enable apps to specify go_routes --- .../lib/src/models/app_implementation.dart | 29 +++++++++++++++++++ .../neon/neon/lib/src/utils/app_route.dart | 16 ++++++++++ packages/neon/neon/lib/utils.dart | 1 + 3 files changed, 46 insertions(+) create mode 100644 packages/neon/neon/lib/src/utils/app_route.dart diff --git a/packages/neon/neon/lib/src/models/app_implementation.dart b/packages/neon/neon/lib/src/models/app_implementation.dart index 750c7b7f..b4224fec 100644 --- a/packages/neon/neon/lib/src/models/app_implementation.dart +++ b/packages/neon/neon/lib/src/models/app_implementation.dart @@ -1,6 +1,7 @@ import 'package:collection/collection.dart'; import 'package:flutter/material.dart'; import 'package:flutter_svg/flutter_svg.dart'; +import 'package:go_router/go_router.dart'; import 'package:neon/l10n/localizations.dart'; import 'package:neon/src/bloc/bloc.dart'; import 'package:neon/src/blocs/accounts.dart'; @@ -68,6 +69,34 @@ abstract class AppImplementation ); } + /// Main branch displayed in the home page. + /// + /// There's usually no need to override this. + StatefulShellBranch get mainBranch => StatefulShellBranch( + routes: [ + route, + ], + ); + + /// Route for the app. + /// + /// All pages of the app must be specified as subroutes. + /// If this is not [GoRoute] an inital route name must be specified by overriding [initialRouteName]. + RouteBase get route => throw UnimplementedError(); + + /// Name of the initial route for this app. + /// + /// Subclasses that don't provide a [GoRoute] for [route] must override this. + String get initialRouteName { + final route = this.route; + + if (route is GoRoute && route.name != null) { + return route.name!; + } + + throw FlutterError('No name for the initial route provided.'); + } + Widget buildIcon({ final Size size = const Size.square(32), final Color? color, diff --git a/packages/neon/neon/lib/src/utils/app_route.dart b/packages/neon/neon/lib/src/utils/app_route.dart new file mode 100644 index 00000000..459b5edd --- /dev/null +++ b/packages/neon/neon/lib/src/utils/app_route.dart @@ -0,0 +1,16 @@ +import 'package:flutter/widgets.dart'; +import 'package:go_router/go_router.dart'; + +/// [RouteData] for the initial page of an app. +/// +/// Subclasses must override one of [build] or [redirect]. +abstract class NeonAppRoute extends GoRouteData { + const NeonAppRoute(); + + @override + Page buildPage(final BuildContext context, final GoRouterState state) => NoTransitionPage( + child: build(context, state), + ); +} + +const appsRoutePrefix = '/apps/'; diff --git a/packages/neon/neon/lib/utils.dart b/packages/neon/neon/lib/utils.dart index a51944c8..6f1522a1 100644 --- a/packages/neon/neon/lib/utils.dart +++ b/packages/neon/neon/lib/utils.dart @@ -1,3 +1,4 @@ +export 'package:neon/src/utils/app_route.dart'; export 'package:neon/src/utils/confirmation_dialog.dart'; export 'package:neon/src/utils/exceptions.dart'; export 'package:neon/src/utils/hex_color.dart';