Flutterexperts

Empowering Vision with FlutterExperts' Expertise
Implement Listener Class in Flutter

When you will code for building anything in Flutter, it will be inside a widget. The focal intention is to make the application out of widgets. It depicts how your application view ought to look with its ongoing design and state. At the point when you make any modification in the code, the widget rebuilds its depiction by working out the distinction between the past and current widgets to decide the negligible changes for delivering in the UI of the application.

Widgets are settled with one another to fabricate the application. It implies the root of your application is itself a widget, and right down is a widget too. For instance, a widget can show something, characterize configuration, handle cooperation, and so on.

This blog will explore the Implement Listener Class in Flutter. We perceive how to execute a demo program. We will learn how to use the Listener class in your flutter applications.

Listener class – widgets library – Dart API
A widget that calls callbacks in response to common pointer events. It listens to events that can construct gestures…api. flutter.dev

Table Of Contents::

Introduction

Constructor

Properties

Code Implement

Code File

Conclusion



Introduction:

A widget called the Listener triggers callbacks in light of continuous pointer events. It focuses on things like when the cursor is squeezed, moved, delivered, or dropped which can make motions. It doesn’t focus on mouse-explicit occasions as when the mouse shows up, leaves, or floats over an area without clicking any buttons.

Use MouseRegion Class for these occasions. While contrasting the list of items that a mouse pointer is floating on between this edge and the past casing, MouseRegion is utilized. This incorporates moving mouse cursors, beginning occasions, and finishing occasions. Use Listener or, ideally, GestureDetector Class to pay attention to normal pointer events.

Demo Module::

This demo video shows how to use the listener class in a flutter and shows how a listener class will work in your flutter applications. We will show a user press or release screen many times, then the value will be shown on your devices.

Constructor:

To utilize Listener, you need to call the constructor underneath:

const Listener({
Key? key,
this.onPointerDown,
this.onPointerMove,
this.onPointerUp,
this.onPointerHover,
this.onPointerCancel,
this.onPointerSignal,
this.behavior = HitTestBehavior.deferToChild,
Widget? child,
})

Properties:

There are some properties of Listener are:

  • onPointerDown: This property is utilized to Callers can be informed regarding these events in a widget tree thanks to the listener.onPointerDown capability.
  • onPointerMove: This property is utilized while the pointer is in contact with the object, it has moved about that object.
  • onPointerUp: This property is utilized to the pointer is no longer near the object.
  • onPointerHover: This property is utilized while the pointer is not in contact with the device, it has moved about it.
  • onPointerCancel: This property is utilized to the input from the pointer is no longer directed towards this receiver.
  • onPointerSignal: This property is utilized to 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: This property is utilized only if one of their children is struck by the hit test do targets that defer to their children get events within their boundaries?
  • child: This property is used in the Flutter framework’s primary class ordered progression is built of widgets. An immutable depiction of a part of a UI is alluded to as a widget. Components that administer the basic render tree can be made by blowing up widgets.

How to implement code in dart file :

You need to implement it in your code respectively:

Create a new dart file called main.dart inside the lib folder.

In the main. dart file, we will create a new class was ListenerDemo. In this class, we will create two integer variables that were _down and _up is equal to zero. Also, creating another two double variables x and y is equal to zero.

int _down = 0;
int _up = 0;
double x = 0.0;
double y = 0.0;

Now, we will create a _updateLocation() method. In this method, we will add setState() function. In this function, we will add x as equal to the details.position.dx and y are equal to the details.position.dy.

void _updateLocation(PointerEvent details) {
setState(() {
x = details.position.dx;
y = details.position.dy;
});
}

Then, we will create _incrementDown() and _incrementUp() methods. In _incrementDown, we will add the _updateLocation(details) and add setState() function. In this function, add _down++. In _incrementUp, we will add the _updateLocation(details) and add setState() function. In this function, add _up++.

void _incrementDown(PointerEvent details) {
_updateLocation(details);
setState(() {
_down++;
});
}

void _incrementUp(PointerEvent details) {
_updateLocation(details);
setState(() {
_up++;
});
}

In the body, we will add the ConstrainedBox widget. In this widget, we will add Listener() widget. In this widget, we will add onPointerDown is equal to the _incrementDown method, onPointerMove is equal to the _updateLocation method, and onPointerUp is equal to the _incrementUp method.

ConstrainedBox(
constraints: BoxConstraints.tight(const Size(380.0, 300.0)),
child: Listener(
onPointerDown: _incrementDown,
onPointerMove: _updateLocation,
onPointerUp: _incrementUp,
child: Container(
color: Colors.orange[200],
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'You have pressed or released this area this many times:'),
const SizedBox(height: 15,),
Text(
'$_down presses\n$_up releases',
style: Theme.of(context).textTheme.headline3,
),
const SizedBox(height: 15,),
Text(
'The cursor is here: (${x.toStringAsFixed(2)}, ${y.toStringAsFixed(2)})',
),
],
),
),
),
),

When we run the application, we ought to get the screen’s output like the underneath screen capture.

Final Output

Code File:

import 'package:flutter/material.dart';
import 'package:flutter_listener_demo/splash_screen.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return const MaterialApp(debugShowCheckedModeBanner: false, home: Splash());
}
}

class ListenerDemo extends StatefulWidget {
const ListenerDemo({Key? key}) : super(key: key);

@override
State createState() => _ListenerDemoState();
}

class _ListenerDemoState 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 Scaffold(
appBar: AppBar(
title: const Text("Flutter Listener Class Demo"),
centerTitle: true,
automaticallyImplyLeading: false,
backgroundColor: Colors.orangeAccent[400],
),
body: Center(
child: ConstrainedBox(
constraints: BoxConstraints.tight(const Size(380.0, 300.0)),
child: Listener(
onPointerDown: _incrementDown,
onPointerMove: _updateLocation,
onPointerUp: _incrementUp,
child: Container(
color: Colors.orange[200],
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'You have pressed or released this area this many times:'),
const SizedBox(
height: 15,
),
Text(
'$_down presses\n$_up releases',
style: Theme.of(context).textTheme.headline3,
),
const SizedBox(
height: 15,
),
Text(
'The cursor is here: (${x.toStringAsFixed(2)}, ${y.toStringAsFixed(2)})',
),
],
),
),
),
),
),
);
}
}

Conclusion:

In the article, I have explained the Listener basic structure in a flutter; you can modify this code according to your choice. This was a small introduction to Listener User Interaction from my side, and it’s working using Flutter.

I hope this blog will provide you with sufficient information on Trying the Listener in your flutter projectsWe will show you what the Introduction is and what are the construction and properties of the Listener, and make a demo program for working with Listener in your flutter applications. So please try it.

❤ ❤ Thanks for reading this article ❤❤

If I got something wrong? Let me know in the comments. I would love to improve.

Clap 👏 If this article helps you.


Feel free to connect with us:
And read more articles from FlutterDevs.com.

FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire a flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! You can connect with us on FacebookGitHubTwitter, and LinkedIn for any flutter-related queries.

We welcome feedback and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences.


Leave comment

Your email address will not be published. Required fields are marked with *.