Open In App

Flutter – Implement an Image Inside an AlertDialog

Last Updated : 16 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

AlertDialogs are a common tool used to deliver messages, notifications, or interactive content to users. While Flutter provides a straightforward way to create AlertDialogs, it may not be immediately obvious how to include images inside them. Fortunately, this article will guide you through the process of implementing images inside AlertDialogs in Flutter, helping you enhance the visual appeal and usability of your mobile applications. This article will demonstrate how to Integrate an Image inside an Alert Dialog in Flutter. A sample video is given below to get an idea about what we are going to do in this article.

Required Tools

To build this app, you need the following items installed on your machine:

  • Visual Studio Code / Android Studio
  • Android Emulator / iOS Simulator / Physical Device device.
  • Flutter Installed
  • Flutter plugin for VS Code / Android Studio.

Step By Step Implementations

Step 1: Create a New Project in Android Studio

To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.

Step 2: Import the Package

First of all import material.dart file.

import 'package:flutter/material.dart';

Step 3: Execute the main Method

Here the execution of our app starts.

Dart




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


Step 4: Create MyApp Class

In this class we are going to implement the MaterialApp , here we are also set the Theme of our App.

Dart




class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.green, // Set the app's primary theme color
      ),
      home: HomePage(),
    );
  }
}


Step 5: Create HomePage Class

In this class, we are going to implement a scaffold to display our UI, and Here we are also applying the Alert Dialog with an Image.

Dart




class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('AlertDialog with Image'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            _showImageAlertDialog(
                context); // Call the function to display the AlertDialog with an image
          },
          child: Text('Show Image AlertDialog'),
        ),
      ),
    );
  }
  
  // Function to display the AlertDialog with an image
  void _showImageAlertDialog(BuildContext context) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('Image Alert Dialog'),
          content: Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              Image.asset(
                'assets/img.png', // Replace with your image path
                width: 150, // Adjust image width as needed
              ),
              SizedBox(height: 16), // Adjust spacing as needed
              Text('This is your image description.'),
            ],
          ),
          actions: <Widget>[
            ElevatedButton(
              onPressed: () {
                Navigator.of(context).pop(); // Close the AlertDialog
              },
              child: Text('Close'),
            ),
          ],
        );
      },
    );
  }
}


Here is the full code to refer to the main.dart file

Dart




import 'package:flutter/material.dart';
  
void main() {
  runApp(MyApp());
}
  
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.green, // Set the app's primary theme color
      ),
      home: HomePage(),
    );
  }
}
  
class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('AlertDialog with Image'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            _showImageAlertDialog(
                context); // Call the function to display the AlertDialog with an image
          },
          child: Text('Show Image AlertDialog'),
        ),
      ),
    );
  }
  
  // Function to display the AlertDialog with an image
  void _showImageAlertDialog(BuildContext context) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('Image Alert Dialog'),
          content: Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              Image.asset(
                'assets/img.png', // Replace with your image path
                width: 150, // Adjust image width as needed
              ),
              SizedBox(height: 16), // Adjust spacing as needed
              Text('This is your image description.'),
            ],
          ),
          actions: <Widget>[
            ElevatedButton(
              onPressed: () {
                Navigator.of(context).pop(); // Close the AlertDialog
              },
              child: Text('Close'),
            ),
          ],
        );
      },
    );
  }
}


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads