问题描述
在我的 Flutter 应用程序中,我尝试使用 scrollController 在 listBuilder 中滚动列表视图,但 ScrollController 无法正常工作。它不显示错误或异常,但列表不滚动。即使我使用滚动控制器 jumpTo 或 animateto 它也不起作用。即使我在 animateto 和 jumpTo 中给出最大值,如 1000 和 20000,它也不会滚动。
import 'package:Flutter/gestures.dart';
import 'package:Flutter/material.dart';
class Class extends StatefulWidget {
final int data;
final List list;
Class({required this.data,required this.list});
@override
_Classstate createState() => _Classstate();
}
class _Classstate extends State<Class> with TickerProviderStateMixin {
late Animation<double> animation;
late AnimationController animationController;
late double height = 0;
final List lst = [
[true,true,true],[false,false,false],[true,];
void initState() {
super.initState();
animationController =
AnimationController(vsync: this,duration: Duration(seconds: 5));
animation = Tween<double>(begin: 0,end: 200).animate(animationController)
..addListener(() {
setState(() {});
})
..addStatusListener((status) {});
animationController.forward();
}
Widget container(data) {
return Stack(
children: [
GestureDetector(
onTap: () {
setState(() {
animationController.forward();
});
},child: Transform.translate(
offset: Offset(0,animation.value),child: Container(
color: lst[data][0] ? Colors.black : Colors.transparent,height: MediaQuery.of(context).size.height / 4,width: MediaQuery.of(context).size.width / 4,child: Text('$data'),),],);
}
@override
Widget build(BuildContext context) {
return container(widget.data);
}
@override
void dispose() {
super.dispose();
animationController.dispose();
}
}
class Yes extends StatefulWidget {
@override
_Yesstate createState() => _Yesstate();
}
class _Yesstate extends State<Yes> {
List lst = [
[true,];
ScrollController _scrollController = ScrollController();
void initState() {
if (_scrollController.hasClients) {
super.initState();
// _scrollController.animateto(0,// // _scrollController.offset + MediaQuery.of(context).size.height/6,// duration: Duration(seconds: 2),// curve: Curves.linear);
_scrollController.jumpTo(_scrollController.position.maxScrollExtent);
}
}
@override
Widget build(BuildContext context) {
return ListView(
controller: _scrollController,// physics: NeverScrollableScrollPhysics(),children: [
ListView.builder(
// physics: NeverScrollableScrollPhysics(),reverse: true,itemCount: lst.length,controller: _scrollController,itemBuilder: (BuildContext context,int position) {
return Stack(children: [Class(data: position,list: lst)]);
}),);
}
}
解决方法
你的代码不够清晰但是 试试这个,因为例子工作得很好
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',theme: ThemeData(
primarySwatch: Colors.blue,),home: ListExample(),);
}
}
class ListExample extends StatelessWidget {
ListExample({Key? key}) : super(key: key);
final ScrollController controller = ScrollController();
double pos = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Expanded(
child: ListView.builder(
controller: controller,itemCount: 10000,itemBuilder: (context,index) {
return Text('item $index');
},ElevatedButton(
onPressed: () {
controller.jumpTo(pos+=250);
},child: Text(' Jump'))
],);
}
}
,
您必须附加侦听器来侦听 ScrollController 的位置以跟踪位置。像这个例子
ScrollController _scrollController;
double _lastEndOfScroll = 0;
_scrollController.addListener(() {
double maxScroll = _scrollController.position.maxScrollExtent;
double currentScroll = _scrollController.position.pixels;
if (maxScroll == currentScroll) {
if (_page < _totalPages - 1 && !_isFetchingData) {
_lastEndOfScroll = maxScroll;
_page++;
print(_page.toString());
search.filter.pageNumber = _page;
fetchData(search.filter);
}
}
});
if (!_isFetchingData) _scrollController.jumpTo(_lastEndOfScroll);