Home > OS >  Autoscroll to a particular child in a column - Flutter
Autoscroll to a particular child in a column - Flutter

Time:11-04

I'm having a Column widget which contain a list of widgets. And this column is inside a SingleChildScrollView. For some reasons I need to autoscroll to the third child of the column while clicking the floating button.

My code looks somewhat like this.

SingleChildScrollView(
        child: Column(
          children: [
            Column(
              children:[ getWidget1(), getWidget2(), getWidget3()],
            ),
          ],
  ),
),

CodePudding user response:

Use ScrollablePositionedList instead. Create the controller

  final ItemScrollController itemScrollController = ItemScrollController();

Create scroll function

  void scrollTo(int index) {
    itemScrollController.scrollTo(index: index, duration: scrollDuration, curve: Curves.easeInOutCubic, alignment: 0);
  }

Assign it to ListBuilder

ScrollablePositionedList.builder(
              key: Key('builder $selected'), //attention
              itemCount: itemlength,
              itemScrollController: itemScrollController,
              itemPositionsListener: itemPositionsListener,
              itemBuilder: (context, index) {
                return `yourWidgets`}
            ),

Call it

ElevatedButton(
onTap:()=> scrollTo("your index here"),
child:...)

CodePudding user response:

you can see scroll_loop_auto_scroll: ^0.0.5 may be helpful

  • Related