scrollToTarget static method

Future<void> scrollToTarget(
  1. GlobalKey<State<StatefulWidget>> key,
  2. ScrollController scrollController
)

Implementation

static Future<void> scrollToTarget(
    GlobalKey key, ScrollController scrollController) async {
  final context = key.currentContext;
  if (context != null) {
    final box = context.findRenderObject() as RenderBox;
    final position = box.localToGlobal(Offset.zero);
    final scrollableBox = scrollController.position.context.storageContext
        .findRenderObject() as RenderBox;
    final offset = position.dy - scrollableBox.localToGlobal(Offset.zero).dy;
    await scrollController.animateTo(
      scrollController.offset + offset,
      duration: const Duration(milliseconds: 500),
      curve: Curves.easeInOut,
    );
  }
}