How to Show an Local image till the NetworkImage() Loads Up in flutter?

前端 未结 9 998
忘掉有多难
忘掉有多难 2021-02-13 03:15
            new CircleAvatar(
                              backgroundColor: Colors.black87,
                              backgroundImage: new NetworkImage(url),
               


        
相关标签:
9条回答
  • 2021-02-13 03:54

    You may want to try a FadeInImage wrapped in a ClipOval. FadeInImage provides a placeholder property you can use while the network image is loading.

    Note: ClipOval can be expensive if you do it a lot, so use it sparingly.

    0 讨论(0)
  • 2021-02-13 03:56

    Two way to solve your problem

    1) Using Image.network : If you want to show progressbar,simmer or any other widget when image loading.

      Image.network(
          "URL",
          fit: BoxFit.cover,
          loadingBuilder: (BuildContext ctx, Widget child, ImageChunkEvent loadingProgress) {
            if (loadingProgress == null) {
              return child;
            }else {
              return Center(
                child: CircularProgressIndicator(
                  valueColor: AlwaysStoppedAnimation<Color>(Colors.green),
                ),
              );
            }
          },       
      )
    



    2) Using FadeInImage : If you want to display your local image when network image loading

     FadeInImage.assetNetwork( 
        image:"URL",
        placeholder:"assets/loading.png" // your assets image path
        fit: BoxFit.cover,
      )
    
    0 讨论(0)
  • 2021-02-13 03:56

    I developed a package named flutter_url_image_load_fail to define the loading and failed to load widgets:

    LoadImageFromUrl(
        'https://upload.wikimedia.org/wikipedia/commons/1/17/Google-flutter-logo.png', //Image URL to load
        (image) => image, //What widget returns when the image is loaded successfully
        () => Text('Loading...'), //What widget returns when the image is loading
        (IRetryLoadImage retryLoadImage, code , message){ //What widget returns when the image failed to load
            return RaisedButton(
                child: Text('Try Again'),
                onPressed: (){
                    retryLoadImage.retryLoadImage(); //Call this method to retry load the image when it failed to load
                },
            );
        },
        requestTimeout: Duration(seconds: 5) //Optionally set the timeout
    )
    
    0 讨论(0)
提交回复
热议问题