How to load images with image.file

后端 未结 11 2135
星月不相逢
星月不相逢 2021-01-01 10:41

I can\'t seem to simply load an image from the hard drive to the screen. Image.network seems straightforward. But I can\'t figure out how to use Image or Image.file. Imag

11条回答
  •  一生所求
    2021-01-01 11:00

    Flutter contains assert section inside pubspec.yaml where it contains asserts of your application. eg:

    assets:
        - data_repo/img/ic_launcher.png
    

    1. With pubspec.yaml:

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(new MaterialApp(home: LoadLocalImage()));
    }
    
    class LoadLocalImage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          appBar: new AppBar(
            title: new Text("Load Local Image"),
          ),
          body: new Container(
            decoration: new BoxDecoration(
                image: new DecorationImage(
                    image: new AssetImage('data_repo/img/ic_launcher.png'), fit: BoxFit.cover)),
          ),
        );
      }
    }
    

    2. With Image.asset:

    import 'package:flutter/material.dart';
    
        void main() {
          runApp(new MaterialApp(home: LoadLocalImage()));
        }
    
        class LoadLocalImage extends StatelessWidget {
          @override
          Widget build(BuildContext context) {
            return new Scaffold(
                appBar: new AppBar(
                  title: new Text("Load Local Image"),
                ),
                body: Image.asset(
                  'data_repo/img/ic_launcher.png',
                  fit: BoxFit.cover,
                ));
          }
        }
    

    Folder Structure: Output:

    Please refer below link for more description:

    https://flutter.dev/docs/cookbook/images/network-image

提交回复
热议问题