scrollToWidget static method

void scrollToWidget(
  1. GlobalKey<State<StatefulWidget>> key,
  2. ScrollController controller,
  3. double screenWidth
)
ListView(
  controller: yourScrollController,
  children: [
    YourWidget(
      key: yourGlobalKey
    )
  ]
)

onTap: (){
  Mixins.scrollToWidget(yourGlobalKey, yourScrollController, MediaQuery.of(context).size.width);
}

Implementation

static void scrollToWidget(
    GlobalKey key, ScrollController controller, double screenWidth) {
  if (key.currentContext != null) {
    RenderBox box = key.currentContext?.findRenderObject() as RenderBox;

    // get width of widget
    double w = box.size.width;

    // get horizontal position of widget
    double dx = box.localToGlobal(Offset.zero).dx;

    // get max scroll of List
    double ms = controller.position.maxScrollExtent;

    // get pixel of scroll position
    double pixel = controller.position.pixels;

    // result, the center position of widget
    double pos = (pixel + dx) - (screenWidth / 2) + (w / 2);

    // scroll to position
    controller.animateTo(
        pos < 0
            ? 0
            : pos > ms
                ? ms
                : pos,
        duration: const Duration(milliseconds: 250),
        curve: Curves.ease);
  }
}