[Flutter]Notification

小黑
發佈於APP
2023/12/25閱讀時間約 1 分鐘

Notification是Flutter中一個重要的機制,在widget樹中,每個節點都可以分發通知,通知會沿著目前節點向上傳遞,所有父節點都可以透過NotificationListener來監聽通知。Flutter中將此由子向父的傳遞通知的機制稱為通知冒泡(Notification Bubbling)。通知冒泡和使用者觸摸事件冒泡是相似的,但有一點不同:通知冒泡可以中止,但使用者觸摸事件不行。


用法

NotificationListener(
onNotification: (notification){
switch (notification.runtimeType){
//你要監聽的類型與要做的事
}
},
child: ListView.builder(
itemCount: 10,
itemBuilder: (context, index) {
return ListTile(title: Text("$index"),);
}
),
);


客製化

class CustomNotification extends Notification {
CustomNotification(this.msg);
final String msg;
}


方法

CustomNotification("your_msg").dispatch(context)








7會員
78內容數
嗨,我是一名程式設計師,會在這分享開發與學習紀錄。
留言0
查看全部
發表第一個留言支持創作者!