Flutter增强提示工具插件extended_tooltip的使用

Flutter增强提示工具插件extended_tooltip的使用

该插件扩展了Flutter中的Tooltip功能,允许传递一个Widget作为提示信息。这个插件通过Overlay来构建提示框。

默认情况下,Dart的Tooltip只接受字符串或TextSpan,但通过此插件,您可以扩展其功能,传递任意的Widget作为提示信息。

提示框的位置会根据当前组件的大小和位置进行计算。

Example

使用方法

您可以前往/example文件夹查看更多的例子。

ExtendedToolTip(
  horizontalPosition: ExtendedTooltipPosition.left, // 设置提示框在水平方向上的位置
  message: Container( // 提示框的内容
    width: 200,
    height: 200,
    decoration: BoxDecoration(
      borderRadius: BorderRadius.circular(10),
      color: Colors.red,
    ),
    child: const Center(child: Text('My custom Message')), // 提示框中的文本
  ),
  child: Text(
    'ExtendToolTip Example', // 需要显示提示框的组件
    style: Theme.of(context).textTheme.headlineMedium,
  ),
)
ExtendedToolTip(
  message: IconButton(
    onPressed: () {
      setState(() {
        value++; // 点击图标后增加计数器
      });
    },
    icon: const Icon(Icons.add), // 图标按钮
  ),
  child: Text('tooltip interaction $value'), // 显示计数器的文本
),

测试平台

  • Windows
  • Web

需要测试其他平台。

完整示例Demo

以下是一个完整的示例,展示了如何在应用中使用ExtendedToolTip插件。

import 'package:flutter/material.dart';
import 'package:extended_tooltip/extended_tooltip.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Extended ToolTip Example',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Extended ToolTip Example'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  [@override](/user/override)
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int value = 0;

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ExtendedToolTip(
              horizontalPosition: ExtendedTooltipPosition.left,
              message: Container(
                width: 200,
                height: 200,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(10),
                  color: Colors.red,
                ),
                child: const Center(child: Text('My custom Message')),
              ),
              child: Text(
                'ExtendToolTip Example',
                style: Theme.of(context).textTheme.headlineMedium,
              ),
            ),
            const SizedBox(height: 30),
            const ExtendedToolTip(
              horizontalPosition: ExtendedTooltipPosition.right,
              message: CircleAvatar(
                radius: 30,
                child: Icon(Icons.person),
              ),
              child: Icon(Icons.person),
            ),
            const SizedBox(height: 30),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                ExtendedToolTip(
                  message: IconButton(
                    onPressed: () {
                      setState(() {
                        value++;
                      });
                    },
                    icon: const Icon(Icons.add),
                  ),
                  child: Text('tooltip interaction $value'),
                ),
              ],
            )
          ],
        ),
      ),
    );
  }
}

更多关于Flutter增强提示工具插件extended_tooltip的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter增强提示工具插件extended_tooltip的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter项目中使用extended_tooltip插件的示例代码。extended_tooltip插件允许开发者创建更丰富的、可定制的提示工具(Tooltip),相对于Flutter默认的Tooltip,它提供了更多的功能和样式选项。

首先,你需要在你的pubspec.yaml文件中添加extended_tooltip依赖:

dependencies:
  flutter:
    sdk: flutter
  extended_tooltip: ^x.y.z  # 替换为最新版本号

然后运行flutter pub get来获取依赖。

接下来,你可以在你的Flutter应用中使用ExtendedTooltip。以下是一个简单的示例,展示如何使用ExtendedTooltip来包裹一个按钮,并在悬停时显示一个自定义的提示:

import 'package:flutter/material.dart';
import 'package:extended_tooltip/extended_tooltip.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Extended Tooltip Example'),
        ),
        body: Center(
          child: ExtendedTooltip(
            // 设置提示内容
            text: 'This is a custom tooltip!',
            // 设置提示内容的样式
            textStyle: TextStyle(color: Colors.white, fontSize: 16),
            // 设置背景颜色和形状
            backgroundColor: Colors.blue,
            borderRadius: BorderRadius.circular(10),
            // 设置边距和箭头
            margin: EdgeInsets.all(8),
            arrowDecoration: BoxDecoration(
              color: Colors.blue,
            ),
            // 设置提示显示的条件,例如悬停或长按
            triggerMode: TriggerMode.hover,
            // 包裹的控件
            child: ElevatedButton(
              onPressed: () {},
              child: Text('Hover over me'),
            ),
          ),
        ),
      ),
    );
  }
}

在这个示例中:

  • ExtendedTooltiptext属性用于设置提示的内容。
  • textStyle属性用于设置提示文本的样式。
  • backgroundColorborderRadius属性用于设置提示背景的颜色和圆角。
  • margin属性用于设置提示的外边距。
  • arrowDecoration属性用于设置提示箭头的样式(在这个例子中,箭头颜色和背景颜色相同)。
  • triggerMode属性用于设置触发提示的模式,这里使用的是TriggerMode.hover,意味着当用户将鼠标悬停在按钮上时,会显示提示。在移动设备上,你可能想使用TriggerMode.longPress
  • child属性是你想要包裹的控件,这里是一个ElevatedButton

你可以根据需要调整这些属性,以实现你想要的提示效果。这个插件提供了很多自定义选项,使得提示工具可以更加符合你的UI设计需求。

回到顶部