[SOLVED] Flutter Tooltip on One Tap

Issue

This Content is from Stack Overflow. Question asked by Timothy Se

My app has several textfields and I want to have a tooltip so that users know can see the definition of each field.

I came across this answer, but it wasn’t helpful: Flutter Tooltip on One Tap. Therefore I decided to try and fix it myself.



Solution

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();
  }
}


This Question was asked in StackOverflow by Moiz Travadi and Answered by Nagual It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?