Flutter - Display a GridView Within an AlertDialog Box
Last Updated :
06 Nov, 2023
AlertDialogs are useful Widgets in Flutter to attract the users attention to a particular message that is showing by the Alert Dialogs, GridView are efficient widgets to display items in a Grid like structure. To implement a GridView inside an AlertDialog in Flutter, you can create a custom dialog content widget that contains a GridView and then display this custom dialog using the showDialog method. In this article we are going to implement an GridView within an Alert Dialog. A sample video is given below to get an idea about what we are going to do in this article.
Basic Syntax of an GridView in Flutter
GridView(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2, // Number of columns in the grid
mainAxisSpacing: 10.0, // Spacing between rows
crossAxisSpacing: 10.0, // Spacing between columns
childAspectRatio: 1.0, // Width to height ratio for grid items
),
children: <Widget>[
// List of grid items
GridTile(
child: YourGridItemWidget(),
),
// Add more GridTile widgets for other grid items
],
)
Basic Syntax of an Alert Dialog in Flutter
AlertDialog(
title: Text('Dialog Title'), // Optional title
content: Text('Dialog Content'), // Main content of the dialog
actions: <Widget>[
// List of action buttons
TextButton(
onPressed: () {
// Action to perform when the button is pressed
},
child: Text('Button 1'),
),
TextButton(
onPressed: () {
// Action to perform when the button is pressed
},
child: Text('Button 2'),
),
// Add more TextButton widgets for additional actions
],
)
Step By Step Implementation
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(
theme: ThemeData(
// Set the app's primary theme color
primarySwatch: Colors.green,
),
debugShowCheckedModeBanner: false,
title: 'GridView in AlertDialog Example',
home: GridViewAlertDialogExample(),
);
}
}
Step 5: Create GridViewAlertDialogExample Class
In this step we are going to create a Button by clicking the AlertDialog with an Gridview will appear on the Screen. When the button is Clicked it calls the showGridViewDialog method then the method call CustomGridViewDialog class on which the GridView Within an Alert Dialog is Implemented.
Dart
class GridViewAlertDialogExample extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('GridView in AlertDialog Example'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
showGridViewDialog(context); // Show the GridView dialog
},
child: Text('Show GridView Dialog'),
),
),
);
}
}
void showGridViewDialog(BuildContext context) {
showDialog(
context: context,
builder: (context) {
return CustomGridViewDialog();
},
);
}
Step 6: Create CustomGridViewDialog Class
In this class we are going to implement a Gridview Within an AlertDialog.
AlertDialog(
title: Text('GridView Inside AlertDialog'),
content: Container(
width: double.maxFinite,
child: GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 1.0,
),
itemCount: 6,
itemBuilder: (context, index) {
return Card(
elevation: 3, // Add elevation for the card effect
margin: EdgeInsets.all(8), // Add margin for spacing between cards
child: InkWell(
onTap: () {
print('Tapped on Item $index');
},
child: Center(
child: Text('Item $index'),
),
),
);
},
),
),
actions: <Widget>[
ElevatedButton(
onPressed: () {
Navigator.of(context).pop(); // Close the dialog
},
child: Text('Close'),
),
],
);
Dart
class CustomGridViewDialog extends StatelessWidget {
@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text('GridView Inside AlertDialog'),
content: Container(
width: double.maxFinite,
child: GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 1.0,
),
itemCount: 6,
itemBuilder: (context, index) {
return Card(
elevation: 3, // Add elevation for the card effect
margin: EdgeInsets.all(8), // Add margin for spacing between cards
child: InkWell(
onTap: () {
print('Tapped on Item $index');
},
child: Center(
child: Text('Item $index'),
),
),
);
},
),
),
actions: <Widget>[
ElevatedButton(
onPressed: () {
Navigator.of(context).pop(); // Close the dialog
},
child: Text('Close'),
),
],
);
}
}
Here is the full Code of main.dart file:
Dart
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
// Set the app's primary theme color
primarySwatch: Colors.green,
),
debugShowCheckedModeBanner: false, // Disable debug banner
title: 'GridView in AlertDialog Example',
home: GridViewAlertDialogExample(),
);
}
}
class GridViewAlertDialogExample extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('GridView in AlertDialog Example'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
showGridViewDialog(context); // Show the GridView dialog
},
child: Text('Show GridView Dialog'),
),
),
);
}
}
void showGridViewDialog(BuildContext context) {
showDialog(
context: context,
builder: (context) {
return CustomGridViewDialog();
},
);
}
class CustomGridViewDialog extends StatelessWidget {
@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text('GridView Inside AlertDialog'),
content: Container(
width: double.maxFinite,
child: GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 1.0,
),
itemCount: 6,
itemBuilder: (context, index) {
return Card(
elevation: 3, // Add elevation for the card effect
margin: EdgeInsets.all(8), // Add margin for spacing between cards
child: InkWell(
onTap: () {
print('Tapped on Item $index');
},
child: Center(
child: Text('Item $index'),
),
),
);
},
),
),
actions: <Widget>[
ElevatedButton(
onPressed: () {
Navigator.of(context).pop(); // Close the dialog
},
child: Text('Close'),
),
],
);
}
}
Output:
Similar Reads
Flutter - Implement an Image Inside an AlertDialog
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 proc
4 min read
Flutter - Animated AlertDialog in Flutter
Animating an AlertDialog in Flutter involves using the Flutter animations framework to create custom animations for showing and hiding the dialogue. In this article, we are going to add an animation to an AlertDialog. A sample video is given below to get an idea about what we are going to do in this
4 min read
Alert Dialog box in Flutter
Alert Dialog box informs the user about the situation that requires acknowledgment. Alert Box is a prompt that takes user confirmation. The very basic use of the alert box is used when we close the app, usually, we are notified with a prompt whether we want to exit or not. That's an alert box. The b
2 min read
Flutter - Make AlertDialog Disappear Automatically After Few Seconds
In Flutter, we can auto-close an AlertDialog by using the Future.delayed method to schedule a delay and then automatically close the AlertDialog. In this article, we are going to implement an AlertDialog and implement the Future.delayed method for auto closing the AlertDialog. A sample video is give
4 min read
Flutter - Implement DropdownButton Inside an AlertDialog
DropdownButton widget is used to create a dropdown menu that allows users to select one option from a list of available choices. It's a common user interface element for selecting items from a list. An Alert Dialog is a useful way to grab users' attention. Here we can see how to implement an AlertDi
5 min read
Flutter - Implement Stepper Widget Inside an AlertDialog
The Stepper widget in Flutter is a user interface element used to create a step-by-step process interface. It allows users to progress through a series of steps or stages, with each step typically representing a specific task or some information. An Alert Dialog is a useful way to grab users' attent
5 min read
How to Build a Flutter App to Display Device Details?
Here we are going to build an android application using flutter that displays device information on which it is running. To display information about the device we are going to use a package called device_info_plus. Create Flutter Project: Now remove all the existing code and remove the test folder
5 min read
How to Add Audio File in Flutter Application?
Flutter is a well known tool for developing cross platform applications. It is considered a good choice among developers because of the single code base policy for all types of OS and devices. Flutter engine provides support to vast libraries and file types, its easy to develop beautifull UI by addi
3 min read
How to Create a Desktop Window Application in Flutter?
The Flutter team recently released Flutter version 2.10 with Desktop support. Desktop support allows you to compile Flutter source code to a native Windows, macOS, or Linux desktop app. Flutterâs desktop support also extends to pluginsâyou can install existing plugins that support the Windows, macOS
3 min read
How to Build a ToDo Application in Flutter?
Flutter offers a stable framework for constructing richly UI-driven cross-platform applications. In this article, we will learn to build a ToDo Flutter Application. What is the ToDo Application?The ToDo application helps users manage and organize their tasks and responsibilities more easily. Managin
6 min read