如何在颤振中滚动列表视图时检查位置?

How to check position while scrolling listview in flutter?

我想在flutter中记录元素在listview中的位置,并在此基础上更改一些显示的信息,下面是listview的使用代码

ListView.separated(
  scrollDirection: Axis.horizontal,
  separatorBuilder: (context, index) =>
      SizedBox(width: MediaQuery.of(context).size.width / 150),
      itemCount: myList.length + 1,
      itemBuilder: (context, index) => index == 0
          ? SizedBox(width: MediaQuery.of(context).size.width / 2.2)
          : ClipRRect(
              child: Padding(
                  padding: EdgeInsets.fromLTRB(2, 14, 18, 14),
                  child: Container(
                      width: MediaQuery.of(context).size.width / 2.55,
                      height: 150,
                      child: Center(
                          child: GestureDetector(
                              onTap: (){
                                  index2 = myList[index-1];
                              },
                              child: Text(
                              myList[index - 1].toString(),
                                 style: TextStyle(
                                     color: Colors.black,
                                     fontSize: 24,
                                     fontWeight: FontWeight.bold),
                                  ),
                           )
                        ),
                        decoration: BoxDecoration(
                           borderRadius:
                                BorderRadius.all(Radius.circular(10)),
                           boxShadow: [
                                BoxShadow(
                                  color: Colors.grey,
                                  offset: Offset(2.0, 2.0), //(x,y)
                                  blurRadius: 4.0,
                                ),
                              ],
                            gradient: LinearGradient(
                                colors: [Colors.blue[200], Colors.white],
                                begin: Alignment.topLeft,
                                end: Alignment.bottomRight
                            )
                        ),
                ),
          )
    )
),

如何记录元素的位置,然后根据该位置显示另一组信息?

要获得列表的 offSets,您需要在 scrollController 上添加一个侦听器,这样您就可以在每次更改时获得 offSets。 这是一个示例代码:

  @override
  _RadioTestState createState() => _RadioTestState();
}

class _RadioTestState extends State<RadioTest> {
  ScrollController _scrollController;
  List<String> list;
  @override
  void initState() {
    super.initState();
    _scrollController = ScrollController()
      ..addListener(() {
        print("offset = ${_scrollController.offset}");
      });
    list = List<String>();
    for (var i = 0; i < 50; i++) {
      list.add("test");
    }
  }

  @override
  Widget build(BuildContext context) {
    return ListView(
      controller: _scrollController,
      children: [
        ...list.map(
          (item) => ListTile(
            title: Text("test"),
          ),
        ),
      ],
    );
  }
}

祝你好运。