Search code examples
flutterdartpopupmenu

Flutter - How to add close icon to popup menu?


             PopupMenuButton<String>(

                    child: SizedBox(
                      child: ....
                    ),

                    onSelected: (value) {},
                    itemBuilder: (BuildContext context) {

               ///Add a close icon here (to the right) before rendering the list
                      return list!.map((notification) {
                        return custom_popup_menu.PopupMenuItem<String>(
                          value: someValue,
                          child: SizedBox(
                            child: Column(
                              children: [
                                const Divider(
                                  thickness: 1,
                                  color: CustomColor.blue_color,
                                ),
                                Text(
                                  'some text',
                                  style: TextStyle(
                                    fontWeight: (notification.readOn == null)
                                        ? FontWeight.w900
                                        : FontWeight.normal,
                                  ),
                                ),
                              ],
                            ),
                          ),
                        );
                      }).toList();
                    },
                  ),

Solution

  • You can include use spreed operator on map to list and create another inner list including close PopupMenuItem.

    PopupMenuButton<String?>(
        onSelected: (value) => setState(() => value = value),
        itemBuilder: (context) => [
              PopupMenuItem<String?>(
                child: TextButton(
                  onPressed: Navigator.of(context).pop,
                  child: Text("close"),
                ),
              ),
              ...["A", "B"]
                  .map(
                    (e) => PopupMenuItem<String?>(
                      value: e,
                      child: Text(e),
                    ),
                  )
                  .toList(),
            ])