Skip to content

Commit 5fc566c

Browse files
Fix: use the sidebar and endSidebar key parameter (#400)
* Fix: use the `sidebard` and `endSidebar` key parameter - pass sidebar key to the sidebar root widget (`AnimatedPositioned`) - create a new sidebar `ScrollController` when the key is change * Increment version and update changelog * Apply suggestions from code review * Update pubspec.lock file in example --------- Co-authored-by: Reuben Turner <groovinchip@gmail.com>
1 parent 7de5419 commit 5fc566c

File tree

4 files changed

+95
-70
lines changed

4 files changed

+95
-70
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## [1.12.5]
2+
* Fixed a bug where the `Sidebar.key` parameter wasn't used, which caused certain layouts to be unachievable.
3+
14
## [1.12.4]
25
* Default the `_selectedDay` state variable to be 1 when selecting the previous/next month from widget to ensure new date is valid for `_formatAsDateTime()` method (https://github.com/flutter/flutter/issues/123669 & https://github.com/macosui/macos_ui/pull/402)
36

example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ packages:
9797
path: ".."
9898
relative: true
9999
source: path
100-
version: "1.12.3"
100+
version: "1.12.5"
101101
matcher:
102102
dependency: transitive
103103
description:

lib/src/layout/window.dart

Lines changed: 90 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ class MacosWindow extends StatefulWidget {
5252
}
5353

5454
class _MacosWindowState extends State<MacosWindow> {
55-
final _sidebarScrollController = ScrollController();
56-
final _endSidebarScrollController = ScrollController();
55+
var _sidebarScrollController = ScrollController();
56+
var _endSidebarScrollController = ScrollController();
5757
double _sidebarWidth = 0.0;
5858
double _sidebarDragStartWidth = 0.0;
5959
double _sidebarDragStartPosition = 0.0;
@@ -74,39 +74,59 @@ class _MacosWindowState extends State<MacosWindow> {
7474
_endSidebarWidth =
7575
(widget.endSidebar?.startWidth ?? widget.endSidebar?.minWidth) ??
7676
_endSidebarWidth;
77-
if (widget.sidebar?.builder != null) {
78-
_sidebarScrollController.addListener(() => setState(() {}));
79-
}
80-
if (widget.endSidebar?.builder != null) {
81-
_endSidebarScrollController.addListener(() => setState(() {}));
82-
}
77+
_addSidebarScrollControllerListenerIfNeeded();
78+
_addEndSidebarScrollControllerListenerIfNeeded();
8379
}
8480

8581
@override
8682
void didUpdateWidget(covariant MacosWindow old) {
8783
super.didUpdateWidget(old);
88-
if (widget.sidebar == null) {
84+
final sidebar = widget.sidebar;
85+
if (sidebar == null) {
8986
_sidebarWidth = 0.0;
90-
} else if (widget.sidebar!.minWidth != old.sidebar!.minWidth ||
91-
widget.sidebar!.maxWidth != old.sidebar!.maxWidth) {
92-
if (widget.sidebar!.minWidth > _sidebarWidth) {
93-
_sidebarWidth = widget.sidebar!.minWidth;
87+
} else if (sidebar.minWidth != old.sidebar!.minWidth ||
88+
sidebar.maxWidth != old.sidebar!.maxWidth) {
89+
if (sidebar.minWidth > _sidebarWidth) {
90+
_sidebarWidth = sidebar.minWidth;
9491
}
95-
if (widget.sidebar!.maxWidth! < _sidebarWidth) {
96-
_sidebarWidth = widget.sidebar!.maxWidth!;
92+
if (sidebar.maxWidth! < _sidebarWidth) {
93+
_sidebarWidth = sidebar.maxWidth!;
9794
}
9895
}
99-
if (widget.endSidebar == null) {
96+
if (sidebar?.key != old.sidebar?.key) {
97+
_sidebarScrollController.dispose();
98+
_sidebarScrollController = ScrollController();
99+
_addSidebarScrollControllerListenerIfNeeded();
100+
}
101+
final endSidebar = widget.endSidebar;
102+
if (endSidebar == null) {
100103
_endSidebarWidth = 0.0;
101-
} else if (widget.endSidebar!.minWidth != old.endSidebar!.minWidth ||
102-
widget.endSidebar!.maxWidth != old.endSidebar!.maxWidth) {
103-
if (widget.endSidebar!.minWidth > _endSidebarWidth) {
104-
_endSidebarWidth = widget.endSidebar!.minWidth;
104+
} else if (endSidebar.minWidth != old.endSidebar!.minWidth ||
105+
endSidebar.maxWidth != old.endSidebar!.maxWidth) {
106+
if (endSidebar.minWidth > _endSidebarWidth) {
107+
_endSidebarWidth = endSidebar.minWidth;
105108
}
106-
if (widget.endSidebar!.maxWidth! < _endSidebarWidth) {
107-
_endSidebarWidth = widget.endSidebar!.maxWidth!;
109+
if (endSidebar.maxWidth! < _endSidebarWidth) {
110+
_endSidebarWidth = endSidebar.maxWidth!;
108111
}
109112
}
113+
if (endSidebar?.key != old.endSidebar?.key) {
114+
_endSidebarScrollController.dispose();
115+
_endSidebarScrollController = ScrollController();
116+
_addEndSidebarScrollControllerListenerIfNeeded();
117+
}
118+
}
119+
120+
void _addSidebarScrollControllerListenerIfNeeded() {
121+
if (widget.sidebar?.builder != null) {
122+
_sidebarScrollController.addListener(() => setState(() {}));
123+
}
124+
}
125+
126+
void _addEndSidebarScrollControllerListenerIfNeeded() {
127+
if (widget.endSidebar?.builder != null) {
128+
_endSidebarScrollController.addListener(() => setState(() {}));
129+
}
110130
}
111131

112132
@override
@@ -120,13 +140,15 @@ class _MacosWindowState extends State<MacosWindow> {
120140
// ignore: code-metrics
121141
Widget build(BuildContext context) {
122142
assert(debugCheckHasMacosTheme(context));
123-
if (widget.sidebar?.startWidth != null) {
124-
assert((widget.sidebar!.startWidth! >= widget.sidebar!.minWidth) &&
125-
(widget.sidebar!.startWidth! <= widget.sidebar!.maxWidth!));
143+
final sidebar = widget.sidebar;
144+
final endSidebar = widget.endSidebar;
145+
if (sidebar?.startWidth != null) {
146+
assert((sidebar!.startWidth! >= sidebar.minWidth) &&
147+
(sidebar.startWidth! <= sidebar.maxWidth!));
126148
}
127-
if (widget.endSidebar?.startWidth != null) {
128-
assert((widget.endSidebar!.startWidth! >= widget.endSidebar!.minWidth) &&
129-
(widget.endSidebar!.startWidth! <= widget.endSidebar!.maxWidth!));
149+
if (endSidebar?.startWidth != null) {
150+
assert((endSidebar!.startWidth! >= endSidebar.minWidth) &&
151+
(endSidebar.startWidth! <= endSidebar.maxWidth!));
130152
}
131153
final MacosThemeData theme = MacosTheme.of(context);
132154
late Color backgroundColor = widget.backgroundColor ?? theme.canvasColor;
@@ -137,8 +159,8 @@ class _MacosWindowState extends State<MacosWindow> {
137159
final isMac = !kIsWeb && defaultTargetPlatform == TargetPlatform.macOS;
138160

139161
// Respect the sidebar color override from parent if one is given
140-
if (widget.sidebar?.decoration?.color != null) {
141-
sidebarBackgroundColor = widget.sidebar!.decoration!.color!;
162+
if (sidebar?.decoration?.color != null) {
163+
sidebarBackgroundColor = sidebar!.decoration!.color!;
142164
} else if (isMac &&
143165
MediaQuery.of(context).platformBrightness.isDark ==
144166
theme.brightness.isDark) {
@@ -154,8 +176,8 @@ class _MacosWindowState extends State<MacosWindow> {
154176
}
155177

156178
// Respect the end sidebar color override from parent if one is given
157-
if (widget.endSidebar?.decoration?.color != null) {
158-
endSidebarBackgroundColor = widget.endSidebar!.decoration!.color!;
179+
if (endSidebar?.decoration?.color != null) {
180+
endSidebarBackgroundColor = endSidebar!.decoration!.color!;
159181
} else if (isMac &&
160182
MediaQuery.of(context).platformBrightness.isDark ==
161183
theme.brightness.isDark) {
@@ -173,9 +195,8 @@ class _MacosWindowState extends State<MacosWindow> {
173195
builder: (context, constraints) {
174196
final width = constraints.maxWidth;
175197
final height = constraints.maxHeight;
176-
final isAtBreakpoint = width <= (widget.sidebar?.windowBreakpoint ?? 0);
177-
final isAtEndBreakpoint =
178-
width <= (widget.endSidebar?.windowBreakpoint ?? 0);
198+
final isAtBreakpoint = width <= (sidebar?.windowBreakpoint ?? 0);
199+
final isAtEndBreakpoint = width <= (endSidebar?.windowBreakpoint ?? 0);
179200
final canShowSidebar = _showSidebar && !isAtBreakpoint;
180201
final canShowEndSidebar = _showEndSidebar && !isAtEndBreakpoint;
181202
final visibleSidebarWidth = canShowSidebar ? _sidebarWidth : 0.0;
@@ -195,8 +216,9 @@ class _MacosWindowState extends State<MacosWindow> {
195216
),
196217

197218
// Sidebar
198-
if (widget.sidebar != null)
219+
if (sidebar != null)
199220
AnimatedPositioned(
221+
key: sidebar.key,
200222
curve: curve,
201223
duration: duration,
202224
height: height,
@@ -206,39 +228,39 @@ class _MacosWindowState extends State<MacosWindow> {
206228
curve: Curves.easeInOut,
207229
color: sidebarBackgroundColor,
208230
constraints: BoxConstraints(
209-
minWidth: widget.sidebar!.minWidth,
210-
maxWidth: widget.sidebar!.maxWidth!,
231+
minWidth: sidebar.minWidth,
232+
maxWidth: sidebar.maxWidth!,
211233
minHeight: height,
212234
maxHeight: height,
213235
).normalize(),
214236
child: Column(
215237
children: [
216-
if ((widget.sidebar?.topOffset ?? 0) > 0)
217-
SizedBox(height: widget.sidebar?.topOffset),
238+
if (sidebar.topOffset > 0)
239+
SizedBox(height: sidebar.topOffset),
218240
if (_sidebarScrollController.hasClients &&
219241
_sidebarScrollController.offset > 0.0)
220242
Divider(thickness: 1, height: 1, color: dividerColor),
221-
if (widget.sidebar!.top != null &&
222-
constraints.maxHeight > 81)
243+
if (sidebar.top != null && constraints.maxHeight > 81)
223244
Padding(
224245
padding: const EdgeInsets.symmetric(horizontal: 8.0),
225-
child: widget.sidebar!.top!,
246+
child: sidebar.top!,
226247
),
227248
Expanded(
228249
child: MacosScrollbar(
229250
controller: _sidebarScrollController,
230251
child: Padding(
231-
padding: widget.sidebar?.padding ?? EdgeInsets.zero,
232-
child: widget.sidebar!
233-
.builder(context, _sidebarScrollController),
252+
padding: sidebar.padding,
253+
child: sidebar.builder(
254+
context,
255+
_sidebarScrollController,
256+
),
234257
),
235258
),
236259
),
237-
if (widget.sidebar?.bottom != null &&
238-
constraints.maxHeight > 141)
260+
if (sidebar.bottom != null && constraints.maxHeight > 141)
239261
Padding(
240262
padding: const EdgeInsets.all(16.0),
241-
child: widget.sidebar!.bottom!,
263+
child: sidebar.bottom!,
242264
),
243265
],
244266
),
@@ -273,7 +295,7 @@ class _MacosWindowState extends State<MacosWindow> {
273295
),
274296

275297
// Sidebar resizer
276-
if (widget.sidebar?.isResizable ?? false)
298+
if (sidebar?.isResizable ?? false)
277299
AnimatedPositioned(
278300
curve: curve,
279301
duration: duration,
@@ -287,13 +309,12 @@ class _MacosWindowState extends State<MacosWindow> {
287309
_sidebarDragStartPosition = details.globalPosition.dx;
288310
},
289311
onHorizontalDragUpdate: (details) {
290-
final sidebar = widget.sidebar!;
291312
setState(() {
292313
var newWidth = _sidebarDragStartWidth +
293314
details.globalPosition.dx -
294315
_sidebarDragStartPosition;
295316

296-
if (sidebar.startWidth != null &&
317+
if (sidebar!.startWidth != null &&
297318
sidebar.snapToStartBuffer != null &&
298319
(newWidth - sidebar.startWidth!).abs() <=
299320
sidebar.snapToStartBuffer!) {
@@ -338,8 +359,9 @@ class _MacosWindowState extends State<MacosWindow> {
338359
),
339360

340361
// End sidebar
341-
if (widget.endSidebar != null)
362+
if (endSidebar != null)
342363
AnimatedPositioned(
364+
key: endSidebar.key,
343365
left: width - visibleEndSidebarWidth,
344366
curve: curve,
345367
duration: duration,
@@ -350,46 +372,47 @@ class _MacosWindowState extends State<MacosWindow> {
350372
curve: Curves.easeInOut,
351373
color: endSidebarBackgroundColor,
352374
constraints: BoxConstraints(
353-
minWidth: widget.endSidebar!.minWidth,
354-
maxWidth: widget.endSidebar!.maxWidth!,
375+
minWidth: endSidebar.minWidth,
376+
maxWidth: endSidebar.maxWidth!,
355377
minHeight: height,
356378
maxHeight: height,
357379
).normalize(),
358380
child: Column(
359381
children: [
360-
if ((widget.endSidebar?.topOffset ?? 0) > 0)
361-
SizedBox(height: widget.endSidebar?.topOffset),
382+
if (endSidebar.topOffset > 0)
383+
SizedBox(height: endSidebar.topOffset),
362384
if (_endSidebarScrollController.hasClients &&
363385
_endSidebarScrollController.offset > 0.0)
364386
Divider(thickness: 1, height: 1, color: dividerColor),
365-
if (widget.endSidebar!.top != null)
387+
if (endSidebar.top != null)
366388
Padding(
367389
padding: const EdgeInsets.symmetric(horizontal: 8.0),
368-
child: widget.endSidebar!.top!,
390+
child: endSidebar.top!,
369391
),
370392
Expanded(
371393
child: MacosScrollbar(
372394
controller: _endSidebarScrollController,
373395
child: Padding(
374-
padding:
375-
widget.endSidebar?.padding ?? EdgeInsets.zero,
376-
child: widget.endSidebar!
377-
.builder(context, _endSidebarScrollController),
396+
padding: endSidebar.padding,
397+
child: endSidebar.builder(
398+
context,
399+
_endSidebarScrollController,
400+
),
378401
),
379402
),
380403
),
381-
if (widget.endSidebar?.bottom != null)
404+
if (endSidebar.bottom != null)
382405
Padding(
383406
padding: const EdgeInsets.all(16.0),
384-
child: widget.endSidebar!.bottom!,
407+
child: endSidebar.bottom!,
385408
),
386409
],
387410
),
388411
),
389412
),
390413

391414
// End sidebar resizer
392-
if (widget.endSidebar?.isResizable ?? false)
415+
if (endSidebar?.isResizable ?? false)
393416
AnimatedPositioned(
394417
curve: curve,
395418
duration: duration,
@@ -403,13 +426,12 @@ class _MacosWindowState extends State<MacosWindow> {
403426
_endSidebarDragStartPosition = details.globalPosition.dx;
404427
},
405428
onHorizontalDragUpdate: (details) {
406-
final endSidebar = widget.endSidebar!;
407429
setState(() {
408430
var newWidth = _endSidebarDragStartWidth -
409431
details.globalPosition.dx +
410432
_endSidebarDragStartPosition;
411433

412-
if (endSidebar.startWidth != null &&
434+
if (endSidebar!.startWidth != null &&
413435
endSidebar.snapToStartBuffer != null &&
414436
(newWidth + endSidebar.startWidth!).abs() <=
415437
endSidebar.snapToStartBuffer!) {

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: macos_ui
22
description: Flutter widgets and themes implementing the current macOS design language.
3-
version: 1.12.4
3+
version: 1.12.5
44
homepage: "https://macosui.dev"
55
repository: "https://github.com/GroovinChip/macos_ui"
66

0 commit comments

Comments
 (0)