Flutter学习笔记 --事件通知

在Flutter中Notification会沿着当前的Context节点从下向上传递,所有父节点都可以通过NotificationListener监听通知,有点类似于冒泡事件。常用的通知监听有LayoutChangeNotification、SizeChangedLayoutNotifier和ScrollNotification等。

例如监听ListView的滚动状态,就可以使用ScrollNotification,代码如下:

class _ScrollNotificationState extends State {
  String _message = '通知';

  get palyload => _message;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: Column(
          children: [
            Container(
              color: Colors.green,
              height: 50,
              width: MediaQuery.of(context).size.width,
              child: Center(
                child: Text(
                  _message,
                  style: const TextStyle(fontSize: 30, color: Colors.white),
                ),
              ),
            ),
            Expanded(
                child: NotificationListener(
              child: ListView.builder(
                itemBuilder: (BuildContext, index) {
                  return ListTile(
                    title: Text('第$index个猴子'),
                  );
                },
                itemCount: 50,
              ),
              onNotification: (scrollNotification) {
                if (scrollNotification is ScrollStartNotification) {
                  _onScrollStart(scrollNotification.metrics);
                } else if (scrollNotification is ScrollUpdateNotification) {
                  _onScrollUpdate(scrollNotification.metrics);
                } else if (scrollNotification is ScrollEndNotification) {
                  _onScrollEnd(scrollNotification.metrics);
                }
                return false;
              },
            ))
          ],
        ));
  }

  void _onScrollStart(ScrollMetrics scrollMetrics) {
    print(scrollMetrics.pixels);
    setState(() {
      this._message = '滚动开始';
    });
    showNotification();
  }

  void _onScrollUpdate(ScrollMetrics scrollMetrics) {
    print(scrollMetrics.pixels);
    setState(() {
      this._message = '滚动中';
    });

    showNotification();
    FlutterAppBadger.updateBadgeCount(10);

  }

  void _onScrollEnd(ScrollMetrics scrollMetrics) {
    print(scrollMetrics.pixels);

    setState(() {
      this._message = '滚动结束';
    });
    showNotification();
  }

  late FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;

  @override
  void initState() {
    super.initState();
    flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
    var android = const AndroidInitializationSettings('@mipmap/ic_launcher');
    var ios = const IOSInitializationSettings();
    var initSettings = InitializationSettings(android: android, iOS: ios);
    flutterLocalNotificationsPlugin.initialize(initSettings);
  }
  showNotification() async {
    var android = const AndroidNotificationDetails('channel id', 'channel NAME',
    priority: Priority.high, importance: Importance.max);

    var iOS = IOSNotificationDetails();
    var platform = NotificationDetails(android: android, iOS: iOS);
    await flutterLocalNotificationsPlugin.show(0, '通知', '通知:$_message', platform, payload: '通知界面');
  }

  Future onSelectNotification(String payload) async {
    await Navigator.push(context, MaterialPageRoute(builder: (context) => ScrollNotificationPage(title: palyload)),);
  }
}

通知栏消息,在Flutter中想要将通知显示在手机的通知栏上,就需要使用Flutter库:flutter_local_notifications。使用方法如下:

  • 添加依赖,在pubspec.yaml文件中添加:dependencies:
    flutter_local_notifications: ^9.1.5
    *初始化参数:
late FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;

  @override
  void initState() {
    super.initState();
    flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
    var android = const AndroidInitializationSettings('@mipmap/ic_launcher');
    var ios = const IOSInitializationSettings();
    var initSettings = InitializationSettings(android: android, iOS: ios);
    flutterLocalNotificationsPlugin.initialize(initSettings);
  }
  showNotification() async {
    var android = const AndroidNotificationDetails('channel id', 'channel NAME',
    priority: Priority.high, importance: Importance.max);

    var iOS = IOSNotificationDetails();
    var platform = NotificationDetails(android: android, iOS: iOS);
    await flutterLocalNotificationsPlugin.show(0, '通知', '通知:$_message', platform, payload: '通知界面');
  }

  Future onSelectNotification(String payload) async {
    await Navigator.push(context, MaterialPageRoute(builder: (context) => ScrollNotificationPage(title: palyload)),);
  }

运行效果:

你可能感兴趣的:(flutter学习笔记,flutter,学习,android)