如何解决ScrollController jumpTo指定的值不起作用Flutter
在我的应用中,我决定显示一个ScrollBar
,因为ScrollController
和ScrollBar
中都需要相同的ListView
,因此我不得不更改{{1 }}到ScrollablePositionedList.builder
。
首先在我的ListView.builder
中是一个List<Widget> homePageItems
(因此在索引0处),其中包含一些“幻灯片”,然后是其他小部件“ content”,它们是幻灯片的解释。每张幻灯片中都有一个按钮,该按钮调用PageView
转到幻灯片的说明。
当我使用controller.jumpTo();
时,ScrollablePositionedList.builder
正常工作,跳转到正确的“内容”,但是使用scrollController.jumpTo(index: 5);
时,它没有..它们都只移动了几个像素,仅此而已..
我现在使用ListView.builder
设置时是否设置了某些错误或缺少设置?
ListView项:
ListView.builder
PageView项:
List<Widget> homePageItems = [
// carousell index 0
SizedBox(
height: MediaQuery.of(context).size.height > 700 ? MediaQuery.of(context).size.height * 0.89 : MediaQuery.of(context).size.height * 0.85,//580,width: MediaQuery.of(context).size.width,child: Stack(
alignment: AlignmentDirectional.center,children: [
Column(
children: [
Expanded(
child: PageView.builder(
controller: pageController,onPageChanged: (index) {
timer.cancel();
setState(() {
if (index > _index) {
_current < slides.length -1 ? _current++ : _current = 0;
_index = index;
} else {
_current > 0 ? _current -- : _current = slides.length -1;
_index = index;
}
print('onPageChanged: index is : $index');
print('onPageChanged: _current is : $_current');
});
},itemBuilder: (context,index){
return slides[_current];
}),),Row(
mainAxisAlignment: MainAxisAlignment.center,children: slides.map((url) {
int index = slides.indexOf(url);
return Container(
width: 5.0,height: 5.0,margin: EdgeInsets.symmetric(vertical: 5.0,horizontal: 2.0),decoration: Boxdecoration(
shape: BoxShape.circle,color: _current == index
? Colors.redAccent//Color.fromrGBO(0,0.9)
: Colors.black54,//Color.fromrGBO(0,0.4),);
}).toList(),],Row(
children: [
IconButton(
icon: Icon(CupertinoIcons.back,color:Colors.white.withAlpha(170),size: 40,onpressed: (){
timer.cancel();
pageController.prevIoUsPage(duration: Duration(milliseconds: 800),curve: Curves.fastOutSlowIn);
}),IconButton(
icon: Icon(CupertinoIcons.forward,color: Colors.white.withAlpha(150),onpressed: () {
timer.cancel();
pageController.nextPage(duration: Duration(milliseconds: 800),mainAxisAlignment: MainAxisAlignment.spaceBetween,// Cloud Biking index 1
SizedBox(
height: MediaQuery.of(context).size.height > 700
? MediaQuery.of(context).size.height * 0.89
: 700,//MediaQuery.of(context).size.height * 0.90,child: CloudBikingContent(key: widget.key,)
),// Stolen bicycles index 2
SizedBox(
height: MediaQuery.of(context).size.height > 700
? MediaQuery.of(context).size.height * 0.95
: 800,child: StolenBicyclesContent(key: widget.key),// Scheduler index 3
SizedBox(
height: MediaQuery.of(context).size.height > 700
? MediaQuery.of(context).size.height * 0.89 // Web
: 870,// Mobile
width: MediaQuery.of(context).size.width,child: SchedulerContent(key: widget.key,// Orders index 4
SizedBox(
height: MediaQuery.of(context).size.height > 700
? MediaQuery.of(context).size.height * 0.89
: 850,child: OrdersContent(key: widget.key,// Bookings index 5
SizedBox(
height: MediaQuery.of(context).size.height > 700
? MediaQuery.of(context).size.height * 0.89
: 850,child: BookingsContent(key: widget.key),NavigationBarBottom()
];
ListView:
List<Widget> slides = [
ScreenTypeLayout(
mobile: CloudBikingSlideMobile(
bgImage: mainBgImageMobile,onTap: (){
controller.jumpTo(1);
// scrollController.jumpTo(index: 1);
// scrollController.scrollTo(index: 1,duration: Duration(milliseconds: 300),curve: Curves.easeInOut);// .jumpTo(index: 1);
}),desktop: CloudBikingSlideDesktop(
bgImage: mainBgImageDesktop,onTap: (){
controller.jumpTo(1);
// scrollController.jumpTo(index: 1);
// scrollController.scrollTo(index: 1,curve: Curves.easeInOut);//.jumpTo(index: 1);
}),ScreenTypeLayout(
mobile: StolenBicyclesSlideMobile(
bgImage: stolenBicyclesBgMobile,onTap: (){
controller.jumpTo(2);
// scrollController.jumpTo(index: 2);
// scrollController.scrollTo(index: 2,curve: Curves.easeInOut);//.jumpTo(index: 2);
}),desktop: StolenBicyclesSlideDesktop(
bgImage: stolenBicyclesBgDesktop,onTap: (){
controller.jumpTo(2);
// scrollController.jumpTo(index: 2);
// scrollController.scrollTo(index: 2,ScreenTypeLayout(
mobile: SchedulerSlideMobile(
bgImage: schedulerBgImageMobile,onTap: (){
controller.jumpTo(3);
// scrollController.jumpTo(index: 3);
// scrollController.scrollTo(index: 3,curve: Curves.easeInOut);//.jumpTo(index: 3);
}),desktop: SchedulerSlideDesktop(
bgImage: schedulerBgImageDesktop,onTap: (){
controller.jumpTo(3);
// scrollController.jumpTo(index: 3);
// scrollController.scrollTo(index: 3,curve: Curves.easeInOut);//.jumpTo(index: 3);
}),ScreenTypeLayout(
mobile: OrdeRSSlideMobile(
bgImage: orderBgImageMobile,onTap: (){
controller.jumpTo(4);
// scrollController.jumpTo(index: 4);
// scrollController.scrollTo(index: 4,curve: Curves.easeInOut);//.jumpTo(index: 4);
}),desktop: OrdeRSSlideDesktop(
bgImage: orderBgImageDesktop,onTap: (){
controller.jumpTo(4);
// scrollController.jumpTo(index: 4);
// scrollController.scrollTo(index: 4,ScreenTypeLayout(
mobile: BookingsSlideMobile(
bgImage: bookingBgImageMobile,onTap: (){
controller.jumpTo(5);
// scrollController.jumpTo(index: 5);
// scrollController.scrollTo(index: 5,curve: Curves.easeInOut);//.jumpTo(index: 5);
}),desktop: BookingsSlideDesktop(
bgImage: bookingBgImageDesktop,];
解决方法
发现了问题。 ScrollController
的{{1}}期望像素值,而jumpTo
的{{1}}期望索引跳到相应位置并移动。.实际上是更好的解决方案恕我直言。
因此,通过将内容高度乘以其索引,您可以移动适当数量的像素。
ItemScrollController
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。