-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathyaru_portrait_layout.dart
150 lines (135 loc) · 4.61 KB
/
yaru_portrait_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
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:yaru/foundation.dart' show YaruPageController;
import 'package:yaru/widgets.dart'
show YaruTitleBarTheme, YaruTitleBarThemeData, YaruTitleBarStyle;
import 'yaru_master_detail_page.dart';
import 'yaru_master_detail_theme.dart';
import 'yaru_master_list_view.dart';
class YaruPortraitLayout extends StatefulWidget {
const YaruPortraitLayout({
super.key,
required this.navigatorKey,
this.navigatorObservers = const <NavigatorObserver>[],
this.initialRoute,
this.onGenerateRoute,
this.onUnknownRoute,
required this.tileBuilder,
required this.pageBuilder,
this.onSelected,
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 PreferredSizeWidget? appBar;
final Widget? bottomBar;
final YaruPageController controller;
@override
State<YaruPortraitLayout> createState() => _YaruPortraitLayoutState();
}
class _YaruPortraitLayoutState extends State<YaruPortraitLayout> {
late int _selectedIndex;
NavigatorState get _navigator => widget.navigatorKey.currentState!;
@override
void initState() {
widget.controller.addListener(_controllerCallback);
_selectedIndex = widget.controller.index;
super.initState();
}
@override
void dispose() {
widget.controller.removeListener(_controllerCallback);
super.dispose();
}
@override
void didUpdateWidget(covariant YaruPortraitLayout oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.controller != oldWidget.controller) {
oldWidget.controller.removeListener(_controllerCallback);
widget.controller.addListener(_controllerCallback);
_selectedIndex = widget.controller.index;
}
}
void _controllerCallback() {
if (widget.controller.index != _selectedIndex) {
setState(() => _selectedIndex = widget.controller.index);
}
}
void _onTap(int index) {
widget.controller.index = index;
widget.onSelected?.call(_selectedIndex);
}
MaterialPage page(int index) {
return MaterialPage(
key: ValueKey(index),
child: Builder(
builder: (context) => widget.pageBuilder(context, _selectedIndex),
),
);
}
@override
Widget build(BuildContext context) {
final theme = YaruMasterDetailTheme.of(context);
return PopScope(
onPopInvoked: (v) async => await _navigator.maybePop(),
child: Theme(
data: Theme.of(context).copyWith(
pageTransitionsTheme: theme.portraitTransitions,
),
child: Navigator(
key: widget.navigatorKey,
initialRoute: widget.initialRoute,
onGenerateRoute: widget.onGenerateRoute,
onUnknownRoute: widget.onUnknownRoute,
onPopPage: (route, result) {
_selectedIndex = -1;
return route.didPop(result);
},
pages: [
MaterialPage(
key: const ValueKey(-1),
child: YaruTitleBarTheme(
data: const YaruTitleBarThemeData(
style: kIsWeb
? YaruTitleBarStyle.undecorated
: YaruTitleBarStyle.normal,
),
child: Scaffold(
backgroundColor: theme.sideBarColor,
appBar: widget.appBar,
body: LayoutBuilder(
builder: (context, constraints) => YaruMasterListView(
length: widget.controller.length,
selectedIndex: _selectedIndex,
onTap: _onTap,
builder: widget.tileBuilder,
availableWidth: constraints.maxWidth,
startUndershoot: widget.appBar != null,
endUndershoot: widget.bottomBar != null,
),
),
bottomNavigationBar: widget.bottomBar == null
? null
: Material(
color: theme.sideBarColor,
child: widget.bottomBar,
),
),
),
),
if (_selectedIndex != -1) page(_selectedIndex),
],
observers: [...widget.navigatorObservers, HeroController()],
),
),
);
}
}