我对 Flutter 的 ScrollController 有疑问。当我滚动 ListView 视图并到达列表末尾时,我希望收到通知(在 ie 中的调试控制台上)。我认为我学到的东西不适用于flutter的新版本。你知道为什么吗?或者我在代码中的某个地方犯了错误?
这是我所拥有的
class _ExploreScreenState extends State<ExploreScreen> {
final mockServer = MockFooderlichService();
late final ScrollController _scrollController;
_scrollListener() {
if (_scrollController.offset >=
_scrollController.position.maxScrollExtent &&
!_scrollController.position.outOfRange) {
setState(() {
debugPrint("reach the top");
});
}
if (_scrollController.offset <=
_scrollController.position.minScrollExtent &&
!_scrollController.position.outOfRange) {
setState(() {
debugPrint("reach the top");
});
}
}
@override
void initState() {
_scrollController = ScrollController();
_scrollController.addListener(_scrollListener);
super.initState();
}
@override
Widget build(BuildContext context) {
return FutureBuilder<ExploreData>(
future: mockServer.getExploreData(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
final recipes = snapshot.data!.todayRecipes;
return ListView(
controller: _scrollController,
scrollDirection: Axis.vertical,
children: [
TodayRecipeListView(recipes: recipes),
const SizedBox(
height: 16,
),
FriendPostListView(
friendPosts: snapshot.data!.friendPosts,
),
],
);
} else {
return const Center(
child: CircularProgressIndicator(),
);
}
},
);
}
@override
void dispose() {
_scrollController.dispose();
super.dispose();
}
}