锚点滚动组件

  • 效果

  • 示例代码

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final GlobalKey<AnchorPointScrollViewState> _anchorKey = GlobalKey();
  int _currentIndex = 0;

  _onPress(int index) {
    _anchorKey.currentState?.jumpToIndex(index);
    // _anchorKey.currentState?.animatedTo(index,duration:const Duration(milliseconds: 150),curve: Curves.easeInOut);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Column(children: [
        Wrap(
          children: List.generate(10, (index) {
            return TextButton(
              onPressed: () => _onPress(index),
              child: Text(
                index.toString(),
                style: TextStyle(
                  color: index == _currentIndex ? Colors.red : Colors.grey,
                  fontSize: index == _currentIndex ? 24 : 12,
                ),
              ),
            );
          }),
        ),
        Expanded(
          child: AnchorPointScrollView(
            key: _anchorKey,
            onIndexChanged: (index) {
              setState(() {
                _currentIndex = index;
              });
            },
            children: List.generate(10, (index) {
              return Container(
                height: 80.0 * (index+1),
                color: Colors.primaries[index],
                alignment: Alignment.center,
                child: Text(index.toString(),
                    style: const TextStyle(fontSize: 28, color: Colors.black)),
              );
            }),
          ),
        ),
      ]),
    );
  }
}


参数

参数名描述是否必传
childrenwidget 列表
excludeOffset不参与计算定位的偏移量
key组件key
onIndexChanged滚动到对应widget 的回调
onScroll滚动过程的回调

可调用方法

  • 滚动到指定下标组件的位置
jumpToIndex(int index);
animatedTo(
    int index, {
    required Duration duration,
    required Curve curve,
  }) 

Libraries

anchor_point_scroll_view