// Importing Flutter material package for UI components
import 'package:flutter/material.dart';
// Entry point of the application
void main() {
runApp(MyApp()); // Runs the app and loads MyApp widget
}
// Root widget of the application
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
// Define the overall theme of the app
theme: ThemeData(
primarySwatch: Colors.green, // Set primary color to green
),
debugShowCheckedModeBanner: false, // Hide the debug banner
title: 'GridView in AlertDialog Example', // App title
home: GridViewAlertDialogExample(), // Set home screen
);
}
}
// Main screen that contains a button to show GridView inside an AlertDialog
class GridViewAlertDialogExample extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
// AppBar widget at the top of the screen
appBar: AppBar(
backgroundColor: Colors.green, // Set app bar background color
foregroundColor: Colors.white, // Set app bar text/icon color
title: Text('GridView in AlertDialog Example'), // Title text
),
// Body of the screen
body: Center(
// Center a button on the screen
child: ElevatedButton(
// Style the button with green background and white text
style: ElevatedButton.styleFrom(
backgroundColor: Colors.green,
foregroundColor: Colors.white,
),
onPressed: () {
showGridViewDialog(context); // Call function to show dialog
},
child: Text('Show GridView Dialog'), // Button label
),
),
);
}
}
// Function to display an AlertDialog containing a GridView
void showGridViewDialog(BuildContext context) {
showDialog(
context: context, // Required to show the dialog in the right widget tree
builder: (context) {
return CustomGridViewDialog(); // Return custom dialog widget
},
);
}
// Custom widget that defines the GridView inside an AlertDialog
class CustomGridViewDialog extends StatelessWidget {
@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text('GridView Inside AlertDialog'), // Dialog title
content: Container(
width: double.maxFinite, // Make container as wide as possible
// GridView to show items in a grid format
child: GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2, // 2 items per row
childAspectRatio: 1.0, // Width-to-height ratio of each item
),
itemCount: 6, // Number of items to show
itemBuilder: (context, index) {
return Card(
color: Colors.green, // Card background color
elevation: 3, // Adds shadow to create depth
margin: EdgeInsets.all(8), // Space around each card
child: InkWell(
onTap: () {
// Print item index when tapped
print('Tapped on Item $index');
},
child: Center(
// Display item index text in the center
child: Text(
'Item $index',
style: TextStyle(color: Colors.white, fontSize: 20),
),
),
),
);
},
),
),
// Dialog action buttons
actions: <Widget>[
ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.green, // Button background color
foregroundColor: Colors.white, // Button text color
),
onPressed: () {
Navigator.of(context).pop(); // Close the dialog on press
},
child: Text('Close'), // Button label
),
],
);
}
}