-
Notifications
You must be signed in to change notification settings - Fork 32
/
yaru_compact_layout.dart
115 lines (102 loc) · 3.08 KB
/
yaru_compact_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
import 'package:flutter/material.dart';
import 'package:yaru/yaru.dart';
import '../../../yaru_widgets.dart';
/// A page layout which use a [YaruNavigationRail] on left for page navigation
class YaruCompactLayout extends StatefulWidget {
const YaruCompactLayout({
super.key,
required this.pageItems,
this.style = YaruNavigationRailStyle.compact,
this.initialIndex = 0,
});
/// A list of page destinations
final List<YaruPageItem> pageItems;
/// Define the navigation rail style, see [YaruNavigationRailStyle]
final YaruNavigationRailStyle style;
/// The index of the [YaruPageItem] that is selected from [pageItems]
final int initialIndex;
@override
State<YaruCompactLayout> createState() => _YaruCompactLayoutState();
}
class _YaruCompactLayoutState extends State<YaruCompactLayout> {
late int _index;
late ScrollController _controller;
@override
void initState() {
_controller = ScrollController();
_index = widget.initialIndex;
super.initState();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (context, constraint) {
return SafeArea(
child: Scaffold(
body: Row(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
_buildNavigationRail(context, constraint),
_buildVerticalSeparator(),
_buildPageView(context),
],
),
),
);
},
);
}
Widget _buildNavigationRail(BuildContext context, BoxConstraints constraint) {
return SizedBox(
height: MediaQuery.of(context).size.height,
child: SingleChildScrollView(
controller: _controller,
child: ConstrainedBox(
constraints: BoxConstraints(minHeight: constraint.maxHeight),
child: YaruNavigationRail(
style: widget.style,
selectedIndex: _index,
onDestinationSelected: (index) {
if (widget.pageItems[index].onTap != null) {
widget.pageItems[index].onTap!.call(context);
}
setState(() {
_index = index;
});
},
destinations: widget.pageItems,
),
),
),
);
}
Widget _buildVerticalSeparator() {
return const VerticalDivider(thickness: 1, width: 1);
}
Widget _buildPageView(BuildContext context) {
return Expanded(
child: Theme(
data: Theme.of(context).copyWith(
pageTransitionsTheme: YaruPageTransitionsTheme.vertical,
),
child: Navigator(
pages: [
MaterialPage(
key: ValueKey(_index),
child: widget.pageItems.length > _index
? widget.pageItems[_index].builder(context)
: widget.pageItems[0].builder(context),
),
],
onPopPage: (route, result) => route.didPop(result),
),
),
);
}
}