dart - 如何在Flutter中实现多个本地通知?

标签 dart flutter notifications

我使用 flutter_local_notifications 包来设置通知。我有一个可扩展列表,标题的每个选项都有一个星形图标。当我按下其中一个白色星形图标时,它的颜色会改变并设置通知(“_showNotification”方法)。

如果我按下两颗或更多颗星,我的应用程序只会显示最后一条通知,但我想显示所有这些。我该怎么做?

这是完整的代码:

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

void main() {
  runApp(new MaterialApp(home: new Home()));
}

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("Expandable List"),
      ),
      body: new ListView.builder(
        itemBuilder: (BuildContext context, int index) {
          return new ExpandableListView(ind: index, title: broadcast[index].title);
        },
        itemCount: 2,
      ),
    );
  }
}

class ExpandableListView extends StatefulWidget {
  final String title;
  final int ind;

  const ExpandableListView({this.ind, this.title});

  @override
  _ExpandableListViewState createState() => new _ExpandableListViewState();
}

class _ExpandableListViewState extends State<ExpandableListView> {
  bool expandFlag = false;
  Color _iconColor = Colors.white;

  @override
  Widget build(BuildContext context) {
    return new Container(
      margin: new EdgeInsets.symmetric(vertical: 1.0),
      child: new Column(
        children: <Widget>[
          new Container(
            padding: new EdgeInsets.symmetric(horizontal: 5.0),
            child: new Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                new IconButton(
                    icon: new Container(
                      height: 50.0,
                      width: 50.0,
                      decoration: new BoxDecoration(
                        color: Colors.orange,
                        shape: BoxShape.circle,
                      ),
                      child: new Center(
                        child: new Icon(
                          expandFlag ? Icons.keyboard_arrow_up : Icons.keyboard_arrow_down,
                        ),
                      ),
                    ),
                    onPressed: () {
                      setState(() {
                        expandFlag = !expandFlag;
                      });
                    }),
                new Text(
                  widget.title,
                )
              ],
            ),
          ),
          new ExpandableContainer(              
              expanded: expandFlag,
              expandedHeight: 60.0 * 3,              
              child: new ListView.builder(
                itemBuilder: (BuildContext context, int index) {
                   return StatefulListTile(
                    title: broadcast[widget.ind].contents[index],
                    second: broadcast[widget.ind].time[index],
                  );
                  },
                itemCount: broadcast[widget.ind].contents.length,
              ))
        ],
      ),
    );
  }
}

class ExpandableContainer extends StatelessWidget {
  final bool expanded;
  final double expandedHeight;
  final Widget child;

  ExpandableContainer({
    @required this.child,
    this.expandedHeight,
    this.expanded = true,
  });

  @override
  Widget build(BuildContext context) {
    double screenWidth = MediaQuery.of(context).size.width;
    return new AnimatedContainer(
      duration: new Duration(milliseconds: 100),
      curve: Curves.easeInOut,
      width: screenWidth,
      height: expanded ? expandedHeight : 0.0,
      child: new Container(
        child: child,
        decoration: new BoxDecoration(border: new Border.all(width: 1.0)),
      ),
    );
  }
}

class StatefulListTile extends StatefulWidget {
  const StatefulListTile({this.title, this.second});
  final String title;
  final int second;

  @override
  _StatefulListTileState createState() => _StatefulListTileState();
}

class _StatefulListTileState extends State<StatefulListTile> {
  Color _iconColor = Colors.white;
  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;

  @override
  initState() {
    super.initState();
    var initializationSettingsAndroid =
        new AndroidInitializationSettings('@mipmap/ic_launcher'); 
    var initializationSettingsIOS = new IOSInitializationSettings();
    var initializationSettings = new InitializationSettings(
        initializationSettingsAndroid, initializationSettingsIOS);
    flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
    flutterLocalNotificationsPlugin.initialize(initializationSettings,
        onSelectNotification: onSelectNotification);
  }

  @override
  Widget build(BuildContext context) {
    return new Container(
      decoration: new BoxDecoration(
          border: new Border.all(width: 1.0, color: Colors.grey),
          color: Colors.blue,),
      child: new ListTile(
        title: new Text(widget.title), 
        leading: new IconButton(
          icon: Icon(Icons.star, color: _iconColor),
          onPressed: () {
            setState(() {
              if (_iconColor == Colors.white) {
                _iconColor = Colors.yellow;
                _showNotification(widget.second);
              } else {
                _iconColor = Colors.white;
              }
            });
          },
        ),
      ),
    );
  }
  Future onSelectNotification(String payload) async {
    showDialog(
      context: context,
      builder: (_) {
        return new AlertDialog(
          title: Text("PayLoad"),
          content: Text("Payload : $payload"),
        );
      },
    );
  }

  Future _showNotification(second) async {  
    var time = new Time(10, 18, second);
    var androidPlatformChannelSpecifics =
      new AndroidNotificationDetails('show weekly channel id',
          'show weekly channel name', 'show weekly description');
    var iOSPlatformChannelSpecifics =
      new IOSNotificationDetails();
    var platformChannelSpecifics = new NotificationDetails(
        androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
    await flutterLocalNotificationsPlugin.showWeeklyAtDayAndTime(
        0,
        widget.title,
        '', 
        Day.Monday,
        time,
        platformChannelSpecifics,
        payload: widget.title);
  }
}

class Broadcast {
  final String title;
  List<String> contents;
  List<int> time = [];

  Broadcast(this.title, this.contents, this.time);
}


 List<Broadcast> broadcast = [
  new Broadcast(
    'A',
    ['1', '2', '3'],
    [5, 10, 15],
  ),
  new Broadcast(
    'B',
    ['4', '5'],
    [20, 25],
  ),
];

最佳答案

您需要为不想堆叠的每个通知更改 channelID。

flutterLocalNotificationsPlugin.showWeeklyAtDayAndTime(your_channelID_goes_here, 
    widget.title,
    '', 
    Day.Monday,
    time,
    platformChannelSpecifics,
    payload: widget.title);

关于dart - 如何在Flutter中实现多个本地通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55453628/

相关文章:

Flutter Bloc 使用 Timer 重新获取数据

objective-c - 如何使用flutter在iOS上以编程方式显示Airplay面板

list - Dart将无序列表与对象进行比较

dart - 具有不同项目的 Flutter 中的 ListView.builder()

datetime - 如何实际保持DateTime.now()为最新状态?

flutter 火炉 : updating a boolean field: type 'String' is not a subtype of type 'DocumentReference'

ios - 在远程通知上安排本地通知

email - 电子邮件通知中的 IBM RTC 日志文件

swift - 设置通知观察者的替代方法(使用选择器)

flutter - Dart - 无法捕获异步异常