Skip to content

Commit

Permalink
Make scaffold support header and layout it on a custom layout
Browse files Browse the repository at this point in the history
  • Loading branch information
tilucasoli committed Feb 5, 2025
1 parent 10083ca commit 991368f
Showing 1 changed file with 61 additions and 1 deletion.
62 changes: 61 additions & 1 deletion packages/remix/lib/src/components/scaffold/scaffold_widget.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
part of 'scaffold.dart';

enum _ScaffoldElement {
header,
body;
}

/// A widget that provides a basic structure for a page or screen.
///
/// It consists of a single [body] widget that is wrapped in a
Expand All @@ -26,11 +31,14 @@ part of 'scaffold.dart';
class Scaffold extends StatelessWidget {
const Scaffold({
super.key,
this.header,
required this.body,
this.style,
this.variants = const [],
});

final Widget? header;

/// The primary content of the scaffold.
final Widget body;

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

return spec.container(child: ToastLayer(child: body));
return spec.container(
child: ToastLayer(
child: CustomMultiChildLayout(
delegate: _ScaffoldLayoutDelegate(),
children: [
if (header != null)
LayoutId(
id: _ScaffoldElement.header,
child: header!,
),
LayoutId(
id: _ScaffoldElement.body,
child: body,
),
],
),
),
);
},
);
}
}

class _ScaffoldLayoutDelegate extends MultiChildLayoutDelegate {
@override
void performLayout(Size size) {
final BoxConstraints looseConstraints = BoxConstraints.loose(size);
final BoxConstraints fullWidthConstraints =
looseConstraints.tighten(width: size.width);

double appBarHeight = 0.0;

const headerKey = _ScaffoldElement.header;
if (hasChild(headerKey)) {
appBarHeight = layoutChild(headerKey, fullWidthConstraints).height;
positionChild(headerKey, Offset.zero);
}

const bodyKey = _ScaffoldElement.body;
if (hasChild(bodyKey)) {
final listHeight = size.height - appBarHeight;

final bodyConstraints = BoxConstraints.tightFor(
width: fullWidthConstraints.maxWidth,
height: listHeight,
);

layoutChild(bodyKey, bodyConstraints);
positionChild(bodyKey, Offset(0, appBarHeight));
}
}

@override
bool shouldRelayout(covariant MultiChildLayoutDelegate oldDelegate) {
return false;
}
}

0 comments on commit 991368f

Please sign in to comment.