Corporate Training
Request Demo
Click me
Menu
Let's Talk
Request Demo

Tutorials

Working with Images and Icons

16. Working with Images and Icons

Images and icons play a significant role in enhancing the visual appeal and usability of Flutter applications. In this tutorial, we'll explore how to work with images and icons in Flutter, including loading images, displaying them, and using icons from Flutter's icon library.

1. Loading and Displaying Images

Flutter provides several ways to load and display images. The most common method is to use the 'Image' widget or the 'Image.network' constructor to load images from the network.

Example: Displaying an Image from the Network
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Images and Icons Tutorial',
      home: ImageExample(),
    );
  }
}

class ImageExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Image Example'),
      ),
      body: Center(
        child: Image.network(
          'https://example.com/path/to/your/image.jpg',
          width: 200.0,
          height: 200.0,
          fit: BoxFit.cover, // Adjust this to your needs
        ),
      ),
    );
  }
}
 

In this example, replace the URL in 'Image.network' with the URL of the image you want to display. You can adjust the 'width', 'height', and 'fit' properties to control the image's size and how it fits within the container.

2. Adding Local Images

To include local images in your Flutter app, you need to add them to your project's assets and update the 'pubspec.yaml'  file.

Step 1: Add Images to the Assets Folder

Create a folder named 'assets' in the root of your Flutter project and place your images inside it.

For example, if you have an image named 'my_image.jpg', the folder structure should look like this:
- my_flutter_app/
  - assets/
    - my_image.jpg
  - lib/
  - ...
 

Step 2: Update 'pubspec.yaml'

In your 'pubspec.yaml' file, add the following lines to include the assets:
flutter:
  assets:
    - assets/
 

Then run 'flutter pub get'  to include the assets in your project.

Step 3: Display Local Image

Now, you can display the local image using the 'Image.asset' constructor:
Image.asset(
  'assets/my_image.jpg',
  width: 200.0,
  height: 200.0,
  fit: BoxFit.cover,
)
 

Replace ''assets/my_image.jpg'' with the path to your local image.

3. Working with Icons

Flutter comes with a rich set of icons that you can use in your application without the need for external image assets. These icons are part of the Flutter icon library.

Example: Using Flutter Icons
Icon(
  Icons.star,
  size: 48.0,
  color: Colors.yellow,
)
 

In this example, we use the 'Icons.star' icon from the Flutter icon library. You can customize the 'size' and 'color' properties to fit your app's design.

4. Custom Icons

If you want to use custom icons in your app, you can create them as custom images or SVG files and follow the steps mentioned earlier for adding local images. Then, you can use the Image.asset widget to display custom icons.

Example: Displaying a Custom Icon

Assuming you have a custom icon image named 'custom_icon.png' in your assets folder, you can display it like this:
Image.asset(
  'assets/custom_icon.png',
  width: 48.0,
  height: 48.0,
)
 

Conclusion:

Working with images and icons in Flutter is a fundamental aspect of app development. Whether you're loading images from the network, adding local images, or using Flutter's built-in icons, it's essential to know how to incorporate visual elements effectively into your application's user interface. Experiment with different image sizes, formats, and icon styles to achieve the desired look and feel for your Flutter app.