Home > Back-end >  How can I show an asset Image while NetworkImage is loading
How can I show an asset Image while NetworkImage is loading

Time:01-18

I am not able to show a circular loading indicator or an image while flutter is loading the image from the network.

image: DecorationImage(
                      image: NetworkImage(
                        Juegos_de_siempre[index].url_foto,
                      ),
                      fit: BoxFit.fill,
                    ),

CodePudding user response:

Flutter's documentation suggested the usage of FadeInImage for the placeholder functionality. Here's a link to an example on Flutter's documentation.

You cannot use FadeInImage inside a DecorationImage, so you have to wrap it inside a Stack instead like shown in this thread.

CodePudding user response:

Use CacheNetworkImage Package.Click here.

This is sample code.

CachedNetworkImage(
                  width: 100,
                  height: 100,
                  fit: BoxFit.cover,
                  imageUrl: imageUrl,
                  placeholder: (context, url) => const Center(
                    child: CupertinoActivityIndicator(),
                  ), // replace with your asset image
                  errorWidget: (context, url, error) =>
                      const Icon(Icons.error),
                ),    

CodePudding user response:

Image.network widget has loadingBuilder property which you can make use of while fetching data (credit goes to https://stackoverflow.com/a/59151953/20446596 ) : -

Image.network(
"www.abcImage.com", // image url
fit: BoxFit.fill,
loadingBuilder: (BuildContext context, Widget child, 
  ImageChunkEvent? loadingProgress) {
if (loadingProgress == null) return child;
return Center(
  child: CircularProgressIndicator(   // circular loader
    value: loadingProgress.expectedTotalBytes != null
        ? loadingProgress.cumulativeBytesLoaded /
            loadingProgress.expectedTotalBytes!
        : null,
  ),
);
},
),

You can use any widget in loadingBuilder in place of CircularProgressIndicator

Complete Code : -

import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      debugShowCheckedModeBanner: false,
      title: _title,
      home: NetworkImage(),
    );
  }
}

class NetworkImage extends StatefulWidget {
  const NetworkImage({Key? key}) : super(key: key);

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

class _NetworkImageState extends State<NetworkImage> {
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
          body: Center(
        child: Image.network(
          "https://wallpapers.com/images/file/4k-lamborghini-logo-on-steering-wheel-s4gxuxvefw3qeqws.jpg", // image url
          fit: BoxFit.fill,
          loadingBuilder: (BuildContext context, Widget child,
              ImageChunkEvent? loadingProgress) {
            if (loadingProgress == null) return child;
            return Center(
              child: CircularProgressIndicator(
                // circular loader
                value: loadingProgress.expectedTotalBytes != null
                    ? loadingProgress.cumulativeBytesLoaded /
                        loadingProgress.expectedTotalBytes!
                    : null,
              ),
            );
          },
        ),
      )),
    );
  }
}

Output : -

  • Related