Skip to content

Instantly share code, notes, and snippets.

@hawkkiller
Created October 27, 2023 15:01
Show Gist options
  • Save hawkkiller/9aaea5e1c6069badb63b9e6a33d8292e to your computer and use it in GitHub Desktop.
Save hawkkiller/9aaea5e1c6069badb63b9e6a33d8292e to your computer and use it in GitHub Desktop.
Sticky headers in Flutter
import 'package:flutter/material.dart';
import 'package:stickyheaders/model.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) => const MaterialApp(
home: ExampleScreen(),
);
}
class ExampleScreen extends StatelessWidget {
const ExampleScreen({super.key});
static final _data = List.generate(
20,
(index) => Section(
'Section $index',
List.generate(
10,
(index) => Item('Item $index'),
),
),
);
@override
Widget build(BuildContext context) {
return Scaffold(
body: CustomScrollView(
slivers: [
for (final section in _data)
SliverMainAxisGroup(
slivers: [
SliverAppBar(
pinned: true,
title: Text(
section.title,
style: Theme.of(context).textTheme.bodyLarge,
),
),
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) => ListTile(
title: Text(section.items[index].content),
),
childCount: section.items.length,
),
),
],
),
],
),
);
}
}
final class Section {
Section(
this.title,
this.items,
);
final List<Item> items;
final String title;
}
final class Item {
Item(this.content);
final String content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment