Flutter桌面通知插件macos_toast_kit的使用

Flutter桌面通知插件macos_toast_kit的使用

macos_toast_kit

一个用于Flutter的新插件项目。

使用入门

Flutter插件,支持类似macOS的系统通知。

    1. 支持应用程序模式
    1. 支持位置设置

看看效果

示例代码

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

import 'package:flutter/services.dart';
import 'package:macos_toast_kit/macos_toast_kit.dart';

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

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

  [@override](/user/override)
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = '未知';
  final _macosToastKitPlugin = MacosToastKit();

  [@override](/user/override)
  void initState() {
    super.initState();
    initPlatformState();
  }

  // 平台消息是异步的,因此我们在异步方法中初始化。
  Future<void> initPlatformState() async {
    String platformVersion;
    // 平台消息可能会失败,所以我们使用try/catch来处理PlatformException。
    // 我们还处理了消息可能返回null的情况。
    try {
      platformVersion =
          await _macosToastKitPlugin.getPlatformVersion() ?? '未知平台版本';
    } on PlatformException {
      platformVersion = '获取平台版本失败。';
    }

    // 如果在异步平台消息还在进行时,该小部件从树中被移除,我们希望丢弃回复而不是调用setState来更新我们的非存在的外观。
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('插件示例应用'),
        ),
        body: Center(
          child: GestureDetector(
            onTap: () {
              // MacosToastKit().show();
              MacosToastKit().show(systemImageName: "hammer.circle.fill", toastContent: "已拷贝");
              // MacosToastKit().show(systemImageName: "hammer.circle.fill", toastContent: "已拷贝", applicationMode: 1);
              // MacosToastKit().show(systemImageName: "hammer.circle.fill", toastContent: "已拷贝", applicationMode: 0, position: 0);
              // MacosToastKit().show(systemImageName: "hammer.circle.fill", toastContent: "已拷贝", applicationMode: 0, position: 1);
              // MacosToastKit().show(systemImageName: "hammer.circle.fill", toastContent: "已拷贝", applicationMode: 2, position: 2);
              // MacosToastKit().show(systemImageName: "hammer.circle.fill", toastContent: "已拷贝", applicationMode: 2, position: 3);
              // MacosToastKit().show(systemImageName: "hammer.circle.fill", toastContent: "已拷贝", applicationMode: 2, position: 4);
            },
            child: Text('运行在: $_platformVersion\n'),
          ),
        ),
      ),
    );
  }
}

更多关于Flutter桌面通知插件macos_toast_kit的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter桌面通知插件macos_toast_kit的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


macos_toast_kit 是一个用于在macOS上显示Toast通知的Flutter插件。它允许开发者在macOS应用程序中显示简单的、非阻塞的通知消息,类似于其他平台上的Toast通知。

安装

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

dependencies:
  flutter:
    sdk: flutter
  macos_toast_kit: ^0.1.0  # 请检查最新版本

然后运行flutter pub get来安装依赖。

基本用法

以下是如何在Flutter应用程序中使用macos_toast_kit显示Toast通知的基本示例:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('macOS Toast Kit Example'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: () {
              // 显示Toast通知
              MacosToastKit.showToast(
                title: 'Hello, macOS!',
                subtitle: 'This is a toast notification.',
                duration: Duration(seconds: 3),
              );
            },
            child: Text('Show Toast'),
          ),
        ),
      ),
    );
  }
}

参数说明

  • title: Toast通知的标题。
  • subtitle: Toast通知的子标题(可选)。
  • duration: Toast通知显示的持续时间。

注意事项

  1. 平台限制: macos_toast_kit 仅适用于macOS平台。在其他平台上调用该插件可能会导致错误或不兼容。

  2. 权限: 在某些情况下,macOS可能会要求用户授予应用程序显示通知的权限。确保在应用程序中处理这些权限请求。

  3. 样式定制: macos_toast_kit 提供了默认的Toast样式,但你可以根据需要通过修改插件的源码来定制样式。

高级用法

如果你需要对Toast通知进行更高级的控制,例如自定义图标、按钮等,可能需要直接修改插件源码或寻找其他插件。

示例代码

以下是一个稍微复杂一点的示例,展示如何根据不同的条件显示不同的Toast通知:

void showCustomToast(BuildContext context) {
  final theme = Theme.of(context);

  MacosToastKit.showToast(
    title: 'Custom Toast',
    subtitle: 'This is a custom toast with a specific theme.',
    duration: Duration(seconds: 5),
    // 你可以在这里添加更多的自定义参数
  );
}
回到顶部