Flutter通知管理插件notifyx的功能

Flutter通知管理插件notifyx的功能

NotifyX

NotifyX 是一个 Flutter 插件,旨在通过现代且视觉上吸引人的用户界面来显示错误状态和其他通知。您可以创建包含颜色编码和图标的丰富警报和通知。它提供了每个场景的可定制按钮、标题和描述区域,是提升应用程序用户体验的完美解决方案。

安装插件

pubspec.yaml 文件中添加以下行:

dependencies:
    notifyx: ^1.0.9

然后,在终端中运行以下命令来安装插件:

flutter pub get

使用方法

视频演示了 NotifyX 插件的使用方法,并展示了不同类型的警报(成功、错误、信息、警告等)。NotifyX 允许您创建现代且可定制的警报,以增强 Flutter 项目的用户体验。

NotifyX 示例

基本用法

onTap: () {
  showAnimatedAlert(
    context: context,
    alertWidget: RichNotifyXWidget(
      type: AlertEnumTypes.successSoft,
      title: 'Example Title',
      description:
          'Lorem Ipsum is simply dummy text of the printing and typesetting industry',
    ),
  );
}

完全自定义

通过一些参数,您可以完全自定义警报以满足项目的需求。

onTap: () {
  showAnimatedAlert(
    context: context,
    alertWidget: RichNotifyXWidget(
      type: AlertEnumTypes.successSoft,
      title: 'Example Title',
      description:
          'Lorem Ipsum is simply dummy text of the printing and typesetting industry',
    ),
    duration: const Duration(seconds: 3), // 警报可见时长
    animationDuration: const Duration(milliseconds: 700), // 动画时长
    topPadding: 50.0, // 顶部内边距
    leftPadding: 16.0, // 左侧内边距
    rightPadding: 16.0, // 右侧内边距
  );
}

作为卡片组件使用

NotifyX 不仅允许您显示动画警报,还可以将警报用作静态组件。这使得在屏幕或页面的固定位置显示警报变得容易。以下是如何将 NotifyX 用作静态组件的示例:

静态组件示例

<Widget>[
  RichNotifyXWidget(
    type: AlertEnumTypes.infoSolid,
    title: 'Lorem Ipsum',
    description:
        'Lorem Ipsum is simply dummy text of the printing and typesetting industry. ',
  ),
  const SizedBox(height: 12),
  RichNotifyXWidget(
    type: AlertEnumTypes.errorSolid,
    title: 'Lorem Ipsum',
    description:
        'Lorem Ipsum is simply dummy text of the printing and typesetting industry. ',
  ),
  const SizedBox(height: 12),
  RichNotifyXWidget(
    type: AlertEnumTypes.primarySolid,
    title: 'Lorem Ipsum',
    description:
        'Lorem Ipsum is simply dummy text of the printing and typesetting industry. ',
  ),
  const SizedBox(height: 12),
  RichNotifyXWidget(
    type: AlertEnumTypes.warningSolid,
    title: 'Lorem Ipsum',
    description:
        'Lorem Ipsum is simply dummy text of the printing and typesetting industry. ',
  ),
  const SizedBox(height: 12),
  RichNotifyXWidget(
    type: AlertEnumTypes.successSolid,
    title: 'Lorem Ipsum',
    description:
        'Lorem Ipsum is simply dummy text of the printing and typesetting industry. ',
  ),
  const SizedBox(height: 12),
  RichNotifyXWidget(
    type: AlertEnumTypes.infoSoft,
    title: 'Lorem Ipsum',
    description:
        'Lorem Ipsum is simply dummy text of the printing and typesetting industry. ',
  ),
  const SizedBox(height: 12),
  RichNotifyXWidget(
    type: AlertEnumTypes.errorOutlined,
    title: 'Lorem Ipsum',
    description:
        'Lorem Ipsum is simply dummy text of the printing and typesetting industry. ',
  ),
]

添加自定义链接按钮

使用 NotifyX,您可以使警报更具交互性,通过添加可自定义的链接按钮。以下是如何在警报中包含链接按钮的示例:

链接按钮示例

<Widget>[
  RichNotifyXWidget(
    type: AlertEnumTypes.infoSoft,
    title: 'Lorem Ipsum',
    description:
        'Lorem Ipsum is simply dummy text of the printing and typesetting industry. ',
    linkButton: LinkButtonModel(onTap: () {}, title: 'Link Button'),
  ),
  const SizedBox(height: 12),
  RichNotifyXWidget(
    type: AlertEnumTypes.primaryOutlined,
    title: 'Lorem Ipsum',
    description:
        'Lorem Ipsum is simply dummy text of the printing and typesetting industry. ',
    linkButton: LinkButtonModel(onTap: () {}, title: 'Link Button'),
  ),
  const SizedBox(height: 12),
  RichNotifyXWidget(
    type: AlertEnumTypes.errorSolid,
    title: 'Lorem Ipsum',
    description:
        'Lorem Ipsum is simply dummy text of the printing and typesetting industry. ',
    linkButton: LinkButtonModel(onTap: () {}, title: 'Link Button'),
  ),
]

自定义属性

NotifyX 组件提供了多种属性,帮助您完全自定义警报。以下是支持的属性及其描述:

  • type (AlertEnumTypes, 必填): 指定警报类型。例如:成功、错误、信息、警告等。
  • title (String?): 警报的标题文本。可选。
  • description (String?): 警报的描述文本。可选。
  • titleTextStyle (TextStyle?): 允许您定义标题文本的自定义样式。
  • descriptionTextStyle (TextStyle?): 允许您定义描述文本的自定义样式。
  • suffixIcon (Widget?): 在警报右侧添加自定义图标或组件。
  • hasCancelIcon (bool): 控制取消(关闭)图标是否可见。默认为 true
  • cancelOnTap (void Function()?): 定义点击取消图标时执行的操作。可选。
  • linkButton (LinkButtonModel?): 允许您在警报底部添加可自定义的链接按钮。

警报类型

您不需要像这样使用 type 参数:type: AlertEnumTypes.infoOutlined。相反,您可以按如下方式自定义:

type: AlertEnumTypes(
  color: Colors.blue,
  prefixIcon: Icon(Icons.abc),
)

或者更简单的形式:

type: AlertEnumTypes(
  color: Colors.blue,
)

这允许您根据需要自定义颜色和前缀图标。

一些截图

示例截图

发布历史

  • 1.0.0
    • NotifyX 的初始发布。
    • 添加了动态和可定制的警报组件。
    • 支持各种警报类型(成功、错误、信息、警告)。
    • 包括标题、描述、图标和自定义链接按钮的选项。
    • 提供优雅且带有动画效果的警报体验。

贡献者

感谢这些对该项目做出贡献的优秀人士:

贡献者 GitHub
ozcantolgahan ozcantolgahan
mertcanbakir94 mertcanbakir94

许可证

本项目采用 MIT 许可证。详情请参阅 LICENSE 文件。

示例代码

