Skip to content

Commit 5022670

Browse files
committed
sticky_header example: Add a double-sliver example
1 parent fbdf8f4 commit 5022670

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

lib/example/sticky_header.dart

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,65 @@ class ExampleHorizontal extends StatelessWidget {
119119
}
120120
}
121121

122+
/// An experimental example approximating the Zulip message list.
123+
class ExampleVerticalDouble extends StatelessWidget {
124+
const ExampleVerticalDouble({
125+
super.key,
126+
required this.title,
127+
// this.reverse = false,
128+
// this.headerDirection = AxisDirection.down,
129+
}); // : assert(axisDirectionToAxis(headerDirection) == Axis.vertical);
130+
131+
final String title;
132+
// final bool reverse;
133+
// final AxisDirection headerDirection;
134+
135+
@override
136+
Widget build(BuildContext context) {
137+
const centerSliverKey = ValueKey('center sliver');
138+
const numSections = 100;
139+
const numBottomSections = 2;
140+
const numPerSection = 10;
141+
return Scaffold(
142+
appBar: AppBar(title: Text(title)),
143+
body: CustomScrollView(
144+
semanticChildCount: numSections,
145+
anchor: 0.5,
146+
center: centerSliverKey,
147+
slivers: [
148+
SliverStickyHeaderList(
149+
headerPlacement: HeaderPlacement.scrollingStart,
150+
delegate: SliverChildBuilderDelegate(
151+
childCount: numSections - numBottomSections,
152+
(context, i) {
153+
final ii = i + numBottomSections;
154+
return StickyHeaderItem(
155+
header: WideHeader(i: ii),
156+
child: Column(
157+
children: List.generate(numPerSection + 1, (j) {
158+
if (j == 0) return WideHeader(i: ii);
159+
return WideItem(i: ii, j: j-1);
160+
})));
161+
})),
162+
SliverStickyHeaderList(
163+
key: centerSliverKey,
164+
headerPlacement: HeaderPlacement.scrollingStart,
165+
delegate: SliverChildBuilderDelegate(
166+
childCount: numBottomSections,
167+
(context, i) {
168+
final ii = numBottomSections - 1 - i;
169+
return StickyHeaderItem(
170+
header: WideHeader(i: ii),
171+
child: Column(
172+
children: List.generate(numPerSection + 1, (j) {
173+
if (j == 0) return WideHeader(i: ii);
174+
return WideItem(i: ii, j: j-1);
175+
})));
176+
})),
177+
]));
178+
}
179+
}
180+
122181
////////////////////////////////////////////////////////////////////////////
123182
//
124183
// That's it!
@@ -257,6 +316,11 @@ class MainPage extends StatelessWidget {
257316
reverse: true,
258317
headerDirection: AxisDirection.left),
259318
];
319+
final otherItems = [
320+
_buildButton(context,
321+
title: 'Double slivers',
322+
page: ExampleVerticalDouble(title: 'Double slivers')),
323+
];
260324
return Scaffold(
261325
appBar: AppBar(title: const Text('Sticky Headers example')),
262326
body: CustomScrollView(slivers: [
@@ -284,6 +348,18 @@ class MainPage extends StatelessWidget {
284348
childAspectRatio: 2,
285349
crossAxisCount: 2,
286350
children: horizontalItems)),
351+
SliverToBoxAdapter(
352+
child: Padding(
353+
padding: const EdgeInsets.only(top: 24),
354+
child: Center(
355+
child: Text("Other examples",
356+
style: Theme.of(context).textTheme.headlineMedium)))),
357+
SliverPadding(
358+
padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 16),
359+
sliver: SliverGrid.count(
360+
childAspectRatio: 2,
361+
crossAxisCount: 2,
362+
children: otherItems)),
287363
]));
288364
}
289365

@@ -304,7 +380,14 @@ class MainPage extends StatelessWidget {
304380
title: title, reverse: reverse, headerDirection: headerDirection);
305381
break;
306382
}
383+
return _buildButton(context, title: title, page: page);
384+
}
307385

386+
Widget _buildButton(BuildContext context, {
387+
bool primary = false,
388+
required String title,
389+
required Widget page,
390+
}) {
308391
var label = Text(title,
309392
textAlign: TextAlign.center,
310393
style: TextStyle(

0 commit comments

Comments
 (0)