-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
desktop_tablet_scaffold.dart
167 lines (153 loc) · 4.98 KB
/
desktop_tablet_scaffold.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:shiori/application/bloc.dart';
import 'package:shiori/generated/l10n.dart';
import 'package:shiori/presentation/artifacts/artifacts_page.dart';
import 'package:shiori/presentation/characters/characters_page.dart';
import 'package:shiori/presentation/home/home_page.dart';
import 'package:shiori/presentation/map/map_page.dart';
import 'package:shiori/presentation/settings/settings_page.dart';
import 'package:shiori/presentation/shared/extensions/focus_scope_node_extensions.dart';
import 'package:shiori/presentation/shared/shiori_icons.dart';
import 'package:shiori/presentation/weapons/weapons_page.dart';
typedef OnWillPop = Future<bool> Function();
class DesktopTabletScaffold extends StatelessWidget {
final int defaultIndex;
final TabController tabController;
const DesktopTabletScaffold({
Key? key,
required this.defaultIndex,
required this.tabController,
}) : super(key: key);
@override
Widget build(BuildContext context) {
final extended = MediaQuery.of(context).orientation != Orientation.portrait;
return Scaffold(
body: SafeArea(
child: Row(
children: [
_NavigationRail(defaultIndex: defaultIndex + 1, extended: extended, tabController: tabController),
const VerticalDivider(thickness: 1, width: 1),
// This is the main content.
Expanded(
child: TabBarView(
controller: tabController,
physics: const NeverScrollableScrollPhysics(),
children: [
const CharactersPage(),
const WeaponsPage(),
HomePage(),
const ArtifactsPage(),
MapPage(),
],
),
),
],
),
),
);
}
}
class _NavigationRail extends StatefulWidget {
final int defaultIndex;
final bool extended;
final TabController tabController;
const _NavigationRail({
Key? key,
required this.defaultIndex,
required this.extended,
required this.tabController,
}) : super(key: key);
@override
State<_NavigationRail> createState() => _NavigationRailState();
}
class _NavigationRailState extends State<_NavigationRail> {
late int _index;
late bool _extended;
@override
void initState() {
_index = widget.defaultIndex;
_extended = widget.extended;
super.initState();
}
@override
Widget build(BuildContext context) {
final s = S.of(context);
return BlocListener<MainTabBloc, MainTabState>(
listener: (ctx, state) async {
state.maybeMap(
initial: (s) => _changeCurrentTab(s.currentSelectedTab),
orElse: () => {},
);
},
child: NavigationRail(
selectedIndex: _index,
onDestinationSelected: (index) => _gotoTab(index),
labelType: _extended ? null : NavigationRailLabelType.selected,
extended: _extended,
destinations: <NavigationRailDestination>[
NavigationRailDestination(
icon: const Icon(Icons.menu),
selectedIcon: const Icon(Icons.menu),
label: Text(s.collapse),
),
NavigationRailDestination(
icon: const Icon(Icons.people),
selectedIcon: const Icon(Icons.people),
label: Text(s.characters),
),
NavigationRailDestination(
icon: const Icon(Shiori.crossed_swords),
selectedIcon: const Icon(Shiori.crossed_swords),
label: Text(s.weapons),
),
NavigationRailDestination(
icon: const Icon(Icons.home),
selectedIcon: const Icon(Icons.home),
label: Text(s.home),
),
NavigationRailDestination(
icon: const Icon(Shiori.overmind),
selectedIcon: const Icon(Shiori.overmind),
label: Text(s.artifacts),
),
NavigationRailDestination(
icon: const Icon(Icons.map),
selectedIcon: const Icon(Icons.map),
label: Text(s.map),
),
NavigationRailDestination(
icon: const Icon(Icons.settings),
selectedIcon: const Icon(Icons.settings),
label: Text(s.settings),
),
],
),
);
}
void _changeCurrentTab(int index) {
FocusScope.of(context).removeFocus();
widget.tabController.index = index;
setState(() {
_index = index + 1;
});
}
Future<void> _gotoTab(int newIndex) async {
if (newIndex == 0) {
setState(() {
_extended = !_extended;
});
return;
}
final realIndex = newIndex - 1;
if (realIndex == 5) {
await _gotoSettingsPage();
return;
}
context.read<MainTabBloc>().add(MainTabEvent.goToTab(index: realIndex));
}
Future<void> _gotoSettingsPage() async {
final route = MaterialPageRoute(builder: (c) => SettingsPage());
await Navigator.push(context, route);
}
}