AlertDialog in Flutter

An AlertDialog is a modal dialog box that appears on top of your UI. It’s typically used for:

  • Confirmations (e.g., delete, logout)
  • Error messages
  • User choices (Yes / No)
  • Input prompts (with TextField)

Source Code​

				
					
myAlert(context){
  return showDialog(
    context: context,
    builder: (BuildContext context) {
      return AlertDialog(
        title: Text('Confirm'),
        content: Text('Are you sure you want to proceed?'),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(context),
            child: Text('Cancel'),
          ),
          ElevatedButton(
            onPressed: () {
              // Perform action here
              Navigator.pop(context);
            },
            child: Text('Yes'),
          ),
        ],
        backgroundColor: Colors.greenAccent,
      );
    },
  );

}

//How to call,

             ElevatedButton(onPressed: (){
                myAlert(context);

              }, child: Text("Alert!")),
				
			

Ai Code Analizer

🎨 Important Properties

PropertyDescription
titleThe heading of the dialog
contentThe body content (usually Text or TextField)
actionsList of buttons (usually TextButton / ElevatedButton)
shapeOptional border styling for custom design
backgroundColorSet a custom background color