flutter_scroll_to_index

pub

flutter滚动组件

Listview 滑动到指定item,类似于定位功能

如何使用?

1.创建控制器
final ScrollToIndexController _scrollToIndexController = ScrollToIndexController();
2.自定义模型对象

注意自定义对象一定要继承ScrollToIndexBaseObject

class TestObject extends ScrollToIndexBaseObject {
  final String name;
  final int age;
  TestObject(this.name, this.age);
}
3.在页面中使用widget

使用组件ScrollToIndex

      ScrollToIndex(
        itemBuilder: (context, index) {
          TestObject item = objs[index]; // 模型 model
          return Card(
            key: item.globalKey, // 必须要的,用来定位,ScrollToIndexBaseObject 的属性
            elevation: 20,
            margin: EdgeInsets.all(20),
            child: Container(
              padding: EdgeInsets.all(8),
              child: Column(
                children: [
                  Text(item.name),
                  Text("${item.age}"),
                ],
              ),
            ),
          );
        },
        list: objs, // 必须要的 model模型列表, 是个数组
        controller: _scrollToIndexController, // 必须要的 controller 
      )
4.滑动到指定index

使用_scrollToIndexController.to(int index)方法滑动到指定item

_scrollToIndexController.to(6);
6.销毁
_scrollToIndexController.dispose();
数组
final List<TestObject> objs = List.generate(20, (index) => TestObject("典典${index + 1}", 22 + index));