How do I tell when a NetworkImage has finished loading?

后端 未结 5 1962
野的像风
野的像风 2020-12-29 10:36

I have a NetworkImage and I\'d like to know when it\'s finished loading. How do I do that?

相关标签:
5条回答
  • 2020-12-29 10:57

    You Could use the loadingBuilder which provides feature from flutter for Image.Network

    The following sample uses loadingBuilder to show a CircularProgressIndicator while an image loads over the network.

       Image.network(
          'https://example.com/image.jpg',
          loadingBuilder: (BuildContext context, Widget child,
              ImageChunkEvent loadingProgress) {
            if (loadingProgress == null) return child;
            return Center(
              child: CircularProgressIndicator(
                value: loadingProgress.expectedTotalBytes != null
                    ? loadingProgress.cumulativeBytesLoaded /
                    loadingProgress.expectedTotalBytes
                    : null,
              ),
            );
          },
        )
    

    0 讨论(0)
  • 2020-12-29 11:02

    I find this method in flutter official demo,wish help you.

    import 'dart:async';
    import 'package:flutter/material.dart';
    
    void _imageLoad() async {
    
        String imageName = "";
    
        Image downloadImage = new Image.network(imageName);
    
        final ImageStream stream = downloadImage.image.resolve(ImageConfiguration.empty);
        final Completer<void> completer = Completer<void>();
        stream.addListener((ImageInfo info, bool syncCall) => completer.complete());
        await completer.future;
        if (mounted) {
          //do sth
        }
    }
    
    0 讨论(0)
  • 2020-12-29 11:08

    You can resolve to get an ImageStream and addListener to the ImageStream

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(new MyApp());
    }
    
    class MyApp extends StatefulWidget {
      State createState() => new MyAppState();
    }
    
    class MyAppState extends State<MyApp> {
      Image _image = new Image.network(
        'https://flutter.io/images/flutter-mark-square-100.png',
      );
      bool _loading = true;
    
      @override
      void initState() {
        _image.image.resolve(new ImageConfiguration()).addListener((_, __) {
          if (mounted) {
            setState(() {
              _loading = false;
            });
          }
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return new MaterialApp(
          home: new Scaffold(
            body: new Center(
              child: _loading ? new Text('Loading...') : _image,
            ),
          ),
        );
      }
    }
    
    0 讨论(0)
  • 2020-12-29 11:08

    thank you for your comment thats help to resolve the situation that how to know if the image is loaded or not hope that help

    I use a StatefulWidget need a editing depend on your AffichScreen

    situation :

    -i have an url that i enter 
     -if url is correct affich the image if not affich an icon
     -if empty affich a Text()
     -precacheImage check if the url is correct if not give an error 
     -and change _loadingimage(bool) to false to affich the icon eror
     -i use a NetworkImage to check with precacheImage and before 
     -affich use a Image.network 
    
    bool _loadingimage;
    ImageProvider _image;
    Image _imagescreen;
    
    @override
      void initState() {
        _loadingimage = true;
        _imageUrlfocusNode.addListener(_updateImageUrl);
        super.initState();
      }
    
       @override
      void dispose() {
        _imageUrlfocusNode.removeListener(_updateImageUrl);
        _quantityfocusNode.dispose();
        _imageUrlConroller.dispose();
        _imageUrlfocusNode.dispose();
        super.dispose();
      }
    
      void _updateImageUrl() {
        setState(() {
          _image = NetworkImage(_imageUrlConroller.text);
        });
        if (!_imageUrlfocusNode.hasFocus) {
          if (_imageUrlConroller.text.isNotEmpty) {
            setState(() {
              loadimage();
            });
          }
        }
      }
    
      void loadimage() {
        _loadingimage = true;
        precacheImage(_image, context, onError: (e, stackTrace) {
          // log.fine('Image ${widget.url} failed to load with error $e.');
          print('error $e');
          setState(() {
            _loadingimage = false;
            print(_loadingimage);
          });
        });
        if (_loadingimage == true) {
          _imagescreen = Image.network(
            _imageUrlConroller.text,
            fit: BoxFit.fill,
          );
        }
      }
    
    
    
      Container(
                                  width: 100,
                                  height: 100,
                                  margin: EdgeInsets.only(top: 13, right: 11),
                                  decoration: BoxDecoration(
                                    border: Border.all(
                                      width: 1,
                                      color: Colors.grey,
                                    ),
                                  ),
                                  child:_imageUrlConroller.text.isEmpty
                                          ? Text('enter an url')
                                          : !_loadingimage
                                              ? Container(
                                                  child: Icon(Icons.add_a_photo),
                                                )
                                              : Container(
                                                  child: _imagescreen,
                                                ),
                                ),
    
    0 讨论(0)
  • 2020-12-29 11:12

    You can do it, with an ImageStreamListener. The first Parameter of the ImageStreamListener is an ImageListener Callback which is called, when the image is fully loaded.

     var _image = NetworkImage("URL");
    
     _image.resolve(ImageConfiguration()).addListener(
        ImageStreamListener(
          (info, call) {
            print('Networkimage is fully loaded and saved');
            // do something
          },
        ),
      );
    
    0 讨论(0)
提交回复
热议问题