【发布时间】:2025-12-18 03:05:02
【问题描述】:
对不起我的英语我希望我能准确地解释我想要什么。如果我在 else 部分创建 imageCount 变量,则设置状态不会发生。但是如果我在全局范围内创建它,它会变为设置状态,但这次它会在每个图像上发生变化。这是我的代码,就像 Instagram 一样,帖子即将发布,它们在另一个下方列出。我也在尝试打印图像编号,现在我应该如何定义这个变量?无论用户滚动浏览哪个帖子,其数量都会增加 1。
Widget _imageDisplay(int index) {
if (postsList == null) {
return null;
} else {
int imageCount=1; //this variable
return Container(
color: Colors.white,
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height * 0.5,
child: Stack(children: [
PhotoViewGallery(
onPageChanged: (index) {
setState(() {
imageCount = index + 1;
});
},
customSize: MediaQuery.of(context).size,
backgroundDecoration: BoxDecoration(color: Colors.white),
pageOptions: _photoPageOpt(index),
),
Align(
alignment: Alignment.topRight,
child: Text(imageCount.toString() +
'/' +
postsList[index].images.length.toString()))
]),
);
}
【问题讨论】: