/
notification.dart
58 lines (54 loc) · 1.83 KB
/
notification.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import 'package:flutter/material.dart';
class NotificationPage extends StatefulWidget {
NotificationPage(this.title, {Key key}) : super(key: key);
final String title;
@override
_NotificationPageState createState() => _NotificationPageState();
}
class _NotificationPageState extends State<NotificationPage> {
String _msg = "";
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: NotificationListener<TestNotification>(
onNotification: (notification) {
setState(() {
_msg += notification.msg + " ";
});
return true;
},
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
// RaisedButton(
// onPressed: () => TestNotification("这是通知内容~~").dispatch(context),
// child: Text("发出通知"),
// ),
//这个context是根Context,而NotificationListener是监听的子树,所以我们通过Builder来构建RaisedButton,来获得按钮位置的context。
Builder(
builder: (context) {
return RaisedButton(
//按钮点击时分发通知
onPressed: () =>
TestNotification("这是通知内容~~").dispatch(context),
child: Text("发出通知"),
textColor: Colors.white,
);
},
),
Text(_msg)
],
),
),
));
}
}
///自定义冒泡通知
class TestNotification extends Notification {
TestNotification(this.msg);
final String msg;
}