Files
pipelines/app/lib/ui/layout/sticky_section.dart
T
2026-10-23 22:09:54 +01:00

177 lines
5.0 KiB
Dart

import 'dart:math' as math;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../adaptive/host.dart';
import '../motion/motion.dart';
import 'section_heading.dart';
/// A collapsible section for a [CustomScrollView]: its title stays pinned at
/// the top while its content scrolls under it, so it can always be closed
/// without scrolling back. Tapping the title opens or closes it.
class StickySection extends StatelessWidget {
const StickySection({
super.key,
required this.title,
required this.open,
required this.onToggle,
required this.body,
this.trailing = const [],
this.titleColor,
});
final String title;
final bool open;
final VoidCallback onToggle;
/// The content shown while open (a box widget).
final Widget body;
/// Small actions beside the title (a count, a copy button).
final List<Widget> trailing;
final Color? titleColor;
@override
Widget build(BuildContext context) {
final extent = math.max(
44.0,
MediaQuery.textScalerOf(context).scale(16) + 28,
);
return SliverMainAxisGroup(
slivers: [
PinnedHeaderSliver(
child: SizedBox(
height: extent,
child: StickySectionTitle(
title: title,
open: open,
onToggle: onToggle,
trailing: trailing,
color: titleColor,
),
),
),
// The content grows and shrinks under the pinned title.
SliverToBoxAdapter(
child: ClipRect(
child: AnimatedSize(
duration: Motion.of(context, Motion.standard),
curve: Motion.curve,
alignment: Alignment.topCenter,
child: open ? body : const SizedBox(width: double.infinity),
),
),
),
],
);
}
}
/// A sticky section's title bar: name, actions and the open/close chevron.
class StickySectionTitle extends StatelessWidget {
const StickySectionTitle({
super.key,
required this.title,
required this.open,
required this.onToggle,
this.trailing = const [],
this.color,
});
final String title;
final bool open;
final VoidCallback onToggle;
final List<Widget> trailing;
final Color? color;
@override
Widget build(BuildContext context) {
final cupertino = usesCupertinoHost(context);
// The shared section-title look: it reads as a divider, not a row.
final heading = SectionHeadingStyle.of(context);
final muted = cupertino
? CupertinoColors.secondaryLabel.resolveFrom(context)
: Theme.of(context).colorScheme.onSurfaceVariant;
final style = color == null
? heading.title
: heading.title.copyWith(color: color);
return DecoratedBox(
decoration: BoxDecoration(
color: heading.background,
border: Border(
left: BorderSide(color: heading.accent, width: 3),
bottom: BorderSide(color: heading.line, width: 0.5),
),
),
child: Padding(
padding: const EdgeInsetsDirectional.only(start: 13, end: 8),
child: Row(
children: [
Expanded(
child: Semantics(
container: true,
header: true,
button: true,
expanded: open,
label: title,
excludeSemantics: true,
onTap: onToggle,
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: onToggle,
child: Row(
children: [
Expanded(
child: Text(
title,
style: style,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
AnimatedRotation(
turns: open ? 0.5 : 0,
duration: Motion.of(context, Motion.standard),
curve: Motion.curve,
child: Icon(
cupertino
? CupertinoIcons.chevron_down
: Icons.expand_more,
color: muted,
),
),
],
),
),
),
),
...trailing,
],
),
),
);
}
}
/// A pinned bar for controls that should stay reachable while a list
/// scrolls (a view switch).
class StickyBar extends StatelessWidget {
const StickyBar({super.key, required this.child});
final Widget child;
@override
Widget build(BuildContext context) {
final cupertino = usesCupertinoHost(context);
return PinnedHeaderSliver(
child: ColoredBox(
color: cupertino
? CupertinoColors.systemGroupedBackground.resolveFrom(context)
: Theme.of(context).colorScheme.surface,
child: child,
),
);
}
}