Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024

Flutter Images

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Google announced Flutter, a free and open-source mobile user interface framework, in May 2017. In a nutshell, it enables you to develop a native app for mobile using only one codebase. You can design two different apps using the same programming language and codebase (for iOS and Android).

Source: Twitter
 

When you create an app in Flutter, it has both assets and code. A file that is bundled and delivered with the app and available at runtime is known as an asset(resources). Examples are Static data, configuration files, icons, and photos.

JPEG, WebP, PNG, GIF, animated WebP/GIF, BMP, and WBMP are among the image formats supported by Flutter.

Step to display the Image in Flutter

Step 1: First, we create a new folder; It should be the starting point for your flutter project. It can be called anything you choose, although assets are recommended.

Step 2: Inside this assets folder, you manually add one Image as shown below.

Step 3:Now update the file pubspec.yaml. And if the image name is Coding_Ninja_logo.jpeg, then

flutter:   
  assets:
        - assets/images/Coding_Ninja_logo.jpeg
        - assets/image/Background.jpeg

If the assets folder has many images, we can include them by adding a slash (/) to the end of the directory name.

flutter:               
  assets:     
         - assets/

 

Step 4: And at the end, open the main.dart file and insert the following code.

 Image.asset('assets/Coding_Ninja_logo.jpeg')

import 'package:flutter/material.dart';  
    
void main() => runApp(MyApp());  
  
class MyApp extends StatelessWidget {  
  
  // This widget is the root 
  // of your application
   
  @override  
  Widget build(BuildContext context) {  
   
    return MaterialApp(  
        
      home: Scaffold(  
        appBar: AppBar(  
          title: Text('Insert Image Demo'),  
        ),  
           
        body: Center(  
          child: Column(              
            children: <Widget>[  
            Image.asset('assets/Coding_Ninja_logo.jpeg'),  
            ],  
          ),  
        ),  
      ),  
    );  
  }  
}  

 

Step 5: You can save all the files and run the app. You will get something like the screen below.

Display images from the internet

It is straightforward to display images from the internet or a network. For working with photos from a URL, Flutter has a built-in method, Image.network. Optional attributes such as height, width, color, fit, and many others are available using the Image.network method. To display a picture from the internet, enter the following syntax.

Image.network(         'https://i.picsum.photos/id/0/5616/3744.jpg?hmac=3GAAioiQziMGEtLbfrdbcoenXoWAW-zlyEAMkfEdBzQ',
              )
 

 

Example:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {

  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter Image Demo'),
        ),
        body: Center(
          child: Column(
            children: <Widget>[
              Image.network(         'https://i.picsum.photos/id/0/5616/3744.jpg?hmac=3GAAioiQziMGEtLbfrdbcoenXoWAW-zlyEAMkfEdBzQ',
              )
            ],
          ),
        ),
      ),
    );
  }
}

 

Output:

Also Read - Image Sampling

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently Asked Questions

What is an Image asset in Flutter?

Static data, configuration files, icons, and photos are all examples of assets. JPEG, WebP, PNG, GIF, animated WebP/GIF, BMP, and WBMP are among the image formats supported by the Flutter app. Syntax: Image.asset('image name')

What is the hive in Flutter?

Android Studio and Flutter SDK are the tools and technology used. Keywords: Hive, Flutter, Storage, Local Database Hive is a dart package for storing and processing data on a targeted device used in Flutter applications.

How do you quickly load pictures on Flutter?

In Flutter, we have an essential but handy function called precacheImage() that we can use to load our asset pictures significantly faster. This method saves the image to the image cache, which means it will load substantially quicker the next time needed. Image Cache, on the other hand, does not support really huge photos.

Does Flutter support SVG?

Flutter's main version does not support SVG.

In the source of Skia, a basic component of Flutter, there is an SVG directory. Skia, on the other hand, can only serialize photos into SVG files. As a result, Skia cannot decode or render SVG images.

What is the splash screen in Flutter?

A splash screen is a graphical control element that contains the Image, logo, and current version of the software. It is also known as a launch screen, start screen, or boot screen. It is the app's first screen, which appears as the program is loading.

Is SVG an image?

Scalable Vector Graphics (SVG) is a two-dimensional vector image format based on XML that supports interactivity and animation.

What is canvas Flutter?

A Canvas in Flutter is a virtual painting surface, similar to how an artist's canvas is a physical drawing surface. However, unlike a traditional painting canvas, the Flutter canvas cannot be painted with tangible brushes. Flutter Canvas determines the position of a point on the screen using a two-point (x and y) coordinate system.

Conclusion

In this article, we discussed Flutter Image. We had also seen different ways to add an image, And finally, in the end, we have shown an example of how to write code for adding an image.

We hope that our blog has helped you enhance your knowledge regarding Flutter Image. Please check out these articles to improve your understanding of Flutter Image:Flutter RichTextFlutter TableFlutter Animation.

Please refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. And also, enroll in our courses and refer to the mock test and problems available. Have a look at the interview experiences and interview bundle for placement preparations.

Please upvote our blog to help other ninjas grow.

Happy Learning!

Topics covered
1.
Introduction
2.
Step to display the Image in Flutter
2.1.
Display images from the internet
3.
Frequently Asked Questions
3.1.
What is an Image asset in Flutter?
3.2.
What is the hive in Flutter?
3.3.
How do you quickly load pictures on Flutter?
3.4.
Does Flutter support SVG?
3.5.
What is the splash screen in Flutter?
3.6.
Is SVG an image?
3.7.
What is canvas Flutter?
4.
Conclusion