Skip to content

Commit 2e1faed

Browse files
committed
sticky_header example: Add double slivers with header at bottom
These changes are NFC for the existing double-sliver example, with the sticky header at the top.
1 parent 191a143 commit 2e1faed

File tree

1 file changed

+41
-11
lines changed

1 file changed

+41
-11
lines changed

lib/example/sticky_header.dart

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,40 +125,61 @@ class ExampleVerticalDouble extends StatelessWidget {
125125
super.key,
126126
required this.title,
127127
// this.reverse = false,
128-
// this.headerDirection = AxisDirection.down,
129-
}); // : assert(axisDirectionToAxis(headerDirection) == Axis.vertical);
128+
required this.headerPlacement,
129+
});
130130

131131
final String title;
132132
// final bool reverse;
133-
// final AxisDirection headerDirection;
133+
final HeaderPlacement headerPlacement;
134134

135135
@override
136136
Widget build(BuildContext context) {
137137
const numSections = 4;
138138
const numBottomSections = 2;
139+
const numTopSections = numSections - numBottomSections;
139140
const numPerSection = 10;
141+
142+
final headerAtBottom = switch (headerPlacement) {
143+
HeaderPlacement.scrollingStart => false,
144+
HeaderPlacement.scrollingEnd => true,
145+
};
146+
147+
// Choose the "center" sliver so that the sliver which might need to paint
148+
// a header overflowing the other header is the sliver that paints last.
149+
final centerKey = headerAtBottom ?
150+
const ValueKey('bottom') : const ValueKey('top');
151+
152+
// This is a side effect of our choice of centerKey.
153+
final topSliverGrowsUpward = headerAtBottom;
154+
140155
return Scaffold(
141156
appBar: AppBar(title: Text(title)),
142157
body: CustomScrollView(
143158
semanticChildCount: numSections,
159+
center: centerKey,
144160
slivers: [
145161
SliverStickyHeaderList(
146-
headerPlacement: HeaderPlacement.scrollingStart,
162+
key: const ValueKey('top'),
163+
headerPlacement: headerPlacement,
147164
delegate: SliverChildBuilderDelegate(
148165
childCount: numSections - numBottomSections,
149166
(context, i) {
150-
final ii = numSections - 1 - i;
167+
final ii = numBottomSections
168+
+ (topSliverGrowsUpward ? i : numTopSections - 1 - i);
151169
return StickyHeaderItem(
152170
allowOverflow: true,
153171
header: WideHeader(i: ii),
154172
child: Column(
173+
verticalDirection: headerAtBottom
174+
? VerticalDirection.up : VerticalDirection.down,
155175
children: List.generate(numPerSection + 1, (j) {
156176
if (j == 0) return WideHeader(i: ii);
157177
return WideItem(i: ii, j: j-1);
158178
})));
159179
})),
160180
SliverStickyHeaderList(
161-
headerPlacement: HeaderPlacement.scrollingStart,
181+
key: const ValueKey('bottom'),
182+
headerPlacement: headerPlacement,
162183
delegate: SliverChildBuilderDelegate(
163184
childCount: numBottomSections,
164185
(context, i) {
@@ -167,10 +188,12 @@ class ExampleVerticalDouble extends StatelessWidget {
167188
allowOverflow: true,
168189
header: WideHeader(i: ii),
169190
child: Column(
191+
verticalDirection: headerAtBottom
192+
? VerticalDirection.up : VerticalDirection.down,
170193
children: List.generate(numPerSection + 1, (j) {
171-
if (j == 0) return WideHeader(i: ii);
172-
return WideItem(i: ii, j: j-1);
173-
})));
194+
if (j == 0) return WideHeader(i: ii);
195+
return WideItem(i: ii, j: j-1);
196+
})));
174197
})),
175198
]));
176199
}
@@ -319,8 +342,15 @@ class MainPage extends StatelessWidget {
319342
];
320343
final otherItems = [
321344
_buildButton(context,
322-
title: 'Double slivers',
323-
page: ExampleVerticalDouble(title: 'Double slivers')),
345+
title: 'Double slivers, headers at top',
346+
page: ExampleVerticalDouble(
347+
title: 'Double slivers, headers at top',
348+
headerPlacement: HeaderPlacement.scrollingStart)),
349+
_buildButton(context,
350+
title: 'Double slivers, headers at bottom',
351+
page: ExampleVerticalDouble(
352+
title: 'Double slivers, headers at bottom',
353+
headerPlacement: HeaderPlacement.scrollingEnd)),
324354
];
325355
return Scaffold(
326356
appBar: AppBar(title: const Text('Sticky Headers example')),

0 commit comments

Comments
 (0)