Flutter iOS风格通知栏插件cupertino_notification_bar_ui的使用

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

Flutter iOS风格通知栏插件cupertino_notification_bar_ui的使用

你好,欢迎来到这里!

我是 Delwin,一名充满热情的 FlutterFire 开发者。

简介

这是一个为 Flutter 在 Android、iOS、Windows 和 Linux 上设计的包。它提供了类似于 iOS 通知中心的可移除容器的 UI 设计。这个想法来源于 StackOverflow 上的一个问题。

iOS风格的通知栏

特点

你可以以堆叠的方式组织 UI 组件。这些组件是可以移除的,并且点击时可以触发一个函数。

StackedListItem 是一个内置的无状态小部件,可以与 StackedList 结合使用来显示组件。你也可以使用 Containers 来实现相同的功能。

开始使用

依赖

pubspec.yaml 文件中添加包:

dependencies:
  cupertino_notification_bar_ui: ^1.0.101

或者,你的编辑器可能支持 flutter pub get。请查阅你的编辑器文档了解更多信息。

你也可以通过以下命令添加依赖:

flutter pub add cupertino_notification_bar_ui
导入

现在可以在 Dart 代码中使用:

import 'package:cupertino_notification_bar_ui/cupertino_notification_bar_ui.dart';

使用

该包接受一个包含 StackedListItemContainer 的子项列表作为参数。 下面是一个设置示例:

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

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

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

  // 这个小部件是您的应用程序的根节点。
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

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> {
  // 定义 `StackItems` 这里。
  // 另外,你可以使用高度为 75px 的 `Containers`
  final stackItems = [
    StackedListItem(
      leading: const CircleAvatar(
          backgroundImage: NetworkImage(
              "https://cdn.pixabay.com/photo/2014/02/27/16/10/flowers-276014_960_720.jpg")),
      title: "Hi",
      subtitle: const Text("It's nice to see you here"),
      trailing: const Icon(Icons.link_rounded),
    ),
    StackedListItem(
      leading: const CircleAvatar(
          backgroundImage: NetworkImage(
              "https://cdn.pixabay.com/photo/2014/02/27/16/10/flowers-276014_960_720.jpg")),
      title: "This happens to be my first open-source",
      subtitle: const Text("We all start somewhere, right?"),
      trailing: const Icon(Icons.link_rounded),
    ),
    StackedListItem(
      leading: const CircleAvatar(
          backgroundImage: NetworkImage(
              "https://cdn.pixabay.com/photo/2014/02/27/16/10/flowers-276014_960_720.jpg")),
      title: "Thanks for checking this out!",
      subtitle: const Text("Happy coding :)"),
      trailing: const Icon(Icons.link_rounded),
    )
  ];

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.deepOrange.shade200,
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: StackedList(children: stackItems));
  }
}

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

1 回复

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


当然,下面是一个关于如何使用cupertino_notification_bar_ui插件在Flutter应用中实现iOS风格通知栏的示例代码。这个插件允许你以iOS风格显示通知栏,非常适合需要模仿iOS用户体验的应用。

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

dependencies:
  flutter:
    sdk: flutter
  cupertino_notification_bar_ui: ^最新版本号  # 请替换为实际的最新版本号

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

接下来,在你的Flutter应用中,你可以按照以下步骤使用cupertino_notification_bar_ui插件:

1. 导入插件

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

import 'package:cupertino_notification_bar_ui/cupertino_notification_bar_ui.dart';

2. 创建通知栏控制器

你需要一个控制器来管理通知栏的显示和隐藏。

final CupertinoNotificationBarController _notificationBarController = CupertinoNotificationBarController();

3. 构建通知栏

在你的UI构建函数中,添加通知栏组件,并使用控制器来控制它。

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Cupertino Notification Bar Example'),
        ),
        body: Stack(
          children: [
            // 其他UI组件
            CupertinoNotificationBar(
              controller: _notificationBarController,
              child: Container(
                color: Colors.blue,
                alignment: Alignment.center,
                child: Text(
                  '这是一个通知',
                  style: TextStyle(color: Colors.white),
                ),
              ),
            ),
          ],
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            // 显示通知栏
            _notificationBarController.show();
          },
          tooltip: '显示通知',
          child: Icon(Icons.add),
        ),
      ),
    );
  }
}

4. 控制通知栏的显示和隐藏

你可以通过调用控制器的方法来显示或隐藏通知栏。例如,在上面的代码中,我们已经在浮动操作按钮的点击事件中调用了_notificationBarController.show()来显示通知栏。

你也可以添加隐藏通知栏的逻辑,例如:

floatingActionButton: FloatingActionButton(
  onPressed: () {
    if (_notificationBarController.isVisible) {
      _notificationBarController.hide();
    } else {
      _notificationBarController.show();
    }
  },
  tooltip: '切换通知',
  child: Icon(Icons.add),
),

完整示例

将以上各部分整合起来,你的完整示例代码可能如下所示:

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

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

class MyApp extends StatelessWidget {
  final CupertinoNotificationBarController _notificationBarController = CupertinoNotificationBarController();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Cupertino Notification Bar Example'),
        ),
        body: Stack(
          children: [
            // 其他UI组件
            CupertinoNotificationBar(
              controller: _notificationBarController,
              child: Container(
                color: Colors.blue,
                alignment: Alignment.center,
                child: Text(
                  '这是一个通知',
                  style: TextStyle(color: Colors.white),
                ),
              ),
            ),
          ],
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            if (_notificationBarController.isVisible) {
              _notificationBarController.hide();
            } else {
              _notificationBarController.show();
            }
          },
          tooltip: '切换通知',
          child: Icon(Icons.add),
        ),
      ),
    );
  }
}

这样,你就成功地在Flutter应用中集成了cupertino_notification_bar_ui插件,并实现了iOS风格的通知栏功能。

回到顶部