SlideShare a Scribd company logo
1 of 14
How to use/implement Listener Class in Flutter?
Introduction:
A widget called the Listener triggers callbacks in response to frequent
pointer events.
It pays attention to things like when the cursor is pressed, moved,
released, or canceled that can create motions.
It doesn’t pay attention to mouse-specific events as when the mouse
arrives, leaves, or hovers over an area without clicking any buttons.
Use MouseRegion Class for these events.
When comparing the list of objects that a mouse pointer is hovering on
between this frame and the previous frame, MouseRegion is used. This
includes moving mouse cursors, starting events, and ending events.
Use Listener or, preferably, GestureDetector Class to listen to common
pointer occurrences.
In this article, we will see how to use the listener class in the Flutter
app development.
Below is the constructor of the Lister Class.
Listener(
{Key? key,
PointerDownEventListener? onPointerDown,
PointerMoveEventListener? onPointerMove,
PointerUpEventListener? onPointerUp,
PointerHoverEventListener? onPointerHover,
PointerCancelEventListener? onPointerCancel,
PointerSignalEventListener? onPointerSignal,
HitTestBehavior behavior = HitTestBehavior.deferToChild,
Widget? child}
)
Properties of the Listener class:
PointerDownEventListener? onPointerDown:
Callers can be informed of these events in a widget tree thanks to the
listener.onPointerDown function.
PointerMoveEventListener? onPointerMove:
While the pointer is in contact with the object, it has moved in relation
to that object.
PointerUpEventListener? onPointerUp:
The pointer is no longer in close proximity to the object.
PointerHoverEventListener? onPointerHover:
While the pointer is not in contact with the device, it has moved in
relation to it.
PointerCancelEventListener? onPointerCancel:
The input from the pointer is no longer directed towards this receiver.
PointerSignalEventListener? onPointerSignal:
Pointer signals are discrete events that come from the pointer but
don’t affect the state of the pointer itself. They don’t have to be
understood in the context of a chain of events to be understood.
HitTestBehavior behavior:
Only if one of their children is struck by the hit test do targets that
defer to their children get events within their boundaries.
Widget? child:
The Flutter framework’s primary class hierarchy is constructed of
widgets. An immutable description of a component of a user interface
is referred to as a widget. Elements that govern the underlying render
tree can be created by inflating widgets.
Example of Listener Class:
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text("Flutter Listener Class Sample")),
body: const Center(
child: MyStatefulWidget(),
),
),
);
}
}
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({Key? key}) : super(key: key);
@override
State createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State {
int _down = 0;
int _up = 0;
double x = 0.0;
double y = 0.0;
void _incrementDown(PointerEvent details) {
_updateLocation(details);
setState(() {
_down++;
});
}
void _incrementUp(PointerEvent details) {
_updateLocation(details);
setState(() {
_up++;
});
}
void _updateLocation(PointerEvent details) {
setState(() {
x = details.position.dx;
y = details.position.dy;
});
}
@override
Widget build(BuildContext context) {
return ConstrainedBox(
constraints: BoxConstraints.tight(const Size(400.0, 300.0)),
child: Listener(
onPointerDown: _incrementDown,
onPointerMove: _updateLocation,
onPointerUp: _incrementUp,
child: Container(
color: Colors.green,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'You have pressed or released in this area this many times:'),
Text(
'$_down pressesn$_up releases',
style: Theme.of(context).textTheme.headline4,
),
Text(
'The cursor is here: (${x.toStringAsFixed(2)}, ${y.toStringAsFixed(2)})',
),
],
),
),
),
);
}
}
Output:
Conclusion:
We described the fundamental implementation of the Listener in a
Flutter in the article; you are free to change the code as you see fit. This
was a brief explanation of Listener On User Interaction from our
perspective, and it functions with Flutter. Get experienced consultation
support from our Flutter developer for custom business mobile
application development solutions. Hope you enjoy this article.
Source: https://flutteragency.com/use-implement-listener-class-
flutter/

