如何在使用"CarouselSlider"时调整图像相对于移动尺寸的图像大小,并在颤振中将按钮文本放在该图像上?



我正在尝试为我的应用程序创建介绍演练滑块,但我无法正确执行操作。使用 pub.dev 提供carousel_slider。我无法将图像填充到整个手机屏幕。它在左右两侧都留下了一些空白。

使用轮播专业版,我无法在滑动图像上放置按钮或文本。我花了几个小时在一些小问题上,但无法实现我想要的。

这是代码

import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';

void main() {
runApp(MaterialApp(
home: MyApp(), // Making initializing home sceen
));
}
List<String> imgList = [
"lib/assets/images/sunset.jpg",
"lib/assets/images/sample3.jpg",
"lib/assets/images/sample2.jpg",
"lib/assets/images/sunshine.jpg",
"lib/assets/images/leaf.png",

];
int current = 0;
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
// var _userName = TextEditingController();
@override
Widget build(BuildContext context) {
Container(
child: ImageCarousel(),
);
}

class ImageCarousel extends StatefulWidget {
@override
_ImageCarouselState createState() => _ImageCarouselState();
}
class _ImageCarouselState extends State<ImageCarousel> {
@override
Widget build(BuildContext context) {
// >>>>>>>>>>>>  C A R O U S E L    S L I D E R    C O D E
return CarouselSlider(
height: double.infinity,
initialPage: 0,
enableInfiniteScroll: false,
onPageChanged: (index) {
setState(() {
current = index;
});
},
items: imgList.map((imgUrl) {
return Builder(
builder: (BuildContext context) {
return Container(
//width: MediaQuery.of(context).size.width,
width: double.infinity,
margin: EdgeInsets.symmetric(horizontal: 1),
decoration: BoxDecoration(
color: Colors.green,
),
child: Image(
image: AssetImage(
imgUrl,
),
fit: BoxFit.cover,
),
);
},
);
}).toList(),
);
}
}

以下是屏幕截图,用于更多说明

截图 1 截图 2

使用 pub.dev 提供Carousel_Pro,如何在图像上放置文本或按钮。

class ImageCarousel extends StatefulWidget {
@override
_ImageCarouselState createState() => _ImageCarouselState();
}
class _ImageCarouselState extends State<ImageCarousel> {
@override
Widget build(BuildContext context) {
return Carousel(
images: [
AssetImage("lib/assets/images/sample2.jpg"),
AssetImage("lib/assets/images/sample3.jpg"),
]
);

截图 3 截图 4

顺便说一句,我已经在pubspec.yaml中安装了所有软件包和映像。

carousel_slider填满整个页面集:viewportFraction: 1.0

要将内容放在映像创建堆栈的顶部,请执行以下操作:

CarouselSlider(
height: double.infinity,
viewportFraction: 1.0,
initialPage: 0,
enableInfiniteScroll: false,
items: imgList.map((imgUrl) {
return Stack(
children: <Widget>[
Image(
height: double.infinity,
image: NetworkImage(
imgUrl,
),
fit: BoxFit.cover,
),
Text("TEST")
],
);
}).toList(),
))

最新更新