How to add image in Flutter

前端 未结 8 1940
渐次进展
渐次进展 2020-12-01 01:10

I am developing Flutter app for the first time.. I have an issue in adding a image. I have a below questions :

  1. Where to create images folder?
  2. Where to
相关标签:
8条回答
  • 2020-12-01 01:39

    their is no need to create asset directory and under it images directory and then you put image. Better is to just create Images directory inside your project where pubspec.yaml exist and put images inside it and access that images just like as shown in tutorial/documention

    assets: - images/lake.jpg // inside pubspec.yaml

    0 讨论(0)
  • 2020-12-01 01:41
    1. Create images folder in root level of your project.

    2. Drop your image in this folder, it should look like

    3. Go to your pubspec.yaml file, add assets header and pay close attention to all the spaces.

      flutter:
      
        uses-material-design: true
      
        # add this
        assets:
          - images/profile.jpg
      
    4. Tap on Packages get at the top right corner of the IDE.

    5. Now you can use your image anywhere using

      Image.asset("images/profile.jpg")
      
    0 讨论(0)
  • 2020-12-01 01:44

    An alternative way to put images in your app (for me it just worked that way):

    1 - Create an assets/images folder

    2 - Add your image to the new folder

    3 - Register the assets folder in pubspec.yaml

    4 - Use this code:

    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
    
        var assetsImage = new AssetImage('assets/images/mountain.jpg'); //<- Creates an object that fetches an image.
        var image = new Image(image: assetsImage, fit: BoxFit.cover); //<- Creates a widget that displays an image.
    
        return MaterialApp(
          home: Scaffold(
            appBar: AppBar(
              title: Text("Climb your mountain!"),
              backgroundColor: Colors.amber[600], //<- background color to combine with the picture :-)
            ),
            body: Container(child: image), //<- place where the image appears
          ),
        );
      }
    }
    

    0 讨论(0)
  • 2020-12-01 01:50

    When you adding assets directory in pubspec.yaml file give more attention in to spaces

    this is wrong

    flutter:
       assets:
        - assets/images/lake.jpg
    

    This is the correct way,

    flutter:
      assets:
        - assets/images/
    
    0 讨论(0)
  • 2020-12-01 01:51

    How to include images in your app

    1. Create an assets/images folder

    • This should be located in the root of your project, in the same folder as your pubspec.yaml file.
    • In Android Studio you can right click in the Project view
    • You don't have to call it assets or images. You don't even need to make images a subfolder. Whatever name you use, though, is what you will regester in the pubspec.yaml file.

    2. Add your image to the new folder

    • You can just copy your image into assets/images. The relative path of lake.jpg, for example, would be assets/images/lake.jpg.

    3. Register the assets folder in pubspec.yaml

    • Open the pubspec.yaml file that is in the root of your project.
    • Add an assets subsection to the flutter section like this:

      flutter:
        assets:
          - assets/images/lake.jpg
      
    • If you have multiple images that you want to include then you can leave off the file name and just use the directory name (include the final /):

      flutter:
        assets:
          - assets/images/
      

    4. Use the image in code

    • Get the asset in an Image widget with Image.asset('assets/images/lake.jpg').
    • The entire main.dart file is here:

      import 'package:flutter/material.dart';
      
      void main() => runApp(MyApp());
      
      class MyApp extends StatelessWidget {
        @override
        Widget build(BuildContext context) {
          return MaterialApp(
            home: Scaffold(
              appBar: AppBar(
                title: Text("Image from assets"),
              ),
              body: Image.asset('assets/images/lake.jpg'), //   <--- image
            ),
          );
        }
      }
      

    5. Restart your app

    When making changes to pubspec.yaml I find that I often need to completely stop my app and restart it again, especially when adding assets. Otherwise I get a crash.

    Running the app now you should have something like this:

    Further reading

    • See the documentation for how to do things like provide alternate images for different densities.
    0 讨论(0)
  • 2020-12-01 01:53

    The problem is in your pubspec.yaml, here you need to delete the last comma.

    uses-material-design: true,
    
    0 讨论(0)
提交回复
热议问题