getIndex function

int getIndex(
  1. BuildContext context,
  2. Offset globalPosition
)

Implementation

int getIndex(BuildContext context, Offset globalPosition) {
  //拿到box
  RenderBox box = context.findRenderObject();
  //拿到y值
  double y = box.globalToLocal(globalPosition).dy;
  //算出字符高度
  var itemHeight = ScreenHeight(context) / 2 / INDEX_WORDS.length;
  //算出第几个item,并且给一个取值范围
  int index = (y ~/ itemHeight).clamp(0, INDEX_WORDS.length - 1);

//  print('现在选中的是${INDEX_WORDS[index]}');
  return index;
}