safaehActivePageSectionId function
String?
safaehActivePageSectionId({
- required List<
(String, GlobalKey< sections,State< )>StatefulWidget> > - required BuildContext scrollContext,
- double activationOffset = 96,
- ScrollMetrics? metrics,
Resolve the active section from scroll position (section tops vs viewport).
Pass ids and keys only — do not allocate translated labels on the scroll path.
Supply metrics to track the nearest section along the reading area as the
page scrolls, including short final sections that cannot reach the top.
Without metrics, section tops crossing activationOffset remain the rule.
Implementation
String? safaehActivePageSectionId({
required List<(String id, GlobalKey key)> sections,
required BuildContext scrollContext,
double activationOffset = 96,
ScrollMetrics? metrics,
}) {
if (sections.isEmpty) return null;
if (metrics != null) {
if (metrics.extentBefore <= 1) return sections.first.$1;
if (metrics.extentAfter <= 1) return sections.last.$1;
}
final scrollBox = scrollContext.findRenderObject();
if (scrollBox is! RenderBox || !scrollBox.hasSize) {
return sections.first.$1;
}
final viewportTop = scrollBox.localToGlobal(Offset.zero).dy;
final progress =
metrics == null || metrics.maxScrollExtent <= metrics.minScrollExtent
? 0.0
: ((metrics.pixels - metrics.minScrollExtent) /
(metrics.maxScrollExtent - metrics.minScrollExtent))
.clamp(0.0, 1.0);
final readingOffset =
activationOffset +
(metrics == null
? 0.0
: (metrics.viewportDimension - activationOffset - 32).clamp(
0.0,
double.infinity,
) *
progress);
var closestDistance = double.infinity;
String? active = sections.first.$1;
for (final section in sections) {
final ctx = section.$2.currentContext;
if (ctx == null) continue;
final box = ctx.findRenderObject();
if (box is! RenderBox || !box.hasSize) continue;
final sectionTop = box.localToGlobal(Offset.zero).dy;
final distance = (sectionTop - viewportTop - readingOffset).abs();
if (metrics != null
? distance < closestDistance
: sectionTop - viewportTop <= activationOffset) {
active = section.$1;
closestDistance = distance;
}
}
return active;
}