1

I want to use a Dismissible Widget in a PopupMenuButton to delete items in the popup list. My example code (see the example main() code below) works for deleting an item (String) in the PopupMenuButton, but the list is not refreshed and resized letting a blank space in the list while the popup is still displayed.
The list is only displayed resized after the popup is closed and then reopened.
I remove the item from the list in a setState in the onDismissed callback so the widget tree should be rebuild.

How can I update and resize the displayed list in the popup without having to close and reopen it?

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter PopupMenuButton sample',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String dropdownValue = 'Orange';

  final _items = [
    'Orange',
    'Banana',
    'Grapes',
    'Apple',
    'watermelon',
    'Pineapple'
  ];

  @override
  Widget build(BuildContext context) {
    print("build Scaffold");
    return Scaffold(
      appBar: AppBar(
        title: Text("DropDownList Example"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text("$dropdownValue"),
            const Spacer(),
            PopupMenuButton<String>(
              icon: Icon(Icons.bookmarks_outlined),
              onSelected: (String value) {
                setState(() {
                  dropdownValue = value;
                });
                print(value);
              },
              itemBuilder: (BuildContext context) {
                return _items.map<PopupMenuItem<String>>((String _value) {
                  return PopupMenuItem(
                    value: _value,
                    child: Dismissible(
                      key: UniqueKey(),
                      direction: DismissDirection.endToStart,
                      onDismissed: (i) {
                        print("onDismissed $_value");
                        setState(() {
                          _items.remove(_value);
                        });
                      },
                      child: ListTile(
                        title: Text(_value),
                        trailing: const Icon(Icons.arrow_back),
                      ),
                      background: Container(
                        color: Colors.red,
                        margin: const EdgeInsets.symmetric(horizontal: 15),
                        alignment: Alignment.centerRight,
                        child: const Icon(
                          Icons.delete,
                          color: Colors.white,
                        ),
                      ),
                    ),
                  );
                }).toList();
              },
            ),
            const Spacer(
              flex: 10,
            ),
          ],
        ),
      ),
    );
  }
}

Jean G
  • 147
  • 1
  • 10
  • 1
    `PopupMenuButton` items create only while opening the menu items. I've tried with `StatefulBuilder` and `ValueNotifier` no result, also it is separated from context, you can try with different widget. – Md. Yeasin Sheikh Jan 27 '22 at 19:28
  • @Yeasin Sheikh: yes, I am actually doing the equivalent with a ListView.builder with Dismissible's in an AlertDialog and it seems to work. I will post the code when finished. – Jean G Jan 27 '22 at 19:47

0 Answers0