More Related Content

Similar to How to use Listener Class in Flutter.pptx

Flash Lite & Touch: build an iPhone-like dynamic list
Flash Lite & Touch: build an iPhone-like dynamic listFlash Lite & Touch: build an iPhone-like dynamic list
Flash Lite & Touch: build an iPhone-like dynamic list
Small Screen Design
 
ITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptxITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptx
udithaisur
 

Similar to How to use Listener Class in Flutter.pptx (20)

AWT
AWT AWT
AWT
 
Lecture 18
Lecture 18Lecture 18
Lecture 18
 
Event Handling in Java
Event Handling in JavaEvent Handling in Java
Event Handling in Java
 
Ajp notes-chapter-03
Ajp notes-chapter-03Ajp notes-chapter-03
Ajp notes-chapter-03
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptx
 
Advance java for bscit
Advance java for bscitAdvance java for bscit
Advance java for bscit
 
Mobile Application Development class 005
Mobile Application Development class 005Mobile Application Development class 005
Mobile Application Development class 005
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 
Unit-3 event handling
Unit-3 event handlingUnit-3 event handling
Unit-3 event handling
 
Event Handling in JAVA
Event Handling in JAVAEvent Handling in JAVA
Event Handling in JAVA
 
Java gui event
Java gui eventJava gui event
Java gui event
 
event handling new.ppt
event handling new.pptevent handling new.ppt
event handling new.ppt
 
09events
09events09events
09events
 
Advance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handlingAdvance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handling
 
java Unit4 chapter1 applets
java Unit4 chapter1 appletsjava Unit4 chapter1 applets
java Unit4 chapter1 applets
 
Flash Lite & Touch: build an iPhone-like dynamic list
Flash Lite & Touch: build an iPhone-like dynamic listFlash Lite & Touch: build an iPhone-like dynamic list
Flash Lite & Touch: build an iPhone-like dynamic list
 
TY.BSc.IT Java QB U1
TY.BSc.IT Java QB U1TY.BSc.IT Java QB U1
TY.BSc.IT Java QB U1
 
ITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptxITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptx
 
Swing
SwingSwing
Swing
 
Event handling in Java(part 1)
Event handling in Java(part 1)Event handling in Java(part 1)
Event handling in Java(part 1)
 

More from Flutter Agency

More from Flutter Agency (20)

Key Steps To Follow For Google Play Store Closed Testing
Key Steps To Follow For Google Play Store Closed TestingKey Steps To Follow For Google Play Store Closed Testing
Key Steps To Follow For Google Play Store Closed Testing
 
Flutter for Web App Development: Exploring the Possibilities
Flutter for Web App Development: Exploring the PossibilitiesFlutter for Web App Development: Exploring the Possibilities
Flutter for Web App Development: Exploring the Possibilities
 
Use Of AI in Custom Application Development | Quick Guide
Use Of AI in Custom Application Development | Quick GuideUse Of AI in Custom Application Development | Quick Guide
Use Of AI in Custom Application Development | Quick Guide
 
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
 
Use Firebase to Host Your Flutter App on the Web
Use Firebase to Host Your Flutter App on the WebUse Firebase to Host Your Flutter App on the Web
Use Firebase to Host Your Flutter App on the Web
 
Authentication Made Simple - Exploring QR Auto Login in Flutter.pdf
Authentication Made Simple - Exploring QR Auto Login in Flutter.pdfAuthentication Made Simple - Exploring QR Auto Login in Flutter.pdf
Authentication Made Simple - Exploring QR Auto Login in Flutter.pdf
 
User Enhancement With Animated Flutter Drawer
User Enhancement With Animated Flutter DrawerUser Enhancement With Animated Flutter Drawer
User Enhancement With Animated Flutter Drawer
 
Unit Testing in Flutter - From Workflow Essentials to Complex Scenarios
Unit Testing in Flutter - From Workflow Essentials to Complex ScenariosUnit Testing in Flutter - From Workflow Essentials to Complex Scenarios
Unit Testing in Flutter - From Workflow Essentials to Complex Scenarios
 
Form Validation in Flutter with Laravel Form Validation Syntax
Form Validation in Flutter with Laravel Form Validation SyntaxForm Validation in Flutter with Laravel Form Validation Syntax
Form Validation in Flutter with Laravel Form Validation Syntax
 
How to Create Custom Shaders in Flutter?
How to Create Custom Shaders in Flutter?How to Create Custom Shaders in Flutter?
How to Create Custom Shaders in Flutter?
 
Benefits Of Hiring Flutter App Developers For Success
Benefits Of Hiring Flutter App Developers For SuccessBenefits Of Hiring Flutter App Developers For Success
Benefits Of Hiring Flutter App Developers For Success
 
Guide to Fix Dropdown Button Not Switching Selected Item | Flutter
Guide to Fix Dropdown Button Not Switching Selected Item | FlutterGuide to Fix Dropdown Button Not Switching Selected Item | Flutter
Guide to Fix Dropdown Button Not Switching Selected Item | Flutter
 
12 Straightforward Steps to Build Your Video On-Demand App in 2024
12 Straightforward Steps to Build Your Video On-Demand App in 202412 Straightforward Steps to Build Your Video On-Demand App in 2024
12 Straightforward Steps to Build Your Video On-Demand App in 2024
 
Flutter's Advantages For Custom Application Development Services
Flutter's Advantages For Custom Application Development ServicesFlutter's Advantages For Custom Application Development Services
Flutter's Advantages For Custom Application Development Services
 
Hire Flutter Developers to Build Cross-Platform App Services - Stonesmentor
Hire Flutter Developers to Build Cross-Platform App Services - StonesmentorHire Flutter Developers to Build Cross-Platform App Services - Stonesmentor
Hire Flutter Developers to Build Cross-Platform App Services - Stonesmentor
 
A Guide For Recovering Your Failing App Project | Flutter Agency
A Guide For Recovering Your Failing App Project | Flutter AgencyA Guide For Recovering Your Failing App Project | Flutter Agency
A Guide For Recovering Your Failing App Project | Flutter Agency
 
Healthcare App-Development Company Fllutter Agency
Healthcare App-Development Company Fllutter AgencyHealthcare App-Development Company Fllutter Agency
Healthcare App-Development Company Fllutter Agency
 
Is Flutter Good for Web Development? | Flutter Agency
Is Flutter Good for Web Development? | Flutter AgencyIs Flutter Good for Web Development? | Flutter Agency
Is Flutter Good for Web Development? | Flutter Agency
 
Choosing App Development: Native, Hybrid, or Flutter Explained
Choosing App Development: Native, Hybrid, or Flutter ExplainedChoosing App Development: Native, Hybrid, or Flutter Explained
Choosing App Development: Native, Hybrid, or Flutter Explained
 
The Role of Digital Transformation in Healthcare - Flutter Agency.pdf
The Role of Digital Transformation in Healthcare - Flutter Agency.pdfThe Role of Digital Transformation in Healthcare - Flutter Agency.pdf
The Role of Digital Transformation in Healthcare - Flutter Agency.pdf
 

Recently uploaded

Jax, FL Admin Community Group 05.14.2024 Combined Deck
Jax, FL Admin Community Group 05.14.2024 Combined DeckJax, FL Admin Community Group 05.14.2024 Combined Deck
Jax, FL Admin Community Group 05.14.2024 Combined Deck
Marc Lester
 

Recently uploaded (20)

Lessons Learned from Building a Serverless Notifications System.pdf
Lessons Learned from Building a Serverless Notifications System.pdfLessons Learned from Building a Serverless Notifications System.pdf
Lessons Learned from Building a Serverless Notifications System.pdf
 
The mythical technical debt. (Brooke, please, forgive me)
The mythical technical debt. (Brooke, please, forgive me)The mythical technical debt. (Brooke, please, forgive me)
The mythical technical debt. (Brooke, please, forgive me)
 
What is a Recruitment Management Software?
What is a Recruitment Management Software?What is a Recruitment Management Software?
What is a Recruitment Management Software?
 
Auto Affiliate AI Earns First Commission in 3 Hours..pdf
Auto Affiliate  AI Earns First Commission in 3 Hours..pdfAuto Affiliate  AI Earns First Commission in 3 Hours..pdf
Auto Affiliate AI Earns First Commission in 3 Hours..pdf
 
Jax, FL Admin Community Group 05.14.2024 Combined Deck
Jax, FL Admin Community Group 05.14.2024 Combined DeckJax, FL Admin Community Group 05.14.2024 Combined Deck
Jax, FL Admin Community Group 05.14.2024 Combined Deck
 
architecting-ai-in-the-enterprise-apis-and-applications.pdf
architecting-ai-in-the-enterprise-apis-and-applications.pdfarchitecting-ai-in-the-enterprise-apis-and-applications.pdf
architecting-ai-in-the-enterprise-apis-and-applications.pdf
 
Food Delivery Business App Development Guide 2024
Food Delivery Business App Development Guide 2024Food Delivery Business App Development Guide 2024
Food Delivery Business App Development Guide 2024
 
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi.pdf
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi.pdfStrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi.pdf
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi.pdf
 
A Deep Dive into Secure Product Development Frameworks.pdf
A Deep Dive into Secure Product Development Frameworks.pdfA Deep Dive into Secure Product Development Frameworks.pdf
A Deep Dive into Secure Product Development Frameworks.pdf
 
Weeding your micro service landscape.pdf
Weeding your micro service landscape.pdfWeeding your micro service landscape.pdf
Weeding your micro service landscape.pdf
 
Automate your OpenSIPS config tests - OpenSIPS Summit 2024
Automate your OpenSIPS config tests - OpenSIPS Summit 2024Automate your OpenSIPS config tests - OpenSIPS Summit 2024
Automate your OpenSIPS config tests - OpenSIPS Summit 2024
 
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
 
