Home > Net >  Flutter show tooltip on tap and do other function onLongPress
Flutter show tooltip on tap and do other function onLongPress

Time:11-17

I wanted to show a tooltip when I tap my gesture detector and do different things when the GestureDetector is long pressed, How can I achieve this? I have written some code about it but on long press still shows a tooltip rather than accessing my selectDate() function

this is my current code:

 GestureDetector(
  onTap: () {
    final dynamic tooltip = _toolTipKey.currentState;
    tooltip.ensureTooltipVisible();
  },
  onLongPress: () {
    if (widget.ticketData['status'] == 'active') {
      showDialog(
        context: context,
        builder: (context) {
          return ReusableConfirmationDialog(
            titleText: 'changeDueDateTitle'.tr(),
            contentText: 'changeDueDateDesc'.tr(),
            declineButtonText: 'cancel'.tr(),
            confirmButtonText: 'change'.tr(),
            onDecline: () {
              Navigator.pop(context);
            },
            onConfirm: () {
              DevMode.log('start changing the due date');
              _selectDate(context);
            },
          );
        },
      );
    }
  },
  child: Tooltip(
    key: _toolTipKey,
    message: "Hello",
    child: Container(
      padding: const EdgeInsets.symmetric(horizontal: 11, vertical: 5),
      decoration: BoxDecoration(
        color: formBackgroundColor,
        borderRadius: BorderRadius.circular(15),
      ),
      child: Row(
        children: [
          Image.asset(
            'assets/logo/calendar.png',
            width: 20,
            height: 20,
          ),
          const SizedBox(width: 5),
        ],
      ),
    ),
  ),
),

CodePudding user response:

Use triggerMode: TooltipTriggerMode.manual and key to show tooltip

 final GlobalKey<TooltipState> tooltipkey = GlobalKey<TooltipState>();
 Tooltip(
  message: "long press to open dialog",
  key: tooltipkey,
  triggerMode: TooltipTriggerMode.manual,
  child: GestureDetector(
    onTap: () {
      tooltipkey.currentState?.ensureTooltipVisible();
    },
    onLongPress: () {
      log("show dialog");
    },
    child: Text("dialog"),
  )),
);

More about Tooltip

CodePudding user response:

create this function in your code

This line triggerMode: TooltipTriggerMode.tap, will allow you to show tooltip on single tap.

 Widget customToolTip({String? text}) {
      return Tooltip(
        message: text ?? "",
        triggerMode: TooltipTriggerMode.tap,
        showDuration: const Duration(seconds: 30),
        child: const Icon(Icons.help_outline, color: AppColors.buttonColor, size: 20),
      );
}
  • Related