我的代码是:
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(
home: MyHomePage(),
));
class MyHomePage extends StatelessWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: NotificationListener(
onNotification: (scrollNotification) {
print(scrollNotification);
return true;
},
child: ListView.builder(
scrollDirection: Axis.horizontal, // <= works if replaced by vertical
itemCount: 2, // <= works if replaces by 22
itemBuilder: (context, index) {
return const Text("text ");
},
),
),
),
);
}
}
当我滚动时,它不会生成滚动事件。
如果我更改为
Axis.vertical
,它会按预期工作。
如果我将
itemCount
更改为 22 - 就可以了。
使用物理:AlwaysScrollableScrollPhysics(),即使没有足够的项目,也会强制列表视图可滚动,以便侦听器接收数据。