功能通知器
在更新后向用户通知您应用中的新功能。
该软件包提供什么?
- 显示新功能的通知。
- 一旦用户关闭通知,除非您通过编程方式持久化它,否则它不会再次出现。
- 通过访问方法来创建您的自定义通知,这些方法允许您根据用户交互来持久化和更改自定义通知的状态。
示例图库
![]() |
![]() |
|---|---|
![]() |
![]() |
用法
将其添加到您的软件包的 pubspec.yaml 文件中
feature_notifier: latest
将 feature notifier 导入您的代码
import 'package:feature_notifier/feature_notifier.dart';
初始化
在您的 main() 函数中,通过调用 FeatureNotifier.init() 来初始化并 await feature notifier。不这样做可能会导致副作用,干扰预期行为。
void main() async {
await FeatureNotifier.init();
runApp(const MyApp());
}
功能通知器
此软件包提供四 (4) 种独特且高度可定制的功能通知器,实现方式略有不同。
1. 条形通知器
返回一个简单且可定制的水平条。
FeatureBarNotifier(
title: "We just released a new feature!",
featureKey: 2,
onClose: () {},
onTapCard: () {},
showIcon: true,
)
2. 卡片通知器
返回一个简单且可定制的卡片
FeatureCardNotifier(
title: "We just released a new feature!",
description: "Checkout the nwq feature that we just released and make."
featureKey: 2,
onClose: () {},
onTapCard: () {},
showIcon: true,
//use the hasButton parameter to display a button
hasButton:true,
)
3. 警报对话框通知器
返回一个简单且可定制的警报对话框。
FeatureAlertNotifier.notify(
context,
title: "We just released a new feature!",
description: "Checkout the nwq feature that we just released and make."
onClose: () {},
featureKey: 3,
hasButton: true,
);
警报对话框通知器的一个常见用例是当一个屏幕完成构建并标记为已构建时显示。因此,您需要在状态化小部件的 initState() 中访问 WidgetsBinding.instance.addPostFrameCallback() 回调方法,以调用 FeatureAlertNotifier 上的 notify() 方法。
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
print("Build Completed");
FeatureAlertNotifier.notify(
context,
title: "We just released a new feature!",
description: "Checkout the nwq feature that we just released and make."
onClose: () {},
featureKey: 3,
hasButton: true,
);
});
}
4. 底部模态框通知器
返回一个简单且可定制的底部模态框。
FeatureBottomModalSheetNotifier.notify(
context,
title: "We just released a new feature!",
description: "Checkout the nwq feature that we just released and make."
onClose: () {},
featureKey: 3,
hasButton: true,
);
与警报对话框通知器一样,警报通知器的一个常见用例是在屏幕完成构建并标记为已构建时显示。因此,您需要在状态化小部件的 initState() 中访问 WidgetsBinding.instance.addPostFrameCallback() 回调方法,以调用 FeatureAlertNotifier 上的 notify() 方法。
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
print("Build Completed");
FeatureBottomModalSheetNotifier.notify(
context,
title: "Alert Notifier",
description: "Modal sheet is a good way to display a feature",
onClose: () {},
featureKey: 3,
hasButton: true,
);
});
}
创建自定义通知器
feature notifier 软件包提供开箱即用的方法,允许您创建自定义通知并操作状态。
以下方法对于创建自定义通知器 UI 及其持久化功能非常有用。
-
FeatureNotifier.close()关闭您的自定义 Feature Notifier 小部件。要更新状态并从小部件树中移除当前显示的小部件,您需要使用
FeatureNotifier.isClosed()方法,该方法返回一个布尔值,用于读取当前显示或已关闭的功能通知器的值,并相应地显示或隐藏您的自定义小部件。 -
FeatureNotifier.isClosed()检查某个功能通知器(自定义或非自定义)是否已关闭。
这在您想更新 UI 的状态以显示或隐藏已打开(
isClosed() 为 false)或已关闭(isClosed() 为 true)的自定义功能通知器时非常有用。 -
FeatureNotifier.persist()在某个功能通知器先前已关闭后,保持其处于活动状态。它通过接受
featureKey作为参数来实现这一点,以便可以唯一地标识该特定功能。当您想重置isClosed()值为false时,请调用此方法。当您选择在新登录后显示功能通知器时,此方法很有用,这意味着在用户注销时必须调用此方法,以便它可以被持久化。 -
FeatureNotifier.persistAll()在所有功能通知器先前已关闭后,将它们全部保持活动状态。当您想在新登录后显示所有功能通知器时,请调用此方法,这意味着在用户注销时必须调用此方法,以便所有值都可以被持久化/重置。要持久化单个功能通知器,请使用
FeatureNotifier.persist()并传入featureKey来标识要持久化的功能。
参数
这是您可以用来定制功能通知器类和方法的参数列表。
int featureKey; /// to uniquely identify a feature
String title;
Color? titleColor;
double? titleFontSize;
String description;
Color? descriptionColor;
double? descriptionFontSize;
String? buttonText;
Color? buttonTextColor;
double? buttonTextFontSize;
Color? buttonBackgroundColor;
Widget? icon;
bool? showIcon;
void Function() onClose;
void Function()? onTapButton;
Color? backgroundColor;
Color? strokeColor;
double? strokeWidth;
void Function() onTapCard;
bool? hasButton;
贡献
当然,该项目是开源的,您可以通过 仓库链接 为其做出贡献
- 如果您发现了一个 bug,请打开一个 issue。
- 如果您有功能请求,请打开一个 issue。
- 如果您想贡献,请提交一个 pull request。




