Skip to content

Commit 51adfd0

Browse files
committed
Refactor: Use navigation shell for game routes
Refactor the game route to use a navigation shell. The game route now redirects to the "/game/action" route by default. Create new pages for backpack, action, and craft. Update the GameIndexPage to use the AdaptiveNavigationScaffold.
1 parent 24b5035 commit 51adfd0

File tree

5 files changed

+98
-149
lines changed

5 files changed

+98
-149
lines changed

escape_wild/lib/route.dart

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import 'package:escape_wild/ui/game/action/action.dart';
2+
import 'package:escape_wild/ui/game/backpack/backpack.dart';
3+
import 'package:escape_wild/ui/game/craft/craft.dart';
14
import 'package:escape_wild/ui/game/index.dart';
25
import 'package:escape_wild/ui/main/game.dart';
36
import 'package:escape_wild/ui/main/index.dart';
@@ -7,6 +10,9 @@ import 'package:go_router/go_router.dart';
710

811
final _$mainGame = GlobalKey<NavigatorState>();
912
final _$mainMine = GlobalKey<NavigatorState>();
13+
final _$gameAction = GlobalKey<NavigatorState>();
14+
final _$gameBackpack = GlobalKey<NavigatorState>();
15+
final _$gameCraft = GlobalKey<NavigatorState>();
1016

1117
RoutingConfig buildRoutingConfig() {
1218
return RoutingConfig(
@@ -51,38 +57,46 @@ RoutingConfig buildRoutingConfig() {
5157
),
5258
GoRoute(
5359
path: "/game",
54-
builder: (ctx, state) => const GameIndexPage(),
55-
// redirect: (ctx, state) {
56-
// if (state.fullPath == "/game") return "/game/action";
57-
// return null;
58-
// },
59-
// routes: [
60-
// StatefulShellRoute.indexedStack(
61-
// builder: (ctx, state, navigationShell) {
62-
// return GameIndexPage(navigationShell: navigationShell);
63-
// },
64-
// branches: [
65-
// StatefulShellBranch(
66-
// navigatorKey: _$mainGame,
67-
// routes: [
68-
// GoRoute(
69-
// path: "/action",
70-
// builder: (ctx, state) => const GamePage(),
71-
// ),
72-
// ],
73-
// ),
74-
// StatefulShellBranch(
75-
// navigatorKey: _$mainMine,
76-
// routes: [
77-
// GoRoute(
78-
// path: "/mine",
79-
// builder: (ctx, state) => const MinePage(),
80-
// ),
81-
// ],
82-
// ),
83-
// ],
84-
// ),
85-
// ],
60+
redirect: (ctx, state) {
61+
if (state.fullPath == "/game") return "/game/action";
62+
return null;
63+
},
64+
routes: [
65+
StatefulShellRoute.indexedStack(
66+
builder: (ctx, state, navigationShell) {
67+
return GameIndexPage(navigationShell: navigationShell);
68+
},
69+
branches: [
70+
StatefulShellBranch(
71+
navigatorKey: _$gameAction,
72+
routes: [
73+
GoRoute(
74+
path: "/action",
75+
builder: (ctx, state) => const GameActionPage(),
76+
),
77+
],
78+
),
79+
StatefulShellBranch(
80+
navigatorKey: _$gameBackpack,
81+
routes: [
82+
GoRoute(
83+
path: "/backpack",
84+
builder: (ctx, state) => const GameBackpackPage(),
85+
),
86+
],
87+
),
88+
StatefulShellBranch(
89+
navigatorKey: _$gameCraft,
90+
routes: [
91+
GoRoute(
92+
path: "/craft",
93+
builder: (c, state) => const GameCraftPage(),
94+
),
95+
],
96+
),
97+
],
98+
),
99+
],
86100
),
87101
],
88102
);

escape_wild/lib/ui/game/action/action.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ import 'package:rettulf/rettulf.dart';
88

99
import 'hud.dart';
1010

