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

问题描述 投票:0回答:1

我使用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],
  ),
];
dart flutter notifications
1个回答
2
投票

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

flutterLocalNotificationsPlugin.showWeeklyAtDayAndTime(your_channelID_goes_here, 
    widget.title,
    '', 
    Day.Monday,
    time,
    platformChannelSpecifics,
    payload: widget.title);
© www.soinside.com 2019 - 2024. All rights reserved.