Skip to content

Instantly share code, notes, and snippets.

@Piinks
Created August 9, 2023 18:29
Show Gist options
  • Save Piinks/6e2b7245063001576a3a83adb23f1121 to your computer and use it in GitHub Desktop.
Save Piinks/6e2b7245063001576a3a83adb23f1121 to your computer and use it in GitHub Desktop.
New Slivers
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
const MyHomePage({
Key? key,
required this.title,
}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(widget.title)),
body: CustomScrollView(
slivers: <Widget>[
SliverMainAxisGroup(slivers: [
const SliverPersistentHeader(
pinned: true,
delegate: HeaderDelegate('Section 1'),
),
SliverPadding(
padding: const EdgeInsets.all(8.0),
sliver: _getDecoratedSliverList(10),
),
]),
SliverMainAxisGroup(slivers: [
const SliverPersistentHeader(
pinned: true,
delegate: HeaderDelegate('Section 2'),
),
SliverCrossAxisGroup(
slivers: [
SliverConstrainedCrossAxis(
maxExtent: 100,
sliver: SliverPadding(
padding: const EdgeInsets.all(8.0),
sliver: _getDecoratedSliverList(10),
),
),
SliverCrossAxisExpanded(
flex: 2,
sliver: SliverPadding(
padding: const EdgeInsets.all(8.0),
sliver: _getDecoratedSliverList(20),
),
),
SliverPadding(
padding: const EdgeInsets.all(8.0),
sliver: _getDecoratedSliverList(10),
),
],
),
]),
],
),
);
}
Widget _getDecoratedSliverList(int itemCount) {
return DecoratedSliver(
decoration: BoxDecoration(
color: Colors.purple[50],
borderRadius: const BorderRadius.all(Radius.circular(20)),
),
sliver: SliverList.separated(
itemBuilder: (_, int index) => Padding(
padding: const EdgeInsets.all(10.0),
child: Text('Item $index'),
),
separatorBuilder: (_, __) => const Divider(indent: 8, endIndent: 8),
itemCount: itemCount,
),
);
}
}
class HeaderDelegate extends SliverPersistentHeaderDelegate {
const HeaderDelegate(this.title);
final String title;
@override
Widget build(
BuildContext context, double shrinkOffset, bool overlapsContent) {
return Container(
height: 30,
color: Colors.purple[100],
child: Padding(
padding: const EdgeInsets.all(4.0),
child: Text(title),
));
}
@override
double get maxExtent => minExtent;
@override
double get minExtent => 30;
@override
bool shouldRebuild(covariant SliverPersistentHeaderDelegate oldDelegate) =>
false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment