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

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


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

    You can also use the frameBuilder property. The good thing: you can implement your custom placeholder widget here.

    Image.network('https://example.com/my-image',
      height: 100,
      frameBuilder: (context, child, frame, _) {
        if (frame == null) {
          // fallback to placeholder
          return MyPlaceholderWidget();
        }
        return child;
      }
    )
    
    0 讨论(0)
  • 2021-02-13 03:32

    While large images load, show a fallback asset!

     new PlutoImage.networkWithPlaceholder("http://68.media.tumblr.com/f7e2e01128ca8eb2b9436aa3eb2a0a33/tumblr_ogwlnpSpcU1sikc68o1_1280.png", new Image.asset("assets/placeholder.png"));
    

    https://github.com/FaisalAbid/pluto

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

    You can Use FadeInImage.

    Use a placeholder from asset

    FadeInImage.assetNetwork(
                                  placeholder: "assets/images/image1.png",
                                  image:"URL"
                                    ),
    

    Use a placeholder from memory

    FadeInImage.memoryNetwork(
                                          placeholder: localImageBytes,
                                          image:"URL"
                                        ),
    
    0 讨论(0)
  • 2021-02-13 03:36

    There is a new official widget for this now!

    First, create a folder called assets in the project root directory.

    Then, mention the folder in pubspec.yaml file (also found in the project root directory):

    flutter:
      uses-material-design: true
      assets:
        - assets/
    

    You can put a picture there, for example, put this as ./assets/loading.gif.

    (If you changed files in assets folder, hot reload won't work. Make sure you restart the app entirely.)

    Now you can refer to the loading file in the code:

    FadeInImage.assetNetwork(
      placeholder: 'assets/loading.gif',
      image: 'https://github.com/flutter/website/blob/master/src/_includes/code/layout/lakes/images/lake.jpg?raw=true',
    );
    

    For more details: https://flutter.io/docs/cookbook/images/fading-in-images#from-asset-bundle

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

    Use a StateflWidget and you can add a listener to the ImageStream and override the initState to trigger a replacement between the local image and the one obtained from the internet when it is fully loaded.

    I have used a high resolution image to show the loading time:

      var _loadImage = new AssetImage(
          'assets/img/basic2-090_loader_loading-512.png');
      var _myEarth = new NetworkImage(
          "http://qige87.com/data/out/73/wp-image-144183272.png");
      bool _checkLoaded = true;
    
      @override
      void initState() {
        _myEarth.resolve(new ImageConfiguration()).addListener((_, __) {
          if (mounted) {
            setState(() {
              _checkLoaded = false;
            });
          }
        });
      }
    
    
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
            body: new Center(child: new Container(
              decoration: new BoxDecoration(shape: BoxShape.circle,),
              height: 80.0,
              width: 80.0,
              child: new CircleAvatar(
                backgroundColor: Theme
                    .of(context)
                    .scaffoldBackgroundColor,
                backgroundImage: _checkLoaded ? _loadImage : _myEarth,
              ),)
            )
        );
      }
    }
    
    0 讨论(0)
  • 2021-02-13 03:44

    There is a new cached_network_image package that has a "loading" and an "error" images. Along with auto image caching. https://stackoverflow.com/a/57493334/5502121

    You can set as a placeholder anything you want, for example from your assets use Image.asset('assets/images/my_placeholder.png')

    0 讨论(0)
提交回复
热议问题