how to save performance for scrolling to index from ListView.builder and GetBuilder/GetXController

Viewed 12

here is my issue, currently I have a statefulWidget and wrapped by GetBuilder, so I can listen the each widget from ListView when I scroll up and down, but now the performance is pretty low and I have no idea how to solve it.

here is the code structure:

  ListView buildYear(TimelineController controller) {
        return ListView.builder(
          physics: BouncingScrollPhysics(),
          controller: scrollController,
          key: _animatedKey,
          scrollDirection: Axis.vertical,
          shrinkWrap: true,
          itemCount: controller.yearOld,
          itemBuilder: (context, index) {
            print('rendering');     // <- here I got really lots of print when I scroll up/down
            return TimelineItemWidget(
              controller: controller,
              index: index,
            );
          },
        );
      };

Widget TimelineItemWidget({int index, TimelineController controller}) {
    // here I need to test the offset of scrollController and give the bool value to Widget, to update Widget status, I dont know it it is a good idea
    return Widget(...);
  }

I am also listening the scrollController in initState to calculate in-time value when I am scrolling:

  @override
  void initState() {
    super.initState();
     scrollController.addListener(() {
     timelineController.getActivatedIndex();
     timelineController.getScrollPy();
    });}

The reason that frame is so low I guess is followings:

  1. I am listening the ScrollController to get current offset, so I can know which widget from ListView.builder is scrolled to.(Because each widget has different height).
  2. Each step I am scroll screen up and down, ListView needs to calculate if the widget needs to be updated based on the value returned by scrollController.
  3. If I extract the widget to a statelessWidget, does it help to save performance?
0 Answers
Related