-
Notifications
You must be signed in to change notification settings - Fork 38
/
yaru_landscape_layout.dart
163 lines (148 loc) · 4.85 KB
/
yaru_landscape_layout.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
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:yaru/yaru.dart';
import 'yaru_master_list_view.dart';
class YaruLandscapeLayout extends StatefulWidget {
const YaruLandscapeLayout({
super.key,
required this.navigatorKey,
this.navigatorObservers = const <NavigatorObserver>[],
this.initialRoute,
this.onGenerateRoute,
this.onUnknownRoute,
required this.tileBuilder,
required this.pageBuilder,
this.onSelected,
required this.paneLayoutDelegate,
this.appBar,
this.bottomBar,
required this.controller,
});
final GlobalKey<NavigatorState> navigatorKey;
final List<NavigatorObserver> navigatorObservers;
final String? initialRoute;
final RouteFactory? onGenerateRoute;
final RouteFactory? onUnknownRoute;
final YaruMasterTileBuilder tileBuilder;
final IndexedWidgetBuilder pageBuilder;
final ValueChanged<int>? onSelected;
final YaruPanedViewLayoutDelegate paneLayoutDelegate;
final Widget? appBar;
final Widget? bottomBar;
final YaruPageController controller;
@override
State<YaruLandscapeLayout> createState() => _YaruLandscapeLayoutState();
}
class _YaruLandscapeLayoutState extends State<YaruLandscapeLayout> {
late int _selectedIndex;
double? _paneWidth;
@override
void initState() {
super.initState();
assert(widget.paneLayoutDelegate.paneSide.isHorizontal);
widget.controller.addListener(_controllerCallback);
_selectedIndex = max(widget.controller.index, 0);
}
@override
void dispose() {
widget.controller.removeListener(_controllerCallback);
super.dispose();
}
@override
void didUpdateWidget(covariant YaruLandscapeLayout oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.controller != oldWidget.controller) {
oldWidget.controller.removeListener(_controllerCallback);
widget.controller.addListener(_controllerCallback);
_selectedIndex = max(widget.controller.index, 0);
}
}
void _controllerCallback() {
if (widget.controller.index != _selectedIndex) {
setState(() => _selectedIndex = max(widget.controller.index, 0));
}
}
void _onTap(int index) {
widget.controller.index = index;
widget.onSelected?.call(_selectedIndex);
widget.navigatorKey.currentState?.popUntil((route) => route.isFirst);
}
@override
Widget build(BuildContext context) {
final theme = YaruMasterDetailTheme.of(context);
return YaruPanedView(
pane: _buildLeftPane(theme),
page: _buildPage(context),
layoutDelegate: widget.paneLayoutDelegate,
includeSeparator: theme.includeSeparator ?? true,
onPaneSizeChange: (size) => _paneWidth = size,
);
}
Widget _buildLeftPane(YaruMasterDetailThemeData theme) {
return Builder(
builder: (context) {
return YaruTitleBarTheme(
data: const YaruTitleBarThemeData(
style: YaruTitleBarStyle.undecorated,
),
child: Column(
children: [
if (widget.appBar != null)
SizedBox(
height: kYaruTitleBarHeight,
child: widget.appBar!,
),
Expanded(
child: Container(
color: theme.sideBarColor,
child: YaruMasterListView(
length: widget.controller.length,
selectedIndex: _selectedIndex,
onTap: _onTap,
builder: widget.tileBuilder,
availableWidth: _paneWidth!,
startUndershoot: widget.appBar != null,
endUndershoot: widget.bottomBar != null,
),
),
),
if (widget.bottomBar != null)
Material(
color: theme.sideBarColor,
child: widget.bottomBar,
),
],
),
);
},
);
}
Widget _buildPage(BuildContext context) {
final theme = YaruMasterDetailTheme.of(context);
return Theme(
data: Theme.of(context).copyWith(
pageTransitionsTheme: theme.landscapeTransitions,
),
child: ScaffoldMessenger(
child: Navigator(
key: widget.navigatorKey,
initialRoute: widget.initialRoute,
onGenerateRoute: widget.onGenerateRoute,
onUnknownRoute: widget.onUnknownRoute,
pages: [
MaterialPage(
key: ValueKey(_selectedIndex),
child: Builder(
builder: (context) => widget.controller.length > _selectedIndex
? widget.pageBuilder(context, _selectedIndex)
: widget.pageBuilder(context, 0),
),
),
],
onPopPage: (route, result) => route.didPop(result),
observers: [...widget.navigatorObservers, HeroController()],
),
),
);
}
}