以下是一个完整的示例代码,展示了如何在 Flutter 应用程序中使用 NotifyX 插件:

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

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

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

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          showAnimatedAlert(
            context: context,
            bottomPadding: 50,
            leftPadding: 24,
            rightPadding: 24,
            alertWidget: RichNotifyXWidget(
              type: AlertEnumTypes.errorSolid,
              title: 'Example Title',
              description:
                  'Lorem Ipsum is simply dummy text of the printing and typesetting industry',
            ),
          );
        },
        child: const Icon(Icons.send),
      ),
      body: Center(
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              RichNotifyXWidget(
                type: AlertEnumTypes(
                  color: Colors.blue,
                ),
                title: 'Lorem Ipsum',
                description:
                    'Lorem Ipsum is simply dummy text of the printing and typesetting industry. ',
              ),
              const SizedBox(height: 12),
              RichNotifyXWidget(
                type: AlertEnumTypes.errorSolid,
                title: 'Lorem Ipsum',
                description:
                    'Lorem Ipsum is simply dummy text of the printing and typesetting industry. ',
              ),
              const SizedBox(height: 12),
              RichNotifyXWidget(
                type: AlertEnumTypes.primarySolid,
                title: 'Lorem Ipsum',
                description:
                    'Lorem Ipsum is simply dummy text of the printing and typesetting industry. ',
                hasCancelIcon: false,
              ),
              const SizedBox(height: 12),
              RichNotifyXWidget(
                type: AlertEnumTypes.errorOutlined,
                title: 'Lorem Ipsum',
                description:
                    'Lorem Ipsum is simply dummy text of the printing and typesetting industry. ',
                hasCancelIcon: false,
                linkButton: LinkButtonModel(onTap: () {}, title: 'Link Button'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

希望这些信息对您有所帮助!如果您有任何问题或需要进一步的帮助,请随时提问。


更多关于Flutter通知管理插件notifyx的功能的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter通知管理插件notifyx的功能的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是一个关于Flutter通知管理插件notifyx的功能展示和相关代码案例。notifyx是一个功能强大的Flutter插件,用于在Android和iOS上发送本地通知。以下是一些主要功能及其代码示例:

主要功能

  1. 显示基本通知
  2. 安排定时通知
  3. 更新和取消通知
  4. 处理通知点击事件
  5. 设置通知渠道(仅Android)

依赖配置

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

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

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

初始化

在应用的入口文件(通常是main.dart)中初始化NotifyX

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

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  NotifyX.init(
    onNotificationOpened: (notification) {
      // 处理通知点击事件
      print('Notification clicked: ${notification.payload}');
    },
  );
  runApp(MyApp());
}

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

显示基本通知

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

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('NotifyX Demo'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            NotifyX.show(
              title: 'Hello',
              body: 'This is a basic notification!',
              payload: 'notification_payload_1',  // 可选,用于处理点击事件
            );
          },
          child: Text('Show Notification'),
        ),
      ),
    );
  }
}

安排定时通知

import 'dart:async';

// 在按钮点击事件中
onPressed: () async {
  Timer(Duration(seconds: 10), () {
    NotifyX.show(
      title: 'Scheduled',
      body: 'This is a scheduled notification!',
      payload: 'notification_payload_2',
    );
  });
},

更新和取消通知

// 更新通知
NotifyX.update(
  id: 'notification_id_1',  // 通知的ID
  title: 'Updated Title',
  body: 'This notification has been updated!',
);

// 取消通知
NotifyX.cancel('notification_id_1');

设置通知渠道(仅Android)

// 在应用启动时配置
if (Platform.isAndroid) {
  NotifyX.createChannel(
    id: 'channel_id_1',
    name: 'Channel 1',
    description: 'Channel 1 description',
    importance: Importance.high,  // 可选值:low, default, high, max
  );
}

// 发送通知时指定渠道
NotifyX.show(
  channelId: 'channel_id_1',  // 仅在Android上有效
  title: 'Channel Notification',
  body: 'This notification uses a specific channel!',
);

处理通知点击事件

在初始化NotifyX时已经设置了onNotificationOpened回调,该回调会在用户点击通知时被调用。你可以在这个回调中处理点击事件,例如导航到应用的某个页面或执行其他操作。

NotifyX.init(
  onNotificationOpened: (notification) {
    // 处理通知点击事件
    print('Notification clicked: ${notification.payload}');
    // 例如,根据payload导航到不同页面
    if (notification.payload == 'notification_payload_1') {
      // 导航逻辑
    }
  },
);

这些代码示例展示了如何使用notifyx插件在Flutter应用中发送、更新、取消和处理本地通知。你可以根据需求进一步扩展和定制这些功能。

回到顶部