safaehActivePageSectionId function

String? safaehActivePageSectionId({
  1. required List<(String, GlobalKey<State<StatefulWidget>>)> sections,
  2. required BuildContext scrollContext,
  3. double activationOffset = 96,
})

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.

Implementation

String? safaehActivePageSectionId({
  required List<(String id, GlobalKey key)> sections,
  required BuildContext scrollContext,
  double activationOffset = 96,
}) {
  if (sections.isEmpty) return null;
  final scrollBox = scrollContext.findRenderObject();
  if (scrollBox is! RenderBox || !scrollBox.hasSize) {
    return sections.first.$1;
  }

  final viewportTop = scrollBox.localToGlobal(Offset.zero).dy;
  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;
    if (sectionTop - viewportTop <= activationOffset) {
      active = section.$1;
    }
  }
  return active;
}