一键颤动工具提示

问题描述 投票:0回答:3

我想在单击而不是长按时显示工具提示。

有人可以帮我吗?

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,
        ),
      ),
    ),
  ),
)
flutter dart tooltip
3个回答
48
投票

最简单的方法是在 Tooltip 构造函数中使用

triggerMode
属性。

  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),
        )),
  )

36
投票

更新: 使用

triggerMode: TooltipTriggerMode.tap,
代替

此解决方案已过时
作为一个选项,您可以创建简单的包装器小部件
完整示例:

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

0
投票
final tooltipController = JustTheController();

JustTheTooltip(
            controller: tooltipController,
            triggerMode: TooltipTriggerMode.manual,
            preferredDirection: AxisDirection.up,
            content: const SizedBox(
              width: 300,
              child: Padding(
                padding: EdgeInsets.all(20),
                child: Text(
                  'This is tooltip text',
                  style: TextStyle(fontSize: 13, color: Colors.black),
                ),
              ),
            ),
            child: IconButton(
              onPressed: () {
                  tooltipController.showTooltip();
              },
              icon: const Icon(
                  Icons.stars,
                  size: 22,
                  color: AppColors.orange,
                ),
            ),
          ),

这个方法对我有用

© www.soinside.com 2019 - 2024. All rights reserved.