如何在 flutter 的单页滑块中显示两个图像
how to display two images in a single page of slider in flutter
我是 Flutter 社区的新手。我曾将图像滑块插件用于纵向图像,将 carousal pro 插件用于横向图像滑块,在将滑块分成两个图像时遇到问题,在滑动时无法获得不同的索引图像,并且一切正常。我想显示图像列表中的图像。这里需要自动播放功能。提前致谢。
已经尝试过下面的代码
//method
List<T> map<T>(List list, Function handler) {
List<T> result = [];
for (var i = 0; i < list.length; i++) {
result.add(handler(i, list[i]));
}
return result;
}
//swiper
swiperTwoImageShow() {
return isAutoPlay
? Center(
child: Carousel(
showIndicator: false,
autoplay: true,
images: map<Widget>(widget.imageUrl, (index, i) {
String tempOddImage;
String tempEvenImage;
index % 2 == 0 ? tempEvenImage = i : tempOddImage = i;
return Row(
children: [
Expanded(
flex: 1,
child: CachedNetworkImage(
imageUrl: tempOddImage ?? tempEvenImage,
fit: BoxFit.contain),
),
Expanded(
flex: 1,
child: CachedNetworkImage(
imageUrl: tempEvenImage ?? tempOddImage,
fit: BoxFit.contain),
),
],
);
}),
),
)
: Center(
child: ImagesSlider(
items: map<Widget>(oddArray, (index, i) {
return Row(
children: [
Expanded(
flex: 1,
child: Container(
width: Utils.getDeviceWidth(context),
child: CachedNetworkImage(
imageUrl: i, fit: BoxFit.contain),
),
),
Expanded(
flex: 1,
child: Container(
width: Utils.getDeviceWidth(context),
child: CachedNetworkImage(
imageUrl: i, fit: BoxFit.contain),
),
),
],
);
}),
autoPlay: false,
viewportFraction: 1.0,
aspectRatio: 1.0,
distortion: false,
updateCallback: (index) {
setState(() {
_current = index;
});
},
),
);
}
尝试使用下面的代码:
swipeTwoImageShow() {
return isReloading
? Container()
: Align(
alignment: Alignment.center,
child: CarouselSlider.builder(
options: CarouselOptions(
initialPage: widget.index,
aspectRatio:
MediaQuery.of(context).orientation == Orientation.portrait
? 1
: (MediaQuery.of(context).size.width / 3) /
(MediaQuery.of(context).size.height / 2.2),
viewportFraction: 1,
autoPlay: isAutoPlay),
itemCount: summaryShow
? ((widget.shortDescription.sommaire.length - 1) / 2).round()
: ((widget.imageUrl.length - 1) / 2).round(),
carouselController: carouselController,
itemBuilder: (context, index) {
final int first = index * 2;
final int second = first + 1;
return Row(
children: [first, second].map((idx) {
return Container(
height: Utils.getDeviceHeight(context),
width: MediaQuery.of(context).orientation ==
Orientation.portrait
? Utils.getDeviceWidth(context) / 2
: Utils.getDeviceWidth(context) / 3,
child: CachedNetworkImage(
imageUrl: summaryShow
? widget.shortDescription.sommaire[idx].pageUrl
: widget.imageUrl[idx],
fit: MediaQuery.of(context).orientation ==
Orientation.landscape
? BoxFit.fill
: BoxFit.contain,
height: MediaQuery.of(context).orientation ==
Orientation.portrait
? Utils.getDeviceHeight(context)
: Utils.getDeviceHeight(context) / 2,
width: MediaQuery.of(context).orientation ==
Orientation.portrait
? Utils.getDeviceWidth(context) / 2
: Utils.getDeviceWidth(context) / 3,
),
);
}).toList(),
);
},
),
);
}
我是 Flutter 社区的新手。我曾将图像滑块插件用于纵向图像,将 carousal pro 插件用于横向图像滑块,在将滑块分成两个图像时遇到问题,在滑动时无法获得不同的索引图像,并且一切正常。我想显示图像列表中的图像。这里需要自动播放功能。提前致谢。
已经尝试过下面的代码
//method
List<T> map<T>(List list, Function handler) {
List<T> result = [];
for (var i = 0; i < list.length; i++) {
result.add(handler(i, list[i]));
}
return result;
}
//swiper
swiperTwoImageShow() {
return isAutoPlay
? Center(
child: Carousel(
showIndicator: false,
autoplay: true,
images: map<Widget>(widget.imageUrl, (index, i) {
String tempOddImage;
String tempEvenImage;
index % 2 == 0 ? tempEvenImage = i : tempOddImage = i;
return Row(
children: [
Expanded(
flex: 1,
child: CachedNetworkImage(
imageUrl: tempOddImage ?? tempEvenImage,
fit: BoxFit.contain),
),
Expanded(
flex: 1,
child: CachedNetworkImage(
imageUrl: tempEvenImage ?? tempOddImage,
fit: BoxFit.contain),
),
],
);
}),
),
)
: Center(
child: ImagesSlider(
items: map<Widget>(oddArray, (index, i) {
return Row(
children: [
Expanded(
flex: 1,
child: Container(
width: Utils.getDeviceWidth(context),
child: CachedNetworkImage(
imageUrl: i, fit: BoxFit.contain),
),
),
Expanded(
flex: 1,
child: Container(
width: Utils.getDeviceWidth(context),
child: CachedNetworkImage(
imageUrl: i, fit: BoxFit.contain),
),
),
],
);
}),
autoPlay: false,
viewportFraction: 1.0,
aspectRatio: 1.0,
distortion: false,
updateCallback: (index) {
setState(() {
_current = index;
});
},
),
);
}
尝试使用下面的代码:
swipeTwoImageShow() {
return isReloading
? Container()
: Align(
alignment: Alignment.center,
child: CarouselSlider.builder(
options: CarouselOptions(
initialPage: widget.index,
aspectRatio:
MediaQuery.of(context).orientation == Orientation.portrait
? 1
: (MediaQuery.of(context).size.width / 3) /
(MediaQuery.of(context).size.height / 2.2),
viewportFraction: 1,
autoPlay: isAutoPlay),
itemCount: summaryShow
? ((widget.shortDescription.sommaire.length - 1) / 2).round()
: ((widget.imageUrl.length - 1) / 2).round(),
carouselController: carouselController,
itemBuilder: (context, index) {
final int first = index * 2;
final int second = first + 1;
return Row(
children: [first, second].map((idx) {
return Container(
height: Utils.getDeviceHeight(context),
width: MediaQuery.of(context).orientation ==
Orientation.portrait
? Utils.getDeviceWidth(context) / 2
: Utils.getDeviceWidth(context) / 3,
child: CachedNetworkImage(
imageUrl: summaryShow
? widget.shortDescription.sommaire[idx].pageUrl
: widget.imageUrl[idx],
fit: MediaQuery.of(context).orientation ==
Orientation.landscape
? BoxFit.fill
: BoxFit.contain,
height: MediaQuery.of(context).orientation ==
Orientation.portrait
? Utils.getDeviceHeight(context)
: Utils.getDeviceHeight(context) / 2,
width: MediaQuery.of(context).orientation ==
Orientation.portrait
? Utils.getDeviceWidth(context) / 2
: Utils.getDeviceWidth(context) / 3,
),
);
}).toList(),
);
},
),
);
}