From 273e2ff310402b96f2f1e95892881f2bbd643dff Mon Sep 17 00:00:00 2001 From: vedasjad <96325125+vedasjad@users.noreply.github.com> Date: Sat, 16 Dec 2023 02:08:55 +0530 Subject: [PATCH] Backspace onLongPress callback added --- lib/src/default_emoji_picker_view.dart | 27 ++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/src/default_emoji_picker_view.dart b/lib/src/default_emoji_picker_view.dart index 7d3fcc8..df14486 100644 --- a/lib/src/default_emoji_picker_view.dart +++ b/lib/src/default_emoji_picker_view.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:emoji_picker_flutter/emoji_picker_flutter.dart'; import 'package:emoji_picker_flutter/src/skin_tone_overlay.dart'; import 'package:flutter/material.dart'; @@ -15,6 +17,7 @@ class DefaultEmojiPickerView extends EmojiPickerBuilder { class _DefaultEmojiPickerViewState extends State with SingleTickerProviderStateMixin, SkinToneOverlayStateMixin { final double _tabBarHeight = 46; + Timer? _onBackspacePressedCallbackTimer; late PageController _pageController; late TabController _tabController; @@ -45,6 +48,7 @@ class _DefaultEmojiPickerViewState extends State _pageController.dispose(); _tabController.dispose(); _scrollController.dispose(); + _onBackspacePressedCallbackTimer?.cancel(); super.dispose(); } @@ -112,15 +116,18 @@ class _DefaultEmojiPickerViewState extends State if (widget.state.onBackspacePressed != null) { return Material( type: MaterialType.transparency, - child: IconButton( + child: GestureDetector( + onLongPressStart: (_) => _startOnBackspacePressedCallback(), + onLongPressEnd: (_) => _stopOnBackspacePressedCallback(), + child: IconButton( padding: const EdgeInsets.only(bottom: 2), icon: Icon( Icons.backspace, color: widget.config.backspaceColor, ), - onPressed: () { - widget.state.onBackspacePressed!(); - }), + onPressed: () => widget.state.onBackspacePressed!(), + ), + ), ); } return const SizedBox.shrink(); @@ -203,4 +210,16 @@ class _DefaultEmojiPickerViewState extends State widget.state.onEmojiSelected(category, emoji); closeSkinToneOverlay(); } + + void _startOnBackspacePressedCallback() { + const callbackInterval = Duration(milliseconds: 75); + _onBackspacePressedCallbackTimer = + Timer.periodic(callbackInterval, (timer) { + widget.state.onBackspacePressed!(); + }); + } + + void _stopOnBackspacePressedCallback() { + _onBackspacePressedCallbackTimer?.cancel(); + } }