Skip to content

Commit 991368f

Browse files
committed
Make scaffold support header and layout it on a custom layout
1 parent 10083ca commit 991368f

File tree

1 file changed

+61
-1
lines changed

1 file changed

+61
-1
lines changed

packages/remix/lib/src/components/scaffold/scaffold_widget.dart

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
part of 'scaffold.dart';
22

3+
enum _ScaffoldElement {
4+
header,
5+
body;
6+
}
7+
38
/// A widget that provides a basic structure for a page or screen.
49
///
510
/// It consists of a single [body] widget that is wrapped in a
@@ -26,11 +31,14 @@ part of 'scaffold.dart';
2631
class Scaffold extends StatelessWidget {
2732
const Scaffold({
2833
super.key,
34+
this.header,
2935
required this.body,
3036
this.style,
3137
this.variants = const [],
3238
});
3339

40+
final Widget? header;
41+
3442
/// The primary content of the scaffold.
3543
final Widget body;
3644

@@ -50,8 +58,60 @@ class Scaffold extends StatelessWidget {
5058
builder: (context) {
5159
final spec = ScaffoldSpec.of(context);
5260

53-
return spec.container(child: ToastLayer(child: body));
61+
return spec.container(
62+
child: ToastLayer(
63+
child: CustomMultiChildLayout(
64+
delegate: _ScaffoldLayoutDelegate(),
65+
children: [
66+
if (header != null)
67+
LayoutId(
68+
id: _ScaffoldElement.header,
69+
child: header!,
70+
),
71+
LayoutId(
72+
id: _ScaffoldElement.body,
73+
child: body,
74+
),
75+
],
76+
),
77+
),
78+
);
5479
},
5580
);
5681
}
5782
}
83+
84+
class _ScaffoldLayoutDelegate extends MultiChildLayoutDelegate {
85+
@override
86+
void performLayout(Size size) {
87+
final BoxConstraints looseConstraints = BoxConstraints.loose(size);
88+
final BoxConstraints fullWidthConstraints =
89+
looseConstraints.tighten(width: size.width);
90+
91+
double appBarHeight = 0.0;
92+
93+
const headerKey = _ScaffoldElement.header;
94+
if (hasChild(headerKey)) {
95+
appBarHeight = layoutChild(headerKey, fullWidthConstraints).height;
96+
positionChild(headerKey, Offset.zero);
97+
}
98+
99+
const bodyKey = _ScaffoldElement.body;
100+
if (hasChild(bodyKey)) {
101+
final listHeight = size.height - appBarHeight;
102+
103+
final bodyConstraints = BoxConstraints.tightFor(
104+
width: fullWidthConstraints.maxWidth,
105+
height: listHeight,
106+
);
107+
108+
layoutChild(bodyKey, bodyConstraints);
109+
positionChild(bodyKey, Offset(0, appBarHeight));
110+
}
111+
}
112+
113+
@override
114+
bool shouldRelayout(covariant MultiChildLayoutDelegate oldDelegate) {
115+
return false;
116+
}
117+
}

0 commit comments

Comments
 (0)