Flutter Image Tutorial and Examples

For some types of apps, an image is almost mandatory. Image appeal to many users learning capability than plain old text. We humans ar after all visual creatures. Therefore loading an image is of significant importance in any framework.

In android native development, loading an image from network is difficult and involves alot of boilerplate code. Normally developers have to rely on third party libraries like Picasso and Glide and these image loading libraries are used in quite so many apps.

It’s therefore interesting to see how flutter tackles this image loading problem. Fortunately for us, Flutter provides us a built in Image class that is super simple to use.

In this piece we want to examine some of the examples to allow us quickly load images.

Example 1 – Load Local Image

In this first flutter example we want to see how to load a local image. Sometimes you just have a few images that you need to be showing in your app, for example in the splash screen for decorative purposes or as infographics. In that case it’s probably an overkill to have to be using your users bandwidth to load such images from the network. Moreover that process is also slower and there is no guarantee that the image will be existing.

Thus it’s vital we also know how to load images locally.

In your main.dart, specify your imports:

import 'package:flutter/material.dart';

Create a StatelessWidget known as MyApp:

class MyApp extends StatelessWidget {

Override the build() method, passing in a BuildContext object:

  @override
  Widget build(BuildContext context) {

Return a Scaffold object:

    return new Scaffold(

Inside the Scaffold constructor pass an AppBar and body:

        appBar: new AppBar(
          title: new Text("Load local image"),
        ),
        body: new Container(

Then finish up the body with the following code:

child: new Center(
            child: new Text(
              "Hello World!",
              style: new TextStyle(color: Colors.white),
            ),
          ),
          // Set the image as the background of the Container
          decoration: new BoxDecoration(
              image: new DecorationImage(
                  // Load image from assets
                  image: new AssetImage('data_repo/img/bg1.jpg'),
                  // Make the image cover the whole area
                  fit: BoxFit.cover)),
        ));

You can see we are using the AssetImage to load an image from assets. In this case a custom folder called data_repo is created to hold the image. In that folder we have another folder called img and it inside this img folder that we have the JPEG image.

Here is the full code for main.dart:

import 'package:flutter/material.dart';

void main() {
  runApp(new MaterialApp(
    home: new MyApp(),
  ));
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(
          title: new Text("Load local image"),
        ),
        body: new Container(
          child: new Center(
            child: new Text(
              "Hello World!",
              style: new TextStyle(color: Colors.white),
            ),
          ),
          // Set the image as the background of the Container
          decoration: new BoxDecoration(
              image: new DecorationImage(
                  // Load image from assets
                  image: new AssetImage('data_repo/img/bg1.jpg'),
                  // Make the image cover the whole area
                  fit: BoxFit.cover)),
        ));
  }
}

Special thanks to @nisrulz for creating and sharing this example.

Here is the demo:

Flutter Load Local Image

Flutter Load Local Image

 

Example 2 – Load Image From Network

Start by creating a flutter project and navigate over to main.dart file.

Add your imports:

import 'package:flutter/material.dart';

Create MyApp class and make it extend the StatelessWidget class:

class MyApp extends StatelessWidget {

Override the build() method:

  @override
  Widget build(BuildContext context) {

Return a MaterialApp object:

    return new MaterialApp(

In its constructor, specify a Scaffold object as the home property:

 home: new Scaffold(
        appBar: new AppBar(
          title: new Text("Image from Network"),
        ),

Then in the body specify a Container with a Column object child:

 body: new Container(
            child: new Column(
          children: <Widget>[

As children of the Column object, specify two Image objects invoking the network() function:

           // Load image from network
            new Image.network(
                'https://github.com/nisrulz/flutter-examples/raw/develop/image_from_network/img/flutter_logo.png'),
            // even loads gifs
            // Gif image from Giphy, all copyrights are owned by Giphy
            new Image.network(
                'https://github.com/nisrulz/flutter-examples/raw/develop/image_from_network/img/loop_anim.gif'),
          ],

You can see all you need is to invoke the network() method and pass it the URL from which the image should be laoded. You can even load a gif image.

Here is the full code of main.dart:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text("Image from Network"),
        ),
        body: new Container(
            child: new Column(
          children: <Widget>[
            // Load image from network
            new Image.network(
                'https://github.com/nisrulz/flutter-examples/raw/develop/image_from_network/img/flutter_logo.png'),
            // even loads gifs
            // Gif image from Giphy, all copyrights are owned by Giphy
            new Image.network(
                'https://github.com/nisrulz/flutter-examples/raw/develop/image_from_network/img/loop_anim.gif'),
          ],
        )),
      ),
    );
  }
}

Special thanks to @nisrulz for creating and sharing this example.

Here is the demo:

Flutter Load Image From Network

Flutter Load Image From Network

 

Share



Share an Example

Share an Example

Browse
What is the capital of Egypt? ( Cairo )