Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Tooltip on One Tap

I want to show a tooltip on a single tap, not a long tap.

Can anyone help me with this?

Tooltip(
  message: e.title,
  child: Card(
    semanticContainer: true,
    child: Padding(
      padding: EdgeInsets.symmetric(
          vertical: 12,
          horizontal: 12
      ),
      child: Center(
        child: Image.network(
          e.icon,
          height: 40,
        ),
      ),
    ),
  ),
)
like image 263
Moiz Travadi Avatar asked Feb 11 '21 11:02

Moiz Travadi


People also ask

How do I show hover text in flutter?

You can wrap your text into a Tooltip widget. Show activity on this post. Tooltip(message: "This is the tooltip message", child: new Text("Show tooltip")); You can also provide a height, decoration and other parameters.

What is SuperTooltip?

SuperTooltip is derived from a widget I wrote for a client. It is super flexible and allows you to display ToolTips in the overlay of the screen. The whole screen gets covered with a typically translucent background color. Tapping on the background closes the Tooltip.

How do you make a custom Tooltip in flutter?

The easiest way would be to view the source code, copy it over to your project, change the direct references from import colors. dart'; to import 'package:flutter/src/material/colors. dart'; and then make the necessary change you want.


2 Answers

The easiest way to do it is to use triggerMode property inside Tooltip constructor.

  Tooltip(
    message: item.description ?? '',
    triggerMode: TooltipTriggerMode.tap,
    child: Text(item.label ?? '',
        style: TextStyle(
          fontWeight: FontWeight.w500,
          fontSize: 1.7.t.toDouble(),
          color: Colors.black.withOpacity(0.6),
        )),
  )
like image 58
pragmateek Avatar answered Oct 13 '22 00:10

pragmateek


UPDATE: use triggerMode: TooltipTriggerMode.tap, instead

THIS SOLUTION IS OBSOLETE
as an option you can create simple wrapper widget
full example:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: Home());
  }
}

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Home")),
      body: Center(
        child: MyTooltip(
          message: 'message',
          child: Image.network('https://picsum.photos/seed/picsum/200/300', height: 40),
        ),
      ),
    );
  }
}

class MyTooltip extends StatelessWidget {
  final Widget child;
  final String message;

  MyTooltip({@required this.message, @required this.child});

  @override
  Widget build(BuildContext context) {
    final key = GlobalKey<State<Tooltip>>();
    return Tooltip(
      key: key,
      message: message,
      child: GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: () => _onTap(key),
        child: child,
      ),
    );
  }

  void _onTap(GlobalKey key) {
    final dynamic tooltip = key.currentState;
    tooltip?.ensureTooltipVisible();
  }
}
like image 29
Nagual Avatar answered Oct 13 '22 01:10

Nagual