Flutter Images and Icons – Detailed Notes
Images and icons are important parts of Flutter application UI. Images can be used for profiles, banners, products, backgrounds, galleries, and illustrations, while icons communicate actions and information visually. Flutter provides built-in widgets such as Image and Icon for working with these elements.
Flutter supports bundled assets as well as images loaded from the internet. Local images are declared in pubspec.yaml, while network images can be displayed using Image.network(). Flutter also provides Material icons through the Icons class.
1. What Are Images in Flutter?
An image is a visual resource displayed inside a Flutter application. Flutter's Image widget can display images from different sources such as local assets, network URLs, memory, and files.
- Local asset images
- Network images
- Memory images
- File images
- Animated GIF images
- Images used as backgrounds
Common Image Constructors
| Constructor | Purpose |
Image.asset() | Displays an image bundled with the application. |
Image.network() | Displays an image from a URL. |
Image.file() | Displays an image from a local file. |
Image.memory() | Displays image data stored in memory. |
2. Adding Local Images to a Flutter Project
Local images are stored inside the Flutter project and bundled with the application. Flutter uses the assets section of pubspec.yaml to identify assets that should be included in the application.
Recommended Project Structure
my_flutter_app/
├── lib/
│ └── main.dart
├── assets/
│ ├── images/
│ │ ├── logo.png
│ │ ├── profile.jpg
│ │ └── banner.jpg
│ └── icons/
│ └── app_icon.png
└── pubspec.yaml
Step 1: Create an Assets Folder
Create an assets folder in the root directory of the Flutter project.
Step 2: Create an Images Folder
assets/images/
Step 3: Add Images
Place your images inside the folder.
assets/images/logo.png
assets/images/profile.jpg
assets/images/banner.jpg
Step 4: Declare Assets in pubspec.yaml
flutter:
uses-material-design: true
assets:
- assets/images/
Flutter's official documentation recommends declaring asset paths under the flutter section of pubspec.yaml. Directory entries can be used to include assets from a directory. :contentReference[oaicite:0]{index=0}
Step 5: Use the Image in Dart
import 'package:flutter/material.dart';
class ImageExample extends StatelessWidget {
const ImageExample({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Local Image'),
),
body: Center(
child: Image.asset('assets/images/logo.png'),
),
);
}
}
3. Image.asset()
Image.asset() is used to display an image stored in the application's asset bundle.
Basic Syntax
Image.asset('assets/images/photo.png')
Example
Center(
child: Image.asset(
'assets/images/profile.jpg',
),
)
Image with Width and Height
Image.asset(
'assets/images/profile.jpg',
width: 200,
height: 200,
)
Image with BoxFit
Image.asset(
'assets/images/banner.jpg',
width: double.infinity,
height: 200,
fit: BoxFit.cover,
)
4. Image.network()
Image.network() displays an image from an internet URL. This is useful when images are stored on a web server, CDN, or backend service.
Basic Syntax
Image.network(
'https://example.com/image.jpg',
)
Practical Example
import 'package:flutter/material.dart';
class NetworkImageExample extends StatelessWidget {
const NetworkImageExample({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Network Image'),
),
body: Center(
child: Image.network(
'https://picsum.photos/300',
width: 300,
height: 300,
fit: BoxFit.cover,
),
),
);
}
}
Flutter provides Image.network() specifically for displaying images from URLs. :contentReference[oaicite:1]{index=1}
5. Image Width and Height
The width and height properties control the size of the rendered image.
Image.asset(
'assets/images/product.png',
width: 250,
height: 200,
)
Responsive Width
Image.asset(
'assets/images/banner.jpg',
width: double.infinity,
height: 220,
)
double.infinity allows the image to use the maximum width available from its parent constraints.
6. BoxFit in Flutter Images
The fit property determines how an image should fit inside its available space.
| BoxFit | Description |
BoxFit.cover | Fills the available area while maintaining the image's aspect ratio. Some content may be cropped. |
BoxFit.contain | Displays the complete image while maintaining its aspect ratio. |
BoxFit.fill | Fills the entire area but may distort the image. |
BoxFit.fitWidth | Fits the image according to the available width. |
BoxFit.fitHeight | Fits the image according to the available height. |
BoxFit.none | Displays the image at its natural size without scaling. |
BoxFit.scaleDown | Scales the image down when necessary while preserving its aspect ratio. |
BoxFit.cover Example
Image.asset(
'assets/images/banner.jpg',
width: double.infinity,
height: 200,
fit: BoxFit.cover,
)
BoxFit.cover is commonly useful for banners and card images where the image should completely cover the allocated area. :contentReference[oaicite:2]{index=2}
7. Image Alignment
The alignment property controls the position of the image inside its available space.
Image.asset(
'assets/images/photo.jpg',
width: 300,
height: 200,
fit: BoxFit.cover,
alignment: Alignment.topCenter,
)
Common Alignments
Alignment.center
Alignment.topCenter
Alignment.bottomCenter
Alignment.centerLeft
Alignment.centerRight
Alignment.topLeft
Alignment.topRight
8. Image Inside a Container
Images can be combined with Container for creating structured UI designs.
Container(
width: 300,
height: 200,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(16),
),
clipBehavior: Clip.antiAlias,
child: Image.asset(
'assets/images/banner.jpg',
fit: BoxFit.cover,
),
)
9. Rounded Images
Rounded images are commonly used for profile pictures, product images, and cards.
Using ClipRRect
ClipRRect(
borderRadius: BorderRadius.circular(20),
child: Image.asset(
'assets/images/profile.jpg',
width: 200,
height: 200,
fit: BoxFit.cover,
),
)
Circular Profile Image
ClipOval(
child: Image.asset(
'assets/images/profile.jpg',
width: 120,
height: 120,
fit: BoxFit.cover,
),
)
CircleAvatar
CircleAvatar(
radius: 60,
backgroundImage: AssetImage(
'assets/images/profile.jpg',
),
)
10. Image Decoration
Images can be placed inside decorated containers to create borders, shadows, rounded corners, and other UI effects.
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
border: Border.all(
color: Colors.blue,
width: 2,
),
boxShadow: const [
BoxShadow(
blurRadius: 10,
offset: Offset(0, 5),
color: Colors.black26,
),
],
),
clipBehavior: Clip.antiAlias,
child: Image.asset(
'assets/images/product.jpg',
width: 250,
height: 250,
fit: BoxFit.cover,
),
)
11. Image Opacity
The opacity property can be used with an image to control transparency.
Image.asset(
'assets/images/background.jpg',
opacity: const AlwaysStoppedAnimation(0.5),
)
For more complex UI, an image can also be placed inside an Opacity widget.
Opacity(
opacity: 0.5,
child: Image.asset(
'assets/images/background.jpg',
),
)
12. Image as a Background
An image can be used as the background of a container using DecorationImage.
Container(
width: double.infinity,
height: 300,
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/background.jpg'),
fit: BoxFit.cover,
),
),
child: const Center(
child: Text(
'Welcome',
style: TextStyle(
color: Colors.white,
fontSize: 32,
fontWeight: FontWeight.bold,
),
),
),
)
13. Loading and Error Handling for Network Images
Network images may take time to load or may fail because of an unavailable URL or network problem. The loadingBuilder and errorBuilder properties can be used to provide better user feedback.
Loading Indicator
Image.network(
'https://picsum.photos/400',
loadingBuilder: (
BuildContext context,
Widget child,
ImageChunkEvent? loadingProgress,
) {
if (loadingProgress == null) {
return child;
}
return const Center(
child: CircularProgressIndicator(),
);
},
)
Error Builder
Image.network(
'https://example.com/invalid-image.jpg',
errorBuilder: (
BuildContext context,
Object error,
StackTrace? stackTrace,
) {
return const Icon(
Icons.broken_image,
size: 80,
color: Colors.grey,
);
},
)
14. Fade-In Network Images
Flutter provides FadeInImage for displaying a placeholder while a network image loads and then fading into the final image.
FadeInImage.assetNetwork(
placeholder: 'assets/images/loading.gif',
image: 'https://picsum.photos/400',
width: 300,
height: 200,
fit: BoxFit.cover,
)
Flutter's documentation provides FadeInImage.assetNetwork() for combining a local placeholder with a network image. :contentReference[oaicite:3]{index=3}
15. Resolution-Aware Images
Flutter supports resolution-aware image assets. This allows different image resolutions to be provided for devices with different pixel densities.
Example Folder Structure
assets/images/
├── logo.png
├── 2.0x/
│ └── logo.png
├── 3.0x/
│ └── logo.png
└── 4.0x/
└── logo.png
Flutter can select an appropriate resolution variant based on the device pixel ratio. The main asset or its parent directory is declared in pubspec.yaml. :contentReference[oaicite:4]{index=4}
16. What Are Icons in Flutter?
An icon is a small graphical representation of an action, object, feature, or status. Flutter's Material library provides a large collection of ready-to-use icons through the Icons class.
Basic Icon Syntax
Icon(Icons.home)
Icon with Size and Color
Icon(
Icons.favorite,
size: 40,
color: Colors.red,
)
17. Common Flutter Material Icons
| Icon | Code | Typical Use |
| Home | Icons.home | Home navigation |
| Search | Icons.search | Search functionality |
| Menu | Icons.menu | Navigation menu |
| Settings | Icons.settings | Application settings |
| Favorite | Icons.favorite | Favorite action |
| Person | Icons.person | User profile |
| Delete | Icons.delete | Delete action |
| Edit | Icons.edit | Edit action |
| Shopping Cart | Icons.shopping_cart | Shopping cart |
| Notifications | Icons.notifications | Notifications |
18. Icon Size
The size property controls the dimensions of an icon.
Icon(
Icons.star,
size: 50,
)
19. Icon Color
The color property changes the icon's color.
Icon(
Icons.favorite,
color: Colors.red,
size: 35,
)
20. Icons Inside Rows
Icons are commonly combined with text using Row.
Row(
children: const [
Icon(
Icons.location_on,
color: Colors.red,
),
SizedBox(width: 8),
Text('Mumbai, India'),
],
)
21. Icons Inside Columns
Column(
children: const [
Icon(
Icons.person,
size: 50,
),
SizedBox(height: 8),
Text('Profile'),
],
)
22. IconButton
IconButton is used when an icon needs to perform an action after the user taps it.
IconButton(
icon: const Icon(Icons.favorite),
iconSize: 35,
color: Colors.red,
onPressed: () {
print('Favorite clicked');
},
)
IconButton with Tooltip
IconButton(
tooltip: 'Delete',
icon: const Icon(Icons.delete),
onPressed: () {
print('Delete clicked');
},
)
23. Icons in AppBar
Icons are frequently used in the AppBar for navigation and actions.
Scaffold(
appBar: AppBar(
title: const Text('My App'),
leading: IconButton(
icon: const Icon(Icons.menu),
onPressed: () {
print('Menu clicked');
},
),
actions: [
IconButton(
icon: const Icon(Icons.search),
onPressed: () {
print('Search clicked');
},
),
IconButton(
icon: const Icon(Icons.notifications),
onPressed: () {
print('Notification clicked');
},
),
],
),
body: const Center(
child: Text('Home Screen'),
),
)
24. Icons in Buttons
Flutter buttons can contain icons and text together.
ElevatedButton.icon
ElevatedButton.icon(
onPressed: () {
print('Download clicked');
},
icon: const Icon(Icons.download),
label: const Text('Download'),
)
TextButton.icon
TextButton.icon(
onPressed: () {
print('Edit clicked');
},
icon: const Icon(Icons.edit),
label: const Text('Edit'),
)
OutlinedButton.icon
OutlinedButton.icon(
onPressed: () {
print('Share clicked');
},
icon: const Icon(Icons.share),
label: const Text('Share'),
)
25. Icons in Cards
Card(
child: Padding(
padding: const EdgeInsets.all(20),
child: Row(
children: const [
Icon(
Icons.shopping_cart,
size: 45,
color: Colors.blue,
),
SizedBox(width: 15),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Shopping Cart',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
Text('5 items'),
],
),
],
),
),
)
26. Icon with Circle Background
Container(
padding: const EdgeInsets.all(15),
decoration: const BoxDecoration(
color: Colors.blue,
shape: BoxShape.circle,
),
child: const Icon(
Icons.person,
color: Colors.white,
size: 35,
),
)
27. IconBadge Example
Icons can be combined with badges to show notification counts.
Stack(
children: [
IconButton(
icon: const Icon(Icons.notifications),
onPressed: () {},
),
Positioned(
right: 5,
top: 5,
child: Container(
padding: const EdgeInsets.all(4),
decoration: const BoxDecoration(
color: Colors.red,
shape: BoxShape.circle,
),
child: const Text(
'3',
style: TextStyle(
color: Colors.white,
fontSize: 10,
),
),
),
),
],
)
28. Image and Icon Together
Images and icons can be combined to create profile cards, product cards, dashboards, and social media interfaces.
Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Row(
children: [
ClipOval(
child: Image.asset(
'assets/images/profile.jpg',
width: 70,
height: 70,
fit: BoxFit.cover,
),
),
const SizedBox(width: 15),
const Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Rahul Sharma',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 5),
Row(
children: [
Icon(
Icons.location_on,
size: 16,
color: Colors.grey,
),
SizedBox(width: 4),
Text('Mumbai'),
],
),
],
),
),
],
),
),
)
29. Creating an Image Gallery
GridView.count(
crossAxisCount: 2,
crossAxisSpacing: 10,
mainAxisSpacing: 10,
padding: const EdgeInsets.all(10),
children: [
Image.asset(
'assets/images/photo1.jpg',
fit: BoxFit.cover,
),
Image.asset(
'assets/images/photo2.jpg',
fit: BoxFit.cover,
),
Image.asset(
'assets/images/photo3.jpg',
fit: BoxFit.cover,
),
Image.asset(
'assets/images/photo4.jpg',
fit: BoxFit.cover,
),
],
)
30. Product Card with Image and Icons
Card(
clipBehavior: Clip.antiAlias,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Image.asset(
'assets/images/shoes.jpg',
width: double.infinity,
height: 200,
fit: BoxFit.cover,
),
Padding(
padding: const EdgeInsets.all(12),
child: Row(
children: [
const Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Running Shoes',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 5),
Text(
'₹2,499',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
],
),
),
IconButton(
icon: const Icon(Icons.favorite_border),
onPressed: () {},
),
IconButton(
icon: const Icon(Icons.shopping_cart),
onPressed: () {},
),
],
),
),
],
),
)
31. Complete Images and Icons Example
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Images and Icons',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.blue,
),
useMaterial3: true,
),
home: const ProductScreen(),
);
}
}
class ProductScreen extends StatelessWidget {
const ProductScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Product'),
actions: [
IconButton(
icon: const Icon(Icons.favorite_border),
onPressed: () {},
),
IconButton(
icon: const Icon(Icons.shopping_cart),
onPressed: () {},
),
],
),
body: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Image.asset(
'assets/images/shoes.jpg',
width: double.infinity,
height: 300,
fit: BoxFit.cover,
),
Padding(
padding: const EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Running Shoes',
style: TextStyle(
fontSize: 26,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 10),
const Row(
children: [
Icon(
Icons.star,
color: Colors.amber,
),
SizedBox(width: 5),
Text('4.8'),
],
),
const SizedBox(height: 15),
const Text(
'Comfortable running shoes designed for everyday use.',
style: TextStyle(
fontSize: 16,
height: 1.5,
),
),
const SizedBox(height: 20),
SizedBox(
width: double.infinity,
child: ElevatedButton.icon(
onPressed: () {},
icon: const Icon(Icons.shopping_cart),
label: const Text('Add to Cart'),
),
),
],
),
),
],
),
),
);
}
}
32. Images vs Icons
| Feature | Images | Icons |
| Purpose | Photos, illustrations, banners, products | Actions, navigation, status, information |
| Examples | JPEG, PNG, WebP | Home, search, delete, settings |
| Typical Widget | Image | Icon |
| Source | Assets, network, files, memory | Material icon data or custom icon data |
| Interaction | Usually displayed visually | Often combined with IconButton |
33. Performance Best Practices for Images
- Use appropriately sized images instead of unnecessarily large files.
- Compress images before adding them to the project.
- Use
BoxFit appropriately for the layout.
- Use cached image solutions when an application repeatedly downloads the same remote images.
- Avoid loading extremely large images into small UI areas.
- Use resolution-aware assets when appropriate for different device pixel densities.
- Use placeholders and error states for network images.
- Use lazy-loading techniques for large galleries and long lists.
34. Performance Best Practices for Icons
- Prefer built-in Material icons when they satisfy the design requirement.
- Use
const icons where possible.
- Avoid unnecessarily loading large image files when a simple icon can represent the same action.
- Use meaningful icons that clearly communicate their purpose.
- Provide tooltips for icon-only actions where appropriate.
- Keep icon sizes consistent throughout the application.
35. Accessibility Considerations
Images and icons should be used in a way that supports accessibility.
Meaningful Images
When an image communicates important information, provide appropriate semantic information where needed.
Decorative Images
Decorative images should not unnecessarily interfere with screen-reader navigation.
Icon-Only Buttons
Icon-only interactive controls should have a meaningful tooltip or semantic label.
IconButton(
tooltip: 'Search',
icon: const Icon(Icons.search),
onPressed: () {},
)
36. Common Mistakes
Mistake 1: Forgetting pubspec.yaml
Image.asset('assets/images/logo.png')
If the asset is not declared correctly in pubspec.yaml, Flutter cannot load it as an application asset.
Mistake 2: Incorrect Asset Path
Image.asset('images/logo.png')
If the actual file is located at assets/images/logo.png, the path must match the declared asset path.
Mistake 3: Incorrect YAML Indentation
flutter:
assets:
- assets/images/
Mistake 4: Using a Huge Image
Very large images can increase memory usage and affect application performance.
Mistake 5: No Error Handling for Network Images
Network images can fail because of connectivity, invalid URLs, server errors, or other network conditions. Use an error state when appropriate.
Mistake 6: Making Icons Too Small
Very small icons can make navigation and actions difficult to recognize or tap.
37. Quick Reference
| Requirement | Flutter Code |
| Local image | Image.asset('assets/images/photo.png') |
| Network image | Image.network('https://example.com/photo.jpg') |
| Image size | width: 200, height: 200 |
| Cover image | fit: BoxFit.cover |
| Contain image | fit: BoxFit.contain |
| Rounded image | ClipRRect(...) |
| Circular image | ClipOval(...) |
| Profile image | CircleAvatar(...) |
| Basic icon | Icon(Icons.home) |
| Colored icon | Icon(Icons.favorite,color: Colors.red) |
| Icon button | IconButton(...) |
| Button with icon | ElevatedButton.icon(...) |
| Network loading | loadingBuilder |
| Network error | errorBuilder |
38. Practice Exercises
- Create a Flutter screen displaying a local company logo.
- Create a profile card containing a circular profile image.
- Create a product card with an image, price, favorite icon, and shopping cart icon.
- Create a two-column image gallery using
GridView.
- Create a network image screen using
Image.network().
- Add a loading indicator while a network image is loading.
- Add an error icon when a network image fails.
- Create an AppBar containing search, notification, and settings icons.
- Create a dashboard containing cards with different icons.
- Create a profile screen combining images, icons, text, and buttons.
39. Interview Questions
- What is the
Image widget in Flutter?
- What is the difference between
Image.asset() and Image.network()?
- Why do images need to be declared in
pubspec.yaml?
- What is
BoxFit.cover?
- What is the difference between
BoxFit.cover and BoxFit.contain?
- How can you create a circular image in Flutter?
- How can you display a network image?
- How can you show a loading indicator for a network image?
- How can you handle a failed network image?
- What is the
Icon widget?
- What is the
Icons class?
- What is the difference between
Icon and IconButton?
- How can you change an icon's size and color?
- How can you add an icon to an ElevatedButton?
- How can images and icons be combined to create a product card?
40. Key Takeaways
Image.asset() is used for local application assets.
Image.network() is used for images loaded from URLs.
- Local image assets should be declared correctly in
pubspec.yaml.
BoxFit controls how images fit inside their available space.
ClipRRect and ClipOval can be used to create rounded and circular images.
CircleAvatar is useful for profile images.
Icon displays an icon.
Icons provides Material icon definitions.
IconButton makes an icon interactive.
loadingBuilder and errorBuilder improve the network-image experience.
- Images and icons can be combined with Row, Column, Card, Stack, AppBar, and buttons to create professional Flutter interfaces.
41. Learning Resources
JustAcademy Flutter Training: https://www.justacademy.co/course-detail/flutter-training
Register for Course Demo: https://www.justacademy.co/register-for-course-demo
Official Flutter Assets and Images Documentation: Flutter Assets and Images
Official Flutter Network Images Guide: Display Images from the Internet
Official Flutter Assets, Images and Icon Widgets: Assets, Images, and Icon Widgets