我對 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();
}
}
uj5u.com熱心網友回復:
您的代碼中沒有這樣的問題,除了列印陳述句,兩者都具有相同的 String 值 debugPrint("reach the top");
_scrollListener() {
if (_scrollController.offset >=
_scrollController.position.maxScrollExtent &&
!_scrollController.position.outOfRange) {
setState(() {
debugPrint("reach the bottom");
});
}
if (_scrollController.offset <=
_scrollController.position.minScrollExtent &&
!_scrollController.position.outOfRange) {
setState(() {
debugPrint("reach the top");
});
}
}
它解決了你的問題嗎?
uj5u.com熱心網友回復:
將此添加到您的 initState 中。請讓我知道這對你有沒有用。
_scrollController.addListener(() {
if (_scrollController.position.atEdge) {
if (_scrollController.position.pixels == 0) {
print("At the top");
} else {
print("At the bottom");
}
}
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/334429.html
上一篇:(Flutter,dart)如何在Listview中選擇特定卡片并將圖示顏色從白色切換為綠色
下一篇:沒有這樣的檔案或目錄:'Tensorflow/workspace/annotations\\label_map.pbtxtonJupyter為什么我的代碼不起作用?
