tstor/ui/lib/components/sliver_header.dart

92 lines
2.2 KiB
Dart
Raw Normal View History

2024-04-27 11:00:34 +00:00
import 'package:flutter/material.dart';
class HideableHeaderSliver extends StatelessWidget {
final Widget? leading;
final Widget body;
final double height;
final List<Widget>? actions;
const HideableHeaderSliver({
super.key,
this.leading,
required this.body,
this.actions,
this.height = 150,
});
@override
Widget build(BuildContext context) {
return SliverPersistentHeader(
floating: true,
pinned: false,
delegate: _HideableHeaderSliverDelegate(
leading: leading,
body: body,
actions: actions,
height: height,
),
);
}
}
class _HideableHeaderSliverDelegate extends SliverPersistentHeaderDelegate {
final Widget? leading;
final Widget body;
final List<Widget>? actions;
final double height;
const _HideableHeaderSliverDelegate({
required this.leading,
required this.body,
required this.actions,
required this.height,
});
@override
double get maxExtent => height;
@override
double get minExtent => height;
@override
bool shouldRebuild(covariant SliverPersistentHeaderDelegate oldDelegate) => true;
@override
Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) {
final content = <Widget>[
if (leading != null) leading!,
Expanded(child: body),
if (actions != null && actions!.isNotEmpty) ButtonBar(children: actions!),
];
final appBarTheme = AppBarTheme.of(context);
final colorScheme = Theme.of(context).colorScheme;
final onTop = (shrinkOffset == 0);
return Material(
color:
onTop ? appBarTheme.backgroundColor ?? colorScheme.surface : colorScheme.surfaceContainer,
elevation: onTop ? 0 : appBarTheme.elevation ?? 3,
surfaceTintColor: appBarTheme.surfaceTintColor ?? colorScheme.surfaceTint,
child: ClipRect(
child: SizedBox(
height: maxExtent,
child: Column(
children: [
const Spacer(),
Row(
children: content,
),
const Spacer(),
const Divider(
height: 1,
thickness: 1,
),
],
),
),
),
);
}
}