如何在 carousel_slider 中使用资产图像?
How to work with asset images in carousel_slider?
这是我调用小部件并尝试传入带有图片的小部件列表的地方。
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Padding(
padding: const EdgeInsets.all(10),
child: CarouselSlider(
options: CarouselOptions(
aspectRatio: 2.0,
enlargeCenterPage: true,
enableInfiniteScroll: false,
initialPage: 2,
autoPlay: false,
),
items: imageSliders,
)),
这就是小部件列表。
final List<String> imgList = [
'images/shoppinglist_main.jpg',
'images/shoppinglist_main.jpg'
];
final List<Widget> imageSliders = imgList
.map((item) => Container(
child: Container(
margin: EdgeInsets.all(5.0),
child: ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(5.0)),
child: Stack(
children: <Widget>[
Image.asset(item, fit: BoxFit.cover, width: 1000.0),
Positioned(
bottom: 0.0,
left: 0.0,
right: 0.0,
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
Color.fromARGB(200, 0, 0, 0),
Color.fromARGB(0, 0, 0, 0)
],
begin: Alignment.bottomCenter,
end: Alignment.topCenter,
),
),
padding: EdgeInsets.symmetric(
vertical: 10.0, horizontal: 20.0),
child: Text(
'No. ${imgList.indexOf(item)} image',
style: TextStyle(
color: Colors.white,
fontSize: 20.0,
fontWeight: FontWeight.bold,
),
),
),
),
],
)),
),
))
.toList();
错误信息如下:
Null 不是 Future 的子类型。
当启用运行时空安全时,这将成为失败。
在 63 毫秒内重新启动应用程序。
══╡渲染图书馆捕获的例外╞═════════════════════════════════════════ ════════════════
在 performLayout() 期间抛出了以下断言:
RenderAspectRatio 具有无限约束。
此 RenderAspectRatio 的纵横比为 2,但宽度和宽度均不受限制
无限高度限制。因为这两个约束都是无界的,所以这个渲染对象没有
知道要消耗多少尺寸。
导致错误的相关小部件是:
纵横比
有人可以给我提示如何解决这个问题吗...
Because both constraints were unbounded, this render object doesn't
know how much size to consume.
您可以像这样分配约束:
SizedBox(
width: 250, //here your width
height: 250, //here your height
child: CarouselSlider(
options: CarouselOptions(
aspectRatio: 2.0,
enlargeCenterPage: true,
enableInfiniteScroll: false,Ï
initialPage: 2,
autoPlay: false,
),
items: imageSliders,
)
)
这是我调用小部件并尝试传入带有图片的小部件列表的地方。
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Padding(
padding: const EdgeInsets.all(10),
child: CarouselSlider(
options: CarouselOptions(
aspectRatio: 2.0,
enlargeCenterPage: true,
enableInfiniteScroll: false,
initialPage: 2,
autoPlay: false,
),
items: imageSliders,
)),
这就是小部件列表。
final List<String> imgList = [
'images/shoppinglist_main.jpg',
'images/shoppinglist_main.jpg'
];
final List<Widget> imageSliders = imgList
.map((item) => Container(
child: Container(
margin: EdgeInsets.all(5.0),
child: ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(5.0)),
child: Stack(
children: <Widget>[
Image.asset(item, fit: BoxFit.cover, width: 1000.0),
Positioned(
bottom: 0.0,
left: 0.0,
right: 0.0,
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
Color.fromARGB(200, 0, 0, 0),
Color.fromARGB(0, 0, 0, 0)
],
begin: Alignment.bottomCenter,
end: Alignment.topCenter,
),
),
padding: EdgeInsets.symmetric(
vertical: 10.0, horizontal: 20.0),
child: Text(
'No. ${imgList.indexOf(item)} image',
style: TextStyle(
color: Colors.white,
fontSize: 20.0,
fontWeight: FontWeight.bold,
),
),
),
),
],
)),
),
))
.toList();
错误信息如下:
Null 不是 Future 的子类型。 当启用运行时空安全时,这将成为失败。 在 63 毫秒内重新启动应用程序。 ══╡渲染图书馆捕获的例外╞═════════════════════════════════════════ ════════════════ 在 performLayout() 期间抛出了以下断言: RenderAspectRatio 具有无限约束。 此 RenderAspectRatio 的纵横比为 2,但宽度和宽度均不受限制 无限高度限制。因为这两个约束都是无界的,所以这个渲染对象没有 知道要消耗多少尺寸。 导致错误的相关小部件是: 纵横比
有人可以给我提示如何解决这个问题吗...
Because both constraints were unbounded, this render object doesn't know how much size to consume.
您可以像这样分配约束:
SizedBox(
width: 250, //here your width
height: 250, //here your height
child: CarouselSlider(
options: CarouselOptions(
aspectRatio: 2.0,
enlargeCenterPage: true,
enableInfiniteScroll: false,Ï
initialPage: 2,
autoPlay: false,
),
items: imageSliders,
)
)