Skip to content

Commit

Permalink
Merge pull request #266 from yumemi-inc/feature/navigation_debug
Browse files Browse the repository at this point in the history
デバッグ時に、任意の画面にすぐ遷移できるようにする
  • Loading branch information
K9i-0 committed Jun 20, 2024
2 parents abd19e0 + de7b983 commit af60baf
Show file tree
Hide file tree
Showing 11 changed files with 253 additions and 1 deletion.
26 changes: 26 additions & 0 deletions apps/app/lib/router/provider/router.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 30 additions & 1 deletion apps/app/lib/router/routes/main/home/debug_page_route.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
part of 'package:flutter_app/router/provider/router.dart';

final class _DebugPageNavigatorImpl implements DebugPageNavigator {
const _DebugPageNavigatorImpl();

@override
void goNavigationDebugPage(BuildContext context) {
const NavigationDebugPageRoute().go(context);
}
}

class DebugPageRoute extends GoRouteData {
const DebugPageRoute();

Expand All @@ -9,6 +18,26 @@ class DebugPageRoute extends GoRouteData {

@override
Widget build(BuildContext context, GoRouterState state) {
return const DebugPage();
return ProviderScope(
overrides: [
debugPageNavigatorProvider.overrideWithValue(
const _DebugPageNavigatorImpl(),
),
],
child: const DebugPage(),
);
}
}

class NavigationDebugPageRoute extends GoRouteData {
const NavigationDebugPageRoute();

static const path = 'navigation_debug';

static final $parentNavigatorKey = _rootNavigatorKey;

@override
Widget build(BuildContext context, GoRouterState state) {
return const NavigationDebugPage();
}
}
5 changes: 5 additions & 0 deletions apps/app/lib/router/routes/main/home/home_shell_branch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ const homeShellBranch = TypedStatefulShellBranch<HomeShellBranch>(
),
TypedGoRoute<DebugPageRoute>(
path: DebugPageRoute.path,
routes: [
TypedGoRoute<NavigationDebugPageRoute>(
path: NavigationDebugPageRoute.path,
),
],
),
TypedGoRoute<WebPageRoute>(
path: WebPageRoute.path,
Expand Down
1 change: 1 addition & 0 deletions melos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ command:
intl: ^0.19.0
json_annotation: ^4.8.1
riverpod_annotation: ^2.3.5
go_router: ^13.0.1
dev_dependencies:
build_runner: ^2.4.7
custom_lint: ^0.6.4
Expand Down
11 changes: 11 additions & 0 deletions packages/features/debug_mode/lib/src/ui/debug_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ import 'dart:async';

import 'package:cores_core/app_status.dart';
import 'package:features_debug_mode/src/data/api/provider/exception_generator_api.dart';
import 'package:features_debug_mode/src/ui/provider/navigator_provider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

abstract interface class DebugPageNavigator {
void goNavigationDebugPage(BuildContext context);
}

class DebugPage extends ConsumerWidget {
const DebugPage({super.key});

Expand Down Expand Up @@ -42,6 +47,12 @@ class DebugPage extends ConsumerWidget {
);
},
),
_FixSizedElevatedButton(
title: 'Go navigation debug page',
onPressed: () => ref
.read(debugPageNavigatorProvider)
.goNavigationDebugPage(context),
),
],
),
),
Expand Down
123 changes: 123 additions & 0 deletions packages/features/debug_mode/lib/src/ui/navigation_debug_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';

class NavigationDebugPage extends ConsumerWidget {
const NavigationDebugPage({super.key});

@override
Widget build(BuildContext context, WidgetRef ref) {
return const Scaffold(
body: CustomScrollView(
slivers: [
SliverAppBar.large(
title: Text('Debug Page'),
),
SliverToBoxAdapter(
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 16),
child: RouteDropdownMenu(),
),
),
],
),
);
}
}

final class RouteDropdownMenu extends HookWidget {
const RouteDropdownMenu({super.key});

@override
Widget build(BuildContext context) {
final router = GoRouter.of(context);
final routeBases = router.configuration.routes;
final dropdownMenuEntries = useMemoized(
() {
final routes = routeBases.toRoutes();
return routes
.map(
(route) {
final routePath = route.path;

// デバッグ関連のルートは除外する
if (routePath.contains('debug')) {
return null;
}
return DropdownMenuEntry<_Route>(
value: route,
label: routePath,
);
},
)
.nonNulls
.toList();
},
routeBases,
);
// DropdownMenu を親の横幅に合わせる
return LayoutBuilder(
builder: (context, constraints) {
return DropdownMenu<_Route>(
width: constraints.maxWidth,
dropdownMenuEntries: dropdownMenuEntries,
// hintText: l.routeDropDownHintText,
onSelected: (route) {
if (route == null) {
return;
}
router.go(route.path);
},
);
},
);
}
}

extension _ToRoutes on List<RouteBase> {
List<_Route> toRoutes([_Route? parentRoute]) {
final routes = <_Route>[];
for (final routeBase in this) {
switch (routeBase) {
case GoRoute():
final route = _Route(
goRoute: routeBase,
parentRoute: parentRoute,
);
routes.add(route);

final childRouteBases = routeBase.routes;
if (childRouteBases.isNotEmpty) {
routes.addAll(childRouteBases.toRoutes(route));
}
case ShellRoute() || StatefulShellRoute():
routes.addAll(routeBase.routes.toRoutes());
}
}
return routes;
}
}

final class _Route {
_Route({
required GoRoute goRoute,
_Route? parentRoute,
}) : _goRoute = goRoute,
_parentRoute = parentRoute;

final GoRoute _goRoute;
final _Route? _parentRoute;

late final String path = _path;

String get _path {
final parentPath = _parentRoute?._path;
final childPath = _goRoute.path;
if (parentPath == null) {
return childPath;
}

return '${parentPath == '/' ? '' : parentPath}/$childPath';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import 'package:features_debug_mode/ui.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'navigator_provider.g.dart';

@Riverpod(dependencies: [])
DebugPageNavigator debugPageNavigator(DebugPageNavigatorRef ref) =>
throw UnimplementedError();

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/features/debug_mode/lib/ui.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export 'src/ui/debug_page.dart';
export 'src/ui/maintenance_page.dart';
export 'src/ui/navigation_debug_page.dart';
export 'src/ui/provider/navigator_provider.dart';
16 changes: 16 additions & 0 deletions packages/features/debug_mode/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,14 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_hooks:
dependency: "direct main"
description:
name: flutter_hooks
sha256: cde36b12f7188c85286fba9b38cc5a902e7279f36dd676967106c041dc9dde70
url: "https://pub.dev"
source: hosted
version: "0.20.5"
flutter_riverpod:
dependency: "direct main"
description:
Expand Down Expand Up @@ -304,6 +312,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.2"
go_router:
dependency: "direct main"
description:
name: go_router
sha256: b465e99ce64ba75e61c8c0ce3d87b66d8ac07f0b35d0a7e0263fcfc10f99e836
url: "https://pub.dev"
source: hosted
version: "13.2.5"
graphs:
dependency: transitive
description:
Expand Down
2 changes: 2 additions & 0 deletions packages/features/debug_mode/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ dependencies:
dio: ^5.4.0
flutter:
sdk: flutter
flutter_hooks: ^0.20.5
flutter_riverpod: ^2.5.1
go_router: ^13.0.1
riverpod_annotation: ^2.3.5

dev_dependencies:
Expand Down

0 comments on commit af60baf

Please sign in to comment.