Flutter – Popover Button
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' ), ), ), ); } } |
Contact Us