11-
class ActionPage extends StatefulWidget {
12-
const ActionPage({
11+
class GameActionPage extends StatefulWidget {
12+
const GameActionPage({
1313
super.key,
1414
});
1515

1616
@override
17-
State<ActionPage> createState() => _ActionPageState();
17+
State<GameActionPage> createState() => _GameActionPageState();
1818
}
1919

20-
class _ActionPageState extends State<ActionPage> {
20+
class _GameActionPageState extends State<GameActionPage> {
2121
@override
2222
Widget build(BuildContext context) {
2323
final actions = player.getAvailableActions();

escape_wild/lib/ui/game/backpack/backpack.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ part 'backpack.i18n.dart';
1313

1414
String get backpackTitle => _I.title;
1515

16-
class BackpackPage extends StatefulWidget {
17-
const BackpackPage({super.key});
16+
class GameBackpackPage extends StatefulWidget {
17+
const GameBackpackPage({super.key});
1818

1919
@override
20-
State<BackpackPage> createState() => _BackpackPageState();
20+
State<GameBackpackPage> createState() => _GameBackpackPageState();
2121
}
2222

2323
Widget buildEmptyBackpack() {
@@ -27,7 +27,7 @@ Widget buildEmptyBackpack() {
2727
);
2828
}
2929

30-
class _BackpackPageState extends State<BackpackPage> {
30+
class _GameBackpackPageState extends State<GameBackpackPage> {
3131
int selectedIndex = 0;
3232

3333
ItemStack get selected => player.backpack[selectedIndex];

escape_wild/lib/ui/game/craft/craft.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import 'package:rettulf/rettulf.dart';
99
import '../backpack/backpack.dart';
1010
import '../shared.dart';
1111

12-
class CraftPage extends StatefulWidget {
13-
const CraftPage({super.key});
12+
class GameCraftPage extends StatefulWidget {
13+
const GameCraftPage({super.key});
1414

1515
@override
16-
State<CraftPage> createState() => _CraftPageState();
16+
State<GameCraftPage> createState() => _GameCraftPageState();
1717
}
1818

19-
class _CraftPageState extends State<CraftPage> {
19+
class _GameCraftPageState extends State<GameCraftPage> {
2020
int _selectedCatIndex = 0;
2121

2222
int get selectedCatIndex => _selectedCatIndex;

escape_wild/lib/ui/game/index.dart

Lines changed: 40 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,28 @@
11
import 'package:easy_localization/easy_localization.dart';
22
import 'package:escape_wild/core/index.dart';
3+
import 'package:escape_wild/design/adaptive_navigation.dart';
34
import 'package:escape_wild/foundation.dart';
45
import 'package:escape_wild/game/serialization.dart';
56
import 'package:escape_wild/generated/icons.dart';
6-
import 'package:escape_wild/ui/game/backpack/backpack.dart';
77
import 'package:flutter/material.dart';
8-
import 'package:flutter/services.dart';
98
import 'package:go_router/go_router.dart';
109
import 'package:rettulf/rettulf.dart';
11-
import 'action/action.dart';
12-
import 'camp/camp.dart';
13-
import 'craft/craft.dart';
1410

1511
part 'index.i18n.dart';
1612

1713
class GameIndexPage extends StatefulWidget {
18-
const GameIndexPage({super.key});
14+
final StatefulNavigationShell navigationShell;
15+
16+
const GameIndexPage({
17+
super.key,
18+
required this.navigationShell,
19+
});
1920

2021
@override
2122
State<GameIndexPage> createState() => _HomePageState();
2223
}
2324

24-
class _P {
25-
_P._();
26-
27-
static const action = 0;
28-
static const backpack = 1;
29-
static const craft = 2;
30-
static const camp = 3;
31-
static const pageCount = 4;
32-
}
33-
3425
class _HomePageState extends State<GameIndexPage> {
35-
var curIndex = _P.action;
36-
3726
@override
3827
Widget build(BuildContext context) {
3928
return PopScope(
@@ -57,26 +46,43 @@ class _HomePageState extends State<GameIndexPage> {
5746
context.pop();
5847
}
5948
},
60-
child: KeyboardListener(
61-
focusNode: focusNode,
62-
autofocus: true,
63-
onKeyEvent: onKeyEvent,
64-
child: player >>
65-
(_) => [
66-
buildMain(),
67-
buildEnvColorCover(),
68-
].stack(),
69-
),
49+
child: player >>
50+
(_) => [
51+
buildMain(),
52+
buildEnvColorCover(),
53+
].stack(),
7054
);
7155
}
7256

7357
Widget buildMain() {
74-
return Scaffold(
75-
body: AnimatedSwitcher(
76-
duration: const Duration(milliseconds: 200),
77-
child: buildBody(),
78-
),
79-
bottomNavigationBar: buildBottom(),
58+
return AdaptiveNavigationScaffold(
59+
navigationShell: widget.navigationShell,
60+
items: [
61+
(
62+
route: "/action",
63+
icon: Icons.grid_view_outlined,
64+
activeIcon: Icons.grid_view_sharp,
65+
label: _I.action,
66+
),
67+
(
68+
route: "/backpack",
69+
icon: Icons.backpack_outlined,
70+
activeIcon: Icons.backpack,
71+
label: _I.backpack,
72+
),
73+
(
74+
route: "/craft",
75+
icon: Icons.build_outlined,
76+
activeIcon: Icons.build,
77+
label: _I.craft,
78+
),
79+
(
80+
route: "/camp",
81+
icon: IconsX.camping_outlined,
82+
activeIcon: IconsX.camping,
83+
label: _I.camp,
84+
),
85+
],
8086
);
8187
}
8288

@@ -97,75 +103,4 @@ class _HomePageState extends State<GameIndexPage> {
97103
duration: const Duration(milliseconds: 100),
98104
);
99105
}
100-
101-
Widget buildBottom() {
102-
return BottomNavigationBar(
103-
type: BottomNavigationBarType.fixed,
104-
landscapeLayout: BottomNavigationBarLandscapeLayout.centered,
105-
currentIndex: curIndex,
106-
onTap: (newIndex) {
107-
if (newIndex != curIndex) {
108-
setState(() {
109-
curIndex = newIndex;
110-
});
111-
}
112-
},
113-
items: [
114-
BottomNavigationBarItem(
115-
label: _I.action,
116-
icon: const Icon(Icons.grid_view_outlined),
117-
activeIcon: const Icon(Icons.grid_view_sharp),
118-
),
119-
BottomNavigationBarItem(
120-
label: _I.backpack,
121-
icon: const Icon(Icons.backpack_outlined),
122-
activeIcon: const Icon(Icons.backpack),
123-
),
124-
BottomNavigationBarItem(
125-
label: _I.craft,
126-
icon: const Icon(Icons.build_outlined),
127-
activeIcon: const Icon(Icons.build),
128-
),
129-
BottomNavigationBarItem(
130-
label: _I.camp,
131-
icon: const Icon(IconsX.camping_outlined),
132-
activeIcon: const Icon(IconsX.camping),
133-
),
134-
],
135-
);
136-
}
137-
138-
final focusNode = FocusNode();
139-
140-
Widget buildBody() {
141-
if (curIndex == _P.action) {
142-
return ActionPage();
143-
} else if (curIndex == _P.backpack) {
144-
return BackpackPage();
145-
} else if (curIndex == _P.craft) {
146-
return CraftPage();
147-
} else {
148-
return CampPage();
149-
}
150-
}
151-
152-
void onKeyEvent(KeyEvent k) {
153-
if (k is KeyDownEvent) {
154-
if (k.character == "z") {
155-
setState(() {
156-
curIndex = (curIndex - 1) % _P.pageCount;
157-
});
158-
} else if (k.character == "x") {
159-
setState(() {
160-
curIndex = (curIndex + 1) % _P.pageCount;
161-
});
162-
}
163-
}
164-
}
165-
166-
@override
167-
void dispose() {
168-
focusNode.dispose();
169-
super.dispose();
170-
}
171106
}

0 commit comments

Comments
 (0)