How do i delete an item list when i clicked on a pop up menu button? however, my list and pop up menu is in two separate files. i need to know which one im deleting according to which list item is pressed on.
pop_up_menu.dart:
import 'package:flutter/material.dart';
class PopUpMenu extends StatelessWidget {
void showMenuSelection(String value) {
print("pressed");
}
@override
Widget build(BuildContext context) {
return PopupMenuButton<String>(
padding: EdgeInsets.zero,
icon: Icon(Icons.more_vert),
onSelected: showMenuSelection,
itemBuilder: (BuildContext context) => <PopupMenuEntry<String>>[
const PopupMenuItem<String>(
value: 'Create another',
child: ListTile(
leading: Icon(Icons.add), title: Text('Create another'))),
const PopupMenuItem<String>(
value: 'Delete',
child: ListTile(
leading: Icon(Icons.delete), title: Text('Delete')))
],
);
}
}
So imported this pop up menu in list_tile.dart, whenever i clicked on the pop up menu button, 'Delete', i need to remove the selected list item that has pressed the pop up menu
List_tile.dart:
import 'package:flutter/material.dart';
import '../pop_up_menu/pop_up_menu.dart';
var levelsData = [];
class ListTile extends StatefulWidget {
@override
ListTileState createState() => new ListTileState();
}
class ListTileState extends State<ListTile> {
@override
Widget build(BuildContext context) {
return ListView.builder(
itemBuilder: (BuildContext context, int index) => Card(
child: SingleChildScrollView(
child: StuffInTiles(
levelsData[index]['user_id'],
levelsData[index]['price'],
),
),
),
itemCount: levelsData.length,
);
}
}
class StuffInTiles extends StatelessWidget {
final String userId;
final double price;
StuffInTiles(this.userId, this.price);
@override
Widget build(BuildContext context) {
List<dynamic> _getChildren() {
List<Widget> children = [];
levelsData.forEach(
(element) {
if (price.toString() == element['price'].toString()) {
children.add(ListTile(
title: Text("@" + element['price'].toString(),
style: TextStyle(color: Colors.lightGreen)),
subtitle: Text(
"Created on 01 Jun 2018 at 06:24AM \n when price was " +
element['price'].toString()),
trailing: PopUpMenu()));
}
},
);
return children;
}
}
}
So each item in this list has a pop up menu with delete option in that menu. When the delete option is pressed, it has to delete the item that triggered it.
Example: when the pop up menu button delete for user2 is pressed, it should delete user2.
remove() function removes the first occurrence of the specified item in the list. This function returns true if the specified value is removed from the list.
Add a callback function to your PopUpMenu
class:
class PopUpMenu extends StatelessWidget {
VoidCallback onDelete;
PopUpMenu({this.onDelete});
void showMenuSelection(String value) {
switch (value) {
case 'Delete':
onDelete();
break;
// Other cases for other menu options
}
}
Then when creating it in your original class:
...
trailing: PopUpMenu(
onDelete: () {
levelsData.removeWhere((element) => element == element);
}
)));
}
General rule of thumb in Flutter is to pass a callback down to children rather than try to access data in a parent.
You may also need to make your StuffInTiles
Widget Stateful and add setState(() {});
to your onDelete
, as simply removing the value won't actually update your view with the new list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With