【问题标题】:unable to load Image list in carousel无法在轮播中加载图像列表
【发布时间】:2021-04-16 03:16:45
【问题描述】:

我试图在轮播滑块中传递图像列表视图,但它显示错误。所有图片都可以正常工作,但是当我在列表中传递它时,它显示无法加载。

以下是轮播滑块的代码 -:

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



class UserProfile extends StatefulWidget {
  UserProfile() : super();

  @override
  _UserProfileState createState() => _UserProfileState();
}

class _UserProfileState extends State<UserProfile> {


  CarouselSlider carouselSlider;

  List imgList = [
    'assets/user1.jpg',
    'assets/f.png',
    'assets/f2.jpg',
  ];




  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: buildAppBar(),
      body: Container(
        child: Padding(
          padding: const EdgeInsets.only(left: 30, right: 30, top: 10),
          child: SingleChildScrollView(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              mainAxisAlignment: MainAxisAlignment.center,
              mainAxisSize: MainAxisSize.min,
              children: [


                Flexible(
                  fit: FlexFit.loose,
                  flex: 5,
                  child: Container(
                    height: 400,
                    child: ListView.builder(
                      itemCount: imgList.length,
                      itemBuilder: (BuildContext context, int index){
                        return Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[


                            CarouselSlider(
                                options: CarouselOptions(
                                  height: 385.0,
                                  enlargeCenterPage: true,
                                  autoPlay: true,
                                  aspectRatio: 16 / 9,
                                  autoPlayCurve: Curves.easeInBack,
                                  enableInfiniteScroll: true,
                                  autoPlayAnimationDuration: Duration(milliseconds: 900),
                                  viewportFraction: 0.8,
                                ),
                                items: [
                                  Padding(
                                    padding: const EdgeInsets.only(left: 30.0),
                                    child: Container(
                                      height: 250,
                                      margin: EdgeInsets.symmetric(vertical: 0),
                                      decoration: BoxDecoration(
                                        color: Colors.black,
                                        borderRadius: BorderRadius.circular(16),
                                        boxShadow: [
                                          BoxShadow(
                                            color: Colors.blueGrey[100],
                                            blurRadius: 3,
                                            spreadRadius: 3,
                                          ),
                                        ],
                                      ),
                                      child: Image.asset(
                                        '${imgList[index]}',
                                        fit: BoxFit.fill,
                                      ),
                                    ),
                                  ),
                                ]
                            ),


                          ],
                        );
                      },
                    ),
                  ),
                ),


                Flexible(
                  fit: FlexFit.loose,
                  flex: 4,
                  child: Placeholder(),
                ),

              ],
            ),
          ),
        ),
      ),
    );
  }



  Widget buildAppBar() => AppBar(
    elevation: 15,
    backgroundColor: Colors.white,
    centerTitle: true,
    leading: InkWell(
      child: Icon(
        Icons.arrow_back_ios,
        color: Colors.pinkAccent,
      ),
      onTap: () {
        Navigator.pop(context);
      },
    ),
    title: Text(
      'Carousel Slider',
      style: TextStyle(
        fontSize: 25,
        color: Colors.black,
      ),
    ),
  );
}

这显示错误-:无法加载图像列表 ['assets/user1.jpg','assets/f.png','assets/f2.png',]

乐于接受建议。

edit -: 文件夹结构图片

【问题讨论】:

  • 我更正了轮播中已经显示但仍然只有第一张图像
  • 是轮播中显示的图像,是您的 imgList 中的最后一张图像吗?
  • 不,这是我列表中的第一张图片
  • 好的,我会在一段时间内为此编写解决方案,请尝试一下,可能会有所帮助。

标签: flutter flutter-layout carousel flutter-dependencies


【解决方案1】:

你是否在 pubspec.yaml 中添加了 assets/user1.jpg,assets/f.png,assets/f2.png

示例链接: enter link description here

示例:

assets:
- assets/user1.jpg
- assets/f.png
- assets/f2.png

【讨论】:

  • 还是不行?你能发布你的图片文件夹结构吗
  • 文件夹结构是否与此有关?
  • 你的代码对我有用。图片正在加载你能放 pubspec.yaml
【解决方案2】:

删除 ListView 小部件

并将其作为项目传递到您的 CarouselSlider 项目中

items: imgList.map((item) {
return Builder(
builder: (BuildContext context) {
return Padding(
padding: const EdgeInsets.only(left: 30.0),
child: Container(
height: 250,
margin: EdgeInsets.symmetric(vertical: 0),
decoration: BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.circular(16),
boxShadow: [
BoxShadow(
color: Colors.blueGrey[100],
blurRadius: 3,
spreadRadius: 3,
),
],
),
child: Image.asset(
item,
fit: BoxFit.fill,
),
),
),
},
);
}).toList(),

【讨论】:

  • 以下是抛出的错误 -: 抛出以下 _TypeError 构建 Builder(dirty): type 'String' is not a subtype of type 'int'
【解决方案3】:

这是因为您没有在 Carousel 项目中迭代 imgList

只需替换此代码为您的CarouselSlider

                        CarouselSlider(
                          options: CarouselOptions(
                            height: 385.0,
                            enlargeCenterPage: true,
                            autoPlay: true,
                            aspectRatio: 16 / 9,
                            autoPlayCurve: Curves.easeInBack,
                            enableInfiniteScroll: true,
                            autoPlayAnimationDuration:
                                Duration(milliseconds: 900),
                            viewportFraction: 0.8,
                          ),
                          items: imgList.map((item) {
                            return Padding(
                              padding: const EdgeInsets.only(left: 30.0),
                              child: Container(
                                height: 250,
                                margin: EdgeInsets.symmetric(vertical: 0),
                                decoration: BoxDecoration(
                                  color: Colors.white,
                                  borderRadius: BorderRadius.circular(16),
                                  boxShadow: [
                                    BoxShadow(
                                      color: Colors.blueGrey[100],
                                      blurRadius: 3,
                                      spreadRadius: 3,
                                    ),
                                  ],
                                ),
                                  child: Image.asset(
                                    '$item',
                                    fit: BoxFit.fill,
                                  ),
                              ),
                            );
                          }).toList(),
                        ),

在这里,我在CarouselSlideritems 中添加了imgList,以在imgList 项目上迭代您的CarouselSlider

【讨论】:

  • 谢谢你这工作我也得到了我的错误。
猜你喜欢
  • 2016-10-13
  • 1970-01-01
  • 1970-01-01
  • 2015-03-22
  • 1970-01-01
  • 1970-01-01
  • 2019-09-05
  • 2014-12-22
  • 1970-01-01
相关资源
最近更新 更多