^Clinic ^%[+27788225528*Abortion Pills For Sale In witbank
^Clinic ^%[+27788225528*Abortion Pills For Sale In witbank^Clinic ^%[+27788225528*Abortion Pills For Sale In witbank
^Clinic ^%[+27788225528*Abortion Pills For Sale In witbank
 
Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?
 
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
 
Abortion Clinic Pretoria ](+27832195400*)[ Abortion Clinic Near Me ● Abortion...
Abortion Clinic Pretoria ](+27832195400*)[ Abortion Clinic Near Me ● Abortion...Abortion Clinic Pretoria ](+27832195400*)[ Abortion Clinic Near Me ● Abortion...
Abortion Clinic Pretoria ](+27832195400*)[ Abortion Clinic Near Me ● Abortion...
 
Abortion Clinic In Pretoria ](+27832195400*)[ 🏥 Safe Abortion Pills in Pretor...
Abortion Clinic In Pretoria ](+27832195400*)[ 🏥 Safe Abortion Pills in Pretor...Abortion Clinic In Pretoria ](+27832195400*)[ 🏥 Safe Abortion Pills in Pretor...
Abortion Clinic In Pretoria ](+27832195400*)[ 🏥 Safe Abortion Pills in Pretor...
 
Spring into AI presented by Dan Vega 5/14
Spring into AI presented by Dan Vega 5/14Spring into AI presented by Dan Vega 5/14
Spring into AI presented by Dan Vega 5/14
 
^Clinic ^%[+27788225528*Abortion Pills For Sale In harare
^Clinic ^%[+27788225528*Abortion Pills For Sale In harare^Clinic ^%[+27788225528*Abortion Pills For Sale In harare
^Clinic ^%[+27788225528*Abortion Pills For Sale In harare
 
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
 

How to use Listener Class in Flutter.pptx

  • 1.
  • 2. How to use/implement Listener Class in Flutter? Introduction: A widget called the Listener triggers callbacks in response to frequent pointer events. It pays attention to things like when the cursor is pressed, moved, released, or canceled that can create motions. It doesn’t pay attention to mouse-specific events as when the mouse arrives, leaves, or hovers over an area without clicking any buttons. Use MouseRegion Class for these events.
  • 3. When comparing the list of objects that a mouse pointer is hovering on between this frame and the previous frame, MouseRegion is used. This includes moving mouse cursors, starting events, and ending events. Use Listener or, preferably, GestureDetector Class to listen to common pointer occurrences. In this article, we will see how to use the listener class in the Flutter app development. Below is the constructor of the Lister Class.
  • 4. Listener( {Key? key, PointerDownEventListener? onPointerDown, PointerMoveEventListener? onPointerMove, PointerUpEventListener? onPointerUp, PointerHoverEventListener? onPointerHover, PointerCancelEventListener? onPointerCancel, PointerSignalEventListener? onPointerSignal, HitTestBehavior behavior = HitTestBehavior.deferToChild, Widget? child} )
  • 5. Properties of the Listener class: PointerDownEventListener? onPointerDown: Callers can be informed of these events in a widget tree thanks to the listener.onPointerDown function. PointerMoveEventListener? onPointerMove: While the pointer is in contact with the object, it has moved in relation to that object. PointerUpEventListener? onPointerUp: The pointer is no longer in close proximity to the object.
  • 6. PointerHoverEventListener? onPointerHover: While the pointer is not in contact with the device, it has moved in relation to it. PointerCancelEventListener? onPointerCancel: The input from the pointer is no longer directed towards this receiver. PointerSignalEventListener? onPointerSignal: Pointer signals are discrete events that come from the pointer but don’t affect the state of the pointer itself. They don’t have to be understood in the context of a chain of events to be understood.
  • 7. HitTestBehavior behavior: Only if one of their children is struck by the hit test do targets that defer to their children get events within their boundaries. Widget? child: The Flutter framework’s primary class hierarchy is constructed of widgets. An immutable description of a component of a user interface is referred to as a widget. Elements that govern the underlying render tree can be created by inflating widgets.
  • 8. Example of Listener Class: import 'package:flutter/material.dart'; void main() => runApp(const MyApp()); class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: const Text("Flutter Listener Class Sample")), body: const Center( child: MyStatefulWidget(), ), ), ); } }
  • 9. class MyStatefulWidget extends StatefulWidget { const MyStatefulWidget({Key? key}) : super(key: key); @override State createState() => _MyStatefulWidgetState(); } class _MyStatefulWidgetState extends State { int _down = 0; int _up = 0; double x = 0.0; double y = 0.0; void _incrementDown(PointerEvent details) { _updateLocation(details); setState(() { _down++; }); }
  • 10. void _incrementUp(PointerEvent details) { _updateLocation(details); setState(() { _up++; }); } void _updateLocation(PointerEvent details) { setState(() { x = details.position.dx; y = details.position.dy; }); }
  • 11. @override Widget build(BuildContext context) { return ConstrainedBox( constraints: BoxConstraints.tight(const Size(400.0, 300.0)), child: Listener( onPointerDown: _incrementDown, onPointerMove: _updateLocation, onPointerUp: _incrementUp, child: Container( color: Colors.green, child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const Text( 'You have pressed or released in this area this many times:'),
  • 12. Text( '$_down pressesn$_up releases', style: Theme.of(context).textTheme.headline4, ), Text( 'The cursor is here: (${x.toStringAsFixed(2)}, ${y.toStringAsFixed(2)})', ), ], ), ), ), ); } }
  • 14. Conclusion: We described the fundamental implementation of the Listener in a Flutter in the article; you are free to change the code as you see fit. This was a brief explanation of Listener On User Interaction from our perspective, and it functions with Flutter. Get experienced consultation support from our Flutter developer for custom business mobile application development solutions. Hope you enjoy this article. Source: https://flutteragency.com/use-implement-listener-class- flutter/