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"),
),
),
],
);
}
}
祝你好运 。
推荐阅读
- android - Android Jetpack Navigation 组件条件导航问题
- sql - 在 Oracle SQL 中查看用户的所有表修改
- regex - 正则表达式初学者问题 - 找不到数字组合
- spring-boot - 通过示例的 Spring Data JPA 查询返回 Null
- bash - ip主机脚本bash
- python-3.x - 找出两个方程的交集
- python - 无法在 Windows 10 上安装 python pandas
- javascript - 如何避免 useRef 影响 DOM 中的其他元素
- arrays - 多个模拟输入为每个通道产生单独的平均值
- sql - PostgresSQL:如何计算一行中的值,然后在行值更改时重置计数