In Flutter, Popover buttons are a user interface pattern commonly used to display additional content or actions in response to user input. They are particularly useful when you want to present contextual options by blurring the main screen. The Popover package in Flutter simplifies the implementation of popover-style components. In this article, we will go through the process of integrating and customizing Popover Buttons in the Flutter applications. A sample video is given below to get an idea about what we are going to do in this article.
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: Adding the Dependencies
Here we have to add the the following dependencies to our pubspec.yaml file.
dependencies:
popover: ^0.2.9
Or, Simply you can run the below command in your VS code Terminal.
flutter pub add popover
Step 3: Import the Package
First of all import material.dart package and the popover package.
import 'package:flutter/material.dart';
import 'package:popover/popover.dart';
Step 4: Execute the main Method
Here the execution of our app starts.
Dart
void main() => runApp(MyApp());
Step 5: 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 {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.green,
),
debugShowCheckedModeBanner: false,
home: PopoverExample(),
);
}
}
Step 6: Create PopoverExample Class
It simple calls a user defined PopoverButton class which will responsible for implementing popoverbutton.Comments are added for better understandings.
Dart
class PopoverExample extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Popover Example')),
body: SafeArea(
child: Padding(
padding: EdgeInsets.all(16),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
PopoverButton(), // Displaying the PopoverButton widget
],
),
),
),
);
}
}
Step 7: Create PopoverButton Class
The PopoverButton class in this Flutter defines a stateless widget representing a screen that implement the popover button. "The body of the screen consists of a centered container, serving as a button, styled with a blue background, rounded corners, and a shadow. When this button is tapped, it triggers the display of a popover using the showPopover function. The popover contains a list of items, and upon tapping an item, the popover is dismissed, and a new route (SecondRoute) is pushed to the navigation stack. Comments are added for better understandings.
Container(
width: 150,
height: 60,
decoration: const BoxDecoration(
color: Colors.green, // Set the background color of the button
borderRadius: BorderRadius.all(Radius.circular(8)), // Apply border radius to the button
boxShadow: [BoxShadow(color: Colors.black26, blurRadius: 5)], // Add a shadow to the button
),
child: GestureDetector(
child: const Center(
child: Text(
'Show Popover', // Display text on the button
style: TextStyle(color: Colors.white), // Set text color to white
),
),
onTap: () {
// Show a popover when the button is tapped
showPopover(
context: context,
bodyBuilder: (context) => const ListItems(), // Display the ListItems in the popover
onPop: () => print('Popover was popped!'), // Print a message when the popover is dismissed
direction: PopoverDirection.bottom, // Set the direction of the popover
width: 200, // Set the width of the popover
height: 250, // Set the height of the popover
arrowHeight: 15, // Set the height of the arrow
arrowWidth: 30, // Set the width of the arrow
);
},
),
),
Dart
class PopoverButton extends StatelessWidget {
const PopoverButton({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Center(
child: Container(
width: 150,
height: 60,
decoration: const BoxDecoration(
color: Colors.green, // Set the background color of the button
borderRadius: BorderRadius.all(Radius.circular(8)), // Apply border radius to the button
boxShadow: [BoxShadow(color: Colors.black26, blurRadius: 5)], // Add a shadow to the button
),
child: GestureDetector(
child: const Center(
child: Text(
'Show Popover', // Display text on the button
style: TextStyle(color: Colors.white), // Set text color to white
),
),
onTap: () {
// Show a popover when the button is tapped
showPopover(
context: context,
bodyBuilder: (context) => const ListItems(), // Display the ListItems in the popover
onPop: () => print('Popover was popped!'), // Print a message when the popover is dismissed
direction: PopoverDirection.bottom, // Set the direction of the popover
width: 200, // Set the width of the popover
height: 250, // Set the height of the popover
arrowHeight: 15, // Set the height of the arrow
arrowWidth: 30, // Set the width of the arrow
);
},
),
),
);
}
}
Step 8: Create ListItems Class
This Class representing a list of items displayed within a popover. Each item in the list is a clickable container with distinctive colors and text labels ('Entry A', 'Entry B', 'Entry C'). Upon tapping an item, it triggers the dismissal of the popover, and a new route (SecondRoute) is pushed onto the navigation stack, providing a navigation mechanism for the selected item. Comments are added for better understandings.
Dart
class ListItems extends StatelessWidget {
const ListItems({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: ListView(
padding: const EdgeInsets.all(8),
children: [
// Item A in the popover
InkWell(
onTap: () {
// Navigate to SecondRoute when Item A is tapped
Navigator.of(context)
..pop() // Dismiss the current screen (popover)
..push(
MaterialPageRoute<SecondRoute>(
builder: (context) => SecondRoute(),
),
);
},
child: Container(
height: 50,
color: Colors.amber[100],
child: const Center(child: Text('Entry A')),
),
),
const Divider(),
// Item B in the popover
Container(
height: 50,
color: Colors.amber[200],
child: const Center(child: Text('Entry B')),
),
const Divider(),
// Item C in the popover
Container(
height: 50,
color: Colors.amber[300],
child: const Center(child: Text('Entry C')),
),
],
),
);
}
}
Step 9: Create SecondRoute Class
The SecondRoute screen will open when a user taps on one of the items within the popover displayed by the ListItems widget.
Dart
class SecondRoute extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Second Route'),
automaticallyImplyLeading: false,
),
body: Center(
child: ElevatedButton(
onPressed: () => Navigator.pop(context),
child: const Text('Back'),
),
),
);
}
}
Here is the full Code of main.dart file
Dart
import 'package:flutter/material.dart';
import 'package:popover/popover.dart';
void main() => runApp(MyApp()); // Entry point of the application
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.green,
),
debugShowCheckedModeBanner: false,
home: PopoverExample(),
);
}
}
class PopoverExample extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Popover Example')),
body: SafeArea(
child: Padding(
padding: EdgeInsets.all(16),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
PopoverButton(), // Displaying the PopoverButton widget
],
),
),
),
);
}
}
class PopoverButton extends StatelessWidget {
const PopoverButton({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Center(
child: Container(
width: 150,
height: 60,
decoration: const BoxDecoration(
color: Colors.green, // Set the background color of the button
borderRadius: BorderRadius.all(
Radius.circular(8)), // Apply border radius to the button
boxShadow: [
BoxShadow(color: Colors.black26, blurRadius: 5)
], // Add a shadow to the button
),
child: GestureDetector(
child: const Center(
child: Text(
'Show Popover', // Display text on the button
style: TextStyle(color: Colors.white), // Set text color to white
),
),
onTap: () {
// Show a popover when the button is tapped
showPopover(
context: context,
bodyBuilder: (context) =>
const ListItems(), // Display the ListItems in the popover
onPop: () => print(
'Popover was popped!'), // Print a message when the popover is dismissed
direction:
PopoverDirection.bottom, // Set the direction of the popover
width: 200, // Set the width of the popover
height: 250, // Set the height of the popover
arrowHeight: 15, // Set the height of the arrow
arrowWidth: 30, // Set the width of the arrow
);
},
),
),
);
}
}
class ListItems extends StatelessWidget {
const ListItems({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: ListView(
padding: const EdgeInsets.all(8),
children: [
// Item A in the popover
InkWell(
onTap: () {
// Navigate to SecondRoute when Item A is tapped
Navigator.of(context)
..pop() // Dismiss the current screen (popover)
..push(
MaterialPageRoute<SecondRoute>(
builder: (context) => SecondRoute(),
),
);
},
child: Container(
height: 50,
color: Colors.amber[100],
child: const Center(child: Text('Entry A')),
),
),
const Divider(),
// Item B in the popover
Container(
height: 50,
color: Colors.amber[200],
child: const Center(child: Text('Entry B')),
),
const Divider(),
// Item C in the popover
Container(
height: 50,
color: Colors.amber[300],
child: const Center(child: Text('Entry C')),
),
],
),
);
}
}
class SecondRoute extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Second Route'),
automaticallyImplyLeading: false,
),
body: Center(
child: ElevatedButton(
onPressed: () => Navigator.pop(context),
child: const Text('Back'),
),
),
);
}
}
Output:
Similar Reads
Flutter - Toggle Buttons A toggle button allows the user to change a setting between two states. You can add a basic toggle button to your layout with the ToggleButton object. A sample video is given below to get an idea about what we are going to do in this article.Demo Video:SyntaxList Of Booleans to Know Which Selected:3
4 min read
Flutter - Neumorphic Button Flutter contains various in-built button types like ElevatedButton, TextButton, etc. But with the help of GestureDetector, we can make any widget perform a set of actions on certain gestures. In this article, we are going to use AnimatedContainer to make a Neumorphic Button in Flutter. The tradition
5 min read
Flutter - Gradient Button Buttons are the building block of any Android Application, Buttons are helping in to perform specific tasks like Navigating from one screen to another screen, Showing the Toast, Sign-in, Sign-up buttons, and many more. But giving the effects of the colors to the Button makes the Button more pretty.
3 min read
Flutter - SVG Image as Button In this article, we will see how to make the SVG image a button in Flutter so that we can perform actions. A sample video is given below to get an idea about what we are going to do in this article. How to Use? You can use the Gesture Detector to add click functionality to any Widget in Flutter. Dar
2 min read
FlatButton Widget in Flutter FlatButton is the material design widget in a flutter. It is a text label material widget that performs an action when the button is tapped. Let's understand with the help of examples. Disclaimer: As of May 2021 the FlatButton class in flutter is deprecated. TextButton class should be used instead.
3 min read