Skip to main content

Command Palette

Search for a command to run...

AlertDialog Widget in Attributes

Published
2 min read
AlertDialog Widget in Attributes
V

"Hello World, I'm Vinit Mepani, a coding virtuoso driven by passion, fueled by curiosity, and always poised to conquer challenges. Picture me as a digital explorer, navigating through the vast realms of code, forever in pursuit of innovation.

In the enchanting kingdom of algorithms and syntax, I wield my keyboard as a magical wand, casting spells of logic and crafting solutions to digital enigmas. With each line of code, I embark on an odyssey of learning, embracing the ever-evolving landscape of technology.

Eager to decode the secrets of the programming universe, I see challenges not as obstacles but as thrilling quests, opportunities to push boundaries and uncover new dimensions in the realm of possibilities.

In this symphony of zeros and ones, I am Vinit Mepani, a coder by passion, an adventurer in the digital wilderness, and a seeker of knowledge in the enchanting world of code. Join me on this quest, and let's create digital wonders together!"

The AlertDialog widget in Flutter is used to display an alert dialog box that typically informs the user about critical information or prompts them to make a decision.

Attributes:

  1. title (Widget):

    • The title of the alert dialog. It's typically a Text widget.
  2. content (Widget):

    • The content of the alert dialog, which can include additional information or instructions. It's typically a Text widget or another widget.
  3. actions (List<Widget>):

    • The actions or buttons to be displayed at the bottom of the alert dialog. Each action is typically represented by a TextButton or ElevatedButton widget.
  4. backgroundColor (Color):

    • The background color of the alert dialog.
  5. elevation (double):

    • The elevation of the alert dialog, which controls the shadow depth.
  6. shape (ShapeBorder):

    • The shape of the alert dialog, which can be customized using various shape border classes like RoundedRectangleBorder or ContinuousRectangleBorder.
  7. scrollable (bool):

    • Determines whether the content of the alert dialog is scrollable if it exceeds the available space.

Example:

import 'package:flutter/material.dart';

class AlertDialogExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('AlertDialog Example'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            showDialog(
              context: context,
              builder: (BuildContext context) {
                return AlertDialog(
                  title: Text('Delete Confirmation'),
                  content: Text('Are you sure you want to delete this item?'),
                  actions: [
                    TextButton(
                      onPressed: () {
                        Navigator.of(context).pop();
                      },
                      child: Text('Cancel'),
                    ),
                    ElevatedButton(
                      onPressed: () {
                        // Perform delete operation
                        Navigator.of(context).pop();
                      },
                      child: Text('Delete'),
                    ),
                  ],
                );
              },
            );
          },
          child: Text('Show AlertDialog'),
        ),
      ),
    );
  }
}

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

In this example, an AlertDialog is displayed when the user taps the "Show AlertDialog" button. The dialog asks for confirmation before deleting an item, and it contains "Cancel" and "Delete" buttons as actions. When the buttons are pressed, the dialog is dismissed. This showcases how to use the AlertDialog widget to prompt users for input or confirmation in a Flutter app.

More from this blog

Vinit Mepani (Flutter Developer)

270 posts

"Vinit Mepani, passionate coder! Dive into my Dart and Flutter journey on the blog. Let's master these tech wonders together. Happy coding! 🚀"