Industrial Training




Flutter Images


In this section, we are going to see how we can display images in Flutter. When you create an app in Flutter, it includes both code and assets (resources). An asset is a file, which is bundled and deployed with the app and is accessible at runtime. The asset can include static data, configuration files, icons, and images. The Flutter supports many image formats, such as JPEG, WebP, PNG, GIF, animated WebP/GIF, BMP, and WBMP.
Displaying images is the fundamental concept of most of the mobile apps. Flutter has an Image widget that allows displaying different types of images in the mobile application.


How to display the image in Flutter


To display an image in Flutter, do the following steps:
Step 1: First, we need to create a new folder inside the root of the Flutter project and named it assets. We can also give it any other name if you want.
Step 2: Next, inside this folder, add one image manually.
Step 3: Update the pubspec.yaml file. Suppose the image name is tablet.png, then pubspec.yaml file is:


assets:  
    - assets/tablet.png  
    - assets/background.png  

If the assets folder contains more than one image, we can include it by specifying the directory name with the slash (/) character at the end.


flutter:  
  assets:  
    - assets/  

Step 4: Finally, open themain.dart file and insert the following code.


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('Flutter Image Demo'),  
        ),  
        body: Center(  
          child: Column(  
            children: < Widget>[  
              Image.asset('assets/tablet.png'),  
              Text(  
                  'A tablet is a wireless touch screen computer that is smaller than a notebook but larger than a smartphone.',  
                  style: TextStyle(fontSize: 20.0),  
              )  
            ],  
          ),  
        ),  
      ),  
    );  
  }  
}  

Step 5: Now, run the app. You will get something like the screen below.



Display images from the internet


Displaying images from the internet or network is very simple. Flutter provides a built-in method Image.network to work with images from a URL. The Image.network method also allows you to use some optional properties, such as height, width, color, fit, and many more. We can use the following syntax to display an image from the internet.


Image.network(  
   'https://picsum.photos/250?image=9',  
)  

The Imag.Network gives one useful thing that supports animated gifs. We can use the following syntax for displaying gifs from the internet.


Image.network(  
  'https://github.com/flutter/plugins/raw/master/packages/video_player/doc/demo_ipod.gif?raw=true',  
);  

Let us understand how to display an image from the network with the following example:


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( 'Flutter Image Demo'),  
        ),  
        body: Center(  
          child: Column(  
            children: < Widget>[  
              Image.network(  
                   'https://static.javatpoint.com/tutorial/flutter/images/flutter-creating-android-platform-specific-code3.png',  
                  height: 400,  
                  width: 250  
              ),  
              Text(  
                   'It is an image displays from the given url.',  
                  style: TextStyle(fontSize: 20.0),  
              )  
            ],  
          ),  
        ),  
      ),  
    );  
  }  
}  

Output

When you run the app in Android Emulator, the following screen appears. Here, you can see the image of the given url.



Display Fade-In Images


When we display an image, it simply pops onto the screen as they are loaded. It does not assume useful between the users. To overcome this issue, the Image uses a FadeInImage widget that shows a placeholder image while the target image is loading, then fades in the new image when it loads. The FadeInImage can work with various types of images, such as local assets, in-memory, or images from the internet.


From asset bundle


Flutter also allows us to use local assets for placeholders. To use local assets, you need to add the asset in your project pubspec.yaml file.


flutter:  
   assets:  
    - assets/loading.gif  

Let us see the following example, which helps you to understand it more clearly. Open the main.dart file and insert the following code.


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('Flutter FadeInImage Demo'),  
        ),  
        body: Center(  
          child: Column(  
            children: < Widget>[  
              FadeInImage.assetNetwork(  
                placeholder: 'assets/tablet.png',  
                image: 'https://static.javatpoint.com/tutorial/flutter/images/flutter-creating-android-platform-specific-code3.png',  
                  height: 400,  
                  width: 250  
              ),  
              Text(  
                'It is an image displays from the given url.',  
                style: TextStyle(fontSize: 20.0),  
              )  
            ],  
          ),  
        ),  
      ),  
    );  
  }  
}  

Output

Now, run the app, it will give the laptop image (placeholder) before the image displayed from given url.



In-Memory

Let us understand it with the following example where FadeInImage works with In-Memory. Here, you must have to use a transparent_image package for transparent placeholder and update the dependencies of pubspec.yaml file as below:


transparent_image: ^1.0.0  

Now, open the main.dart file and insert the following code. When you run the app, it will give a transparent image as a placeholder.


import 'package:flutter/material.dart';  
import 'package:transparent_image/transparent_image.dart';  
  
void main() => runApp(MyApp());  
  
class MyApp extends StatelessWidget {  
  @override  
  Widget build(BuildContext context) {  
    return MaterialApp(  
      home: Scaffold(  
        appBar: AppBar(  
          title: Text('Flutter FadeInImage Demo'),  
        ),  
        body: Center(  
          child: Column(  
            children: < Widget>[  
              FadeInImage.memoryNetwork(  
                placeholder: kTransparentImage,  
                image: 'https://static.javatpoint.com/tutorial/flutter/images/flutter-creating-android-platform-specific-code3.png',  
                height: 400,  
                width: 250  
              ),  
              Text(  
                'It is an image displays from the given url.',  
                style: TextStyle(fontSize: 20.0),  
              )  
            ],  
          ),  
        ),  
      ),  
    );  
  }  
}  



Hi I am Pluto.