Ad

Use Offline Image For Slider In Flutter

- 1 answer

good evening

i have use this sample of slider image and it's worked correctly but i have little problem, i want to use my local image on this code but when the project has been running , it's show me blue rectangle , how can i use my local image on this slider? thank you here is the Code:

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

     final String title = "Carousel Demo";

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

    class CarouselDemoState extends State<CarouselDemo> {

    CarouselSlider carouselSlider;
    int _current = 0;
     List imgList = [
    new AssetImage('images/main-slider/1.jpg'),
    new AssetImage('images/main-slider/2.jpg'),
    new AssetImage('images/main-slider/3.jpg'),
    new AssetImage('images/main-slider/4.jpg'),
    ];


     @override
     Widget build(BuildContext context) {
       return Scaffold(
      body: Container(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            carouselSlider = CarouselSlider(
              height: 400.0,
              initialPage: 0,
                                onPageChanged: (index) {
                setState(() {
                  _current = index;
                });
              },
              items: imgList.map((imgUrl) {
                return Builder(
                  builder: (BuildContext context) {
                    return Container(
                      width: MediaQuery.of(context).size.width,
                      margin: EdgeInsets.symmetric(horizontal: 10.0),
                      decoration: BoxDecoration(
                        color: Colors.green,
                      ),
                      child: Image.network(
                        imgUrl,
                        fit: BoxFit.fill,
                      ),
                    );
                  },
                );
              }).toList(),
            ),
        ),
      ),
    );
  }
Ad

Answer

The problem is that imgList is a List<AssetImage> but your are trying to display the images with a Image.network.

Image.network creates a widget that displays an [ImageStream] obtained from the network.

What you need is creat a List<Sring> where the String will be the the paths relative to the pubspec.yaml file. Also do not use Image.Network you should use Image.assest because you want to creates a widget that displays an [ImageStream] obtained from an asset bundle.

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

  final String title = "Carousel Demo";

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

class CarouselDemoState extends State<CarouselDemo> {
  CarouselSlider carouselSlider;
  int _current = 0;
  List imgList = [
    'images/main-slider/1.jpg',
    'images/main-slider/2.jpg',
    'images/main-slider/3.jpg',
    'images/main-slider/4.jpg'
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            carouselSlider = CarouselSlider(
              height: 400.0,
              initialPage: 0,
              onPageChanged: (index) {
                setState(() {
                  _current = index;
                });
              },
              items: imgList.map((imgUrl) {
                return Builder(
                  builder: (BuildContext context) {
                    return Container(
                      width: MediaQuery.of(context).size.width,
                      margin: EdgeInsets.symmetric(horizontal: 10.0),
                      decoration: BoxDecoration(
                        color: Colors.green,
                      ),
                      child: Image.asset(
                        imgUrl,
                        fit: BoxFit.fill,
                      ),
                    );
                  },
                );
              }).toList(),
            ),
          ],
        ),
      ),
    );
  }
}
Ad
source: stackoverflow.com
Ad