我是 Flutter 和 UI 开发的新手,我被困住了,所以我需要一些帮助!
我正在尝试使用延迟加载/无限滚动在我的应用程序中构建视频帖子列表。延迟加载部分似乎工作正常,但我遇到的问题是每次加载新视频时,构建后滚动都会回到顶部。这是我的代码:
class _ProfileState extends State<Profile> {
Future _videoResponseFuture;
ScrollController _scrollController = ScrollController();
UserService _userService = UserService();
List<VideoResponse> _videos = [];
@override
void initState() {
super.initState();
_videoResponseFuture = _getUserVideos();
_scrollController.addListener(() {
if (_scrollController.position.pixels ==
_scrollController.position.maxScrollExtent) {
_getMore();
}
});
}
@override
void dispose() {
super.dispose();
_scrollController.dispose();
}
Future<List<VideoResponse>> _getUserVideos() async {
return _userService.getUserVideos();
}
_getMore() {
setState(() {
_videoResponseFuture = _userService.getMoreVideos();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Title'),
),
body: Scaffold(
body: SafeArea(
child: Padding(
child: Column(
children: <Widget>[
// Other children...
Expanded(
child: FutureBuilder(
future: _videoResponseFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (_videos.length == 0) {
_videos = snapshot.data;
} else {
List<VideoResponse> newVideos = snapshot.data;
newVideos.forEach((element) {
_videos.add(element);
});
}
return Scrollbar(
child: ListView.builder(
shrinkWrap: true,
itemCount: _videos.length,
scrollDirection: Axis.vertical,
controller: _scrollController,
itemBuilder: (BuildContext context, int index) {
return Text(_videos[index].videoDetails.fileName);
},
),
);
}
return CircularProgressIndicator();
},
),
),
],
),
),
),
),
);
}
}
我试图让滚动控制器在initState
方法中使用以下代码滚动到底部,但它不起作用:
scrollController.jumpTo(scrollController.position.maxScrollExtent);
我还尝试了使用 a 的不同滚动替代方案,ScrollController
但似乎没有其他方法可以使用ListView
.
任何想法将不胜感激!