Flutter浮动通知插件flutter_floating_notification的使用

发布于 1周前 作者 zlyuanteng 来自 Flutter

Flutter浮动通知插件flutter_floating_notification的使用

本文将介绍如何在Flutter项目中使用flutter_floating_notification插件来创建浮动通知。我们将通过几个简单的步骤来展示如何设置和使用这个插件。

1. 引入依赖

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

dependencies:
  flutter:
    sdk: flutter
  flutter_floating_notification: ^版本号 # 请替换为最新版本号

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

2. 创建浮动通知

2.1 使用局部队列

FlutterFloatNotification() 维护一个局部队列,这意味着每个通知都会独立处理。

示例代码

import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter_floating_notification/flutter_floating_notification.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.blue),
        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.primary,
        foregroundColor: Colors.white,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ElevatedButton(
              onPressed: () async {
                // 生成随机数
                final random = Random();

                final color = Color.fromARGB(
                  255,
                  random.nextInt(255),
                  random.nextInt(255),
                  random.nextInt(255),
                );

                final double randomHeight = 100 + random.nextInt(100).toDouble();

                final v = await FlutterFloatNotification().showFloatingBar<int>(
                  context,
                  childBuilder: (context, dismiss) {
                    return Container(
                      color: color,
                      height: randomHeight,
                      alignment: Alignment.center,
                      child: ElevatedButton(
                        onPressed: () => dismiss(value: color.value),
                        child: const Text('Dismiss'),
                      ),
                    );
                  },
                );

                debugPrint('v: $v');
              },
              child: const Text('Show Flush'),
            ),
          ],
        ),
      ),
    );
  }
}

2.2 使用全局队列

FlutterFloatNotification.global() 包含一个全局队列,这使得所有通知共享同一个队列,按顺序显示。

示例代码

ElevatedButton(
  onPressed: () async {
    // 生成随机数
    final random = Random();

    final color = Color.fromARGB(
      255,
      random.nextInt(255),
      random.nextInt(255),
      random.nextInt(255),
    );

    final double randomHeight = 100 + random.nextInt(100).toDouble();

    final v = await FlutterFloatNotification.global().showFloatingBar<int>(
      context,
      direction: FloatingGestureDirection.vertical,
      childBuilder: (context, FlushDismiss<int> dismiss) {
        return Container(
          color: color,
          height: randomHeight,
          alignment: Alignment.center,
          child: ElevatedButton(
            onPressed: () => dismiss(value: color.value),
            child: const Text('Dismiss Global'),
          ),
        );
      },
    );

    debugPrint('v: $v');
  },
  child: const Text('Show Global Flush'),
),

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

1 回复

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


当然,以下是如何在Flutter项目中集成和使用flutter_floating_notification插件的示例代码。这个插件允许你在应用中显示浮动通知。

首先,确保你已经在pubspec.yaml文件中添加了flutter_floating_notification依赖:

dependencies:
  flutter:
    sdk: flutter
  flutter_floating_notification: ^最新版本号 # 请替换为最新版本号

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

接下来,你可以按照以下步骤在你的Flutter应用中实现浮动通知。

1. 导入插件

在你的Dart文件中导入插件:

import 'package:flutter_floating_notification/flutter_floating_notification.dart';

2. 初始化插件

在你的主应用文件(通常是main.dart)中,初始化FlutterFloatingNotification

void main() {
  runApp(MyApp());
  FlutterFloatingNotification.initialize(); // 初始化插件
}

3. 显示浮动通知

你可以在任何地方调用FlutterFloatingNotification.showNotification方法来显示一个浮动通知。例如,在一个按钮点击事件中:

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

void main() {
  runApp(MyApp());
  FlutterFloatingNotification.initialize(); // 初始化插件
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter Floating Notification Example'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: () {
              showFloatingNotification();
            },
            child: Text('Show Notification'),
          ),
        ),
      ),
    );
  }

  void showFloatingNotification() {
    FlutterFloatingNotification.showNotification(
      title: 'Hello',
      subTitle: 'This is a floating notification!',
      backgroundColor: Colors.blue,
      onClick: () {
        // 点击通知时的回调
        print('Notification clicked!');
      },
      // 其他可选参数,如icon, duration等
    );
  }
}

4. 自定义通知样式(可选)

FlutterFloatingNotification.showNotification方法提供了许多可选参数,允许你自定义通知的样式和行为。例如,你可以设置通知的图标、显示时间、位置等。

void showCustomFloatingNotification() {
  FlutterFloatingNotification.showNotification(
    title: 'Custom',
    subTitle: 'This is a custom floating notification!',
    icon: Icon(Icons.alarm),
    backgroundColor: Colors.green,
    textColor: Colors.white,
    duration: Duration(seconds: 5), // 通知显示5秒
    position: NotificationPosition.bottom, // 显示在底部
    onClick: () {
      print('Custom notification clicked!');
    },
  );
}

你可以根据需要调用showCustomFloatingNotification方法,或者将其集成到你的UI逻辑中。

这样,你就成功地在Flutter应用中集成了flutter_floating_notification插件,并能够显示自定义的浮动通知了。

回到顶部