Flutter - 如何重置 PageView 中的当前页面索引(刷卡后)?
Flutter - How to reset the current page index in PageView (after card swipe)?
我有一个简单的PageView.builder:
@override
void initState() {
super.initState();
_pageController =
PageController(initialPage: 0, keepPage: true, viewportFraction: 1);
_pageController.addListener(() {
setState(() {
_activeImageIndex = _pageController.page!.toInt();
});
});
}
......
child: PageView.builder(
scrollDirection: Axis.vertical,
controller: _pageController,
itemCount: lstUserImages.length,
onPageChanged: (page) {
setState(() {
_activeImageIndex = page;
});
},
itemBuilder: (context, index) {
return GestureDetector(
child: Image.asset(
lstUserImages[index],
fit: BoxFit.cover,
),
);
}),
它工作正常,只是当我刷卡时,它不会将页面索引重置为 0(对于下一张卡)。因此,如果对于一张卡片,我正在查看第三张图片(索引 = 2),那么在我刷卡后,下一张卡片会加载显示第三张图片。
我有 onSwipeComplete 可用:
swipeCompleteCallback:
(CardSwipeOrientation orientation, int index) {
setState(() {
//WHAT DO I DO HERE SO THAT THE PAGE INDEX IS SET TO ZERO AGAIN..??
//isCardChanged = true;
//_pageController.jumpToPage(0);
_pageController = PageController(
initialPage: 0, keepPage: true, viewportFraction: 1);
});
},
只需将 TinderSwapCard 内容更新为
TinderSwapCard(
orientation: AmassOrientation.BOTTOM,
totalNum: 3,
stackNum: 3,
swipeEdge: 4.0,
maxWidth: MediaQuery.of(context).size.width * 0.9,
maxHeight: MediaQuery.of(context).size.width * 0.9,
minWidth: MediaQuery.of(context).size.width * 0.8,
minHeight: MediaQuery.of(context).size.width * 0.8,
cardBuilder: (context, index) {
/// to reset the [_pageController] to a new value
/// preventing the "multiple PageViews are attached to the same PageController" from appears
_pageController = PageController(
initialPage: 0, keepPage: true, viewportFraction: 1);
return PageView.builder(
scrollDirection: Axis.vertical,
controller: _pageController,
itemCount: lstUserImages.length,
onPageChanged: (page) => setState(() => _activeImageIndex = page),
itemBuilder: (context, index) {
return Image.network(
lstUserImages[index],
fit: BoxFit.cover,
);
});
},
cardController: cardController,
swipeCompleteCallback: (CardSwipeOrientation orientation, int index) {
/// at [TinderSwapCard] swiping ensure the [_pageController] scrolled to the zero index
setState(() {
_pageController.jumpToPage(0);
});
},
)
我有一个简单的PageView.builder:
@override
void initState() {
super.initState();
_pageController =
PageController(initialPage: 0, keepPage: true, viewportFraction: 1);
_pageController.addListener(() {
setState(() {
_activeImageIndex = _pageController.page!.toInt();
});
});
}
......
child: PageView.builder(
scrollDirection: Axis.vertical,
controller: _pageController,
itemCount: lstUserImages.length,
onPageChanged: (page) {
setState(() {
_activeImageIndex = page;
});
},
itemBuilder: (context, index) {
return GestureDetector(
child: Image.asset(
lstUserImages[index],
fit: BoxFit.cover,
),
);
}),
它工作正常,只是当我刷卡时,它不会将页面索引重置为 0(对于下一张卡)。因此,如果对于一张卡片,我正在查看第三张图片(索引 = 2),那么在我刷卡后,下一张卡片会加载显示第三张图片。
我有 onSwipeComplete 可用:
swipeCompleteCallback:
(CardSwipeOrientation orientation, int index) {
setState(() {
//WHAT DO I DO HERE SO THAT THE PAGE INDEX IS SET TO ZERO AGAIN..??
//isCardChanged = true;
//_pageController.jumpToPage(0);
_pageController = PageController(
initialPage: 0, keepPage: true, viewportFraction: 1);
});
},
只需将 TinderSwapCard 内容更新为
TinderSwapCard(
orientation: AmassOrientation.BOTTOM,
totalNum: 3,
stackNum: 3,
swipeEdge: 4.0,
maxWidth: MediaQuery.of(context).size.width * 0.9,
maxHeight: MediaQuery.of(context).size.width * 0.9,
minWidth: MediaQuery.of(context).size.width * 0.8,
minHeight: MediaQuery.of(context).size.width * 0.8,
cardBuilder: (context, index) {
/// to reset the [_pageController] to a new value
/// preventing the "multiple PageViews are attached to the same PageController" from appears
_pageController = PageController(
initialPage: 0, keepPage: true, viewportFraction: 1);
return PageView.builder(
scrollDirection: Axis.vertical,
controller: _pageController,
itemCount: lstUserImages.length,
onPageChanged: (page) => setState(() => _activeImageIndex = page),
itemBuilder: (context, index) {
return Image.network(
lstUserImages[index],
fit: BoxFit.cover,
);
});
},
cardController: cardController,
swipeCompleteCallback: (CardSwipeOrientation orientation, int index) {
/// at [TinderSwapCard] swiping ensure the [_pageController] scrolled to the zero index
setState(() {
_pageController.jumpToPage(0);
});
},
)