【问题标题】:how to delete item list using pop up menu - flutter如何使用弹出菜单删除项目列表 - 颤振
【发布时间】:2019-07-26 14:45:16
【问题描述】:

当我单击弹出菜单按钮时如何删除项目列表?但是,我的列表和弹出菜单位于两个单独的文件中。我需要根据按下的列表项知道我删除的是哪一个。

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')))
          ],
    );
  }
}

所以在list_tile.dart中导入了这个弹出菜单,每当我点击弹出菜单按钮“删除”时,我需要删除按下弹出菜单的选定列表项

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;
    }
  }
}

因此,此列表中的每个项目都有一个弹出菜单,该菜单中带有删除选项。当按下删除选项时,它必须删除触发它的项目。

示例:当按下 user2 的弹出菜单按钮删除时,它应该删除 user2。

【问题讨论】:

    标签: list flutter popupmenu


    【解决方案1】:

    为您的PopUpMenu 类添加回调函数:

    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
        }
      }
    

    然后在你原来的类中创建它时:

             ...
                    trailing: PopUpMenu(
                      onDelete: () {
                        levelsData.removeWhere((element) => element == element);
                      }
                    )));
              }
    

    Flutter 中的一般经验法则是将回调向下传递给子级,而不是尝试访问父级中的数据。

    您可能还需要使您的StuffInTiles 小部件有状态并将setState(() {}); 添加到您的onDelete,因为简单地删除该值实际上不会使用新列表更新您的视图。

    【讨论】:

      猜你喜欢
      • 2019-10-28
      • 1970-01-01
      • 2019-05-17
      • 2021-11-19
      • 1970-01-01
      • 2020-02-27
      • 2021-08-14
      • 2019-12-20
      • 2021-03-28
      相关资源
      最近更新 更多