dart 如何在flutter中编辑listview builder中的文本?

3lxsmp7m  于 2023-03-10  发布在  Flutter
关注(0)|答案(1)|浏览(153)

我有一个存储在列表中的项目列表,我通过listview.builder显示它,现在我需要允许用户编辑该文本,我该如何做呢?我尝试使用TextField,它会更改文本,但我还需要更新列表中的文本。

List shoppingList = [];
ListView.builder(
        shrinkWrap: true,
        itemCount: shoppingList.length,
        itemBuilder: (context, index) {
          return ShoppingListItemTile(
              addText: () {
                shoppingList[index] = '';
              },
              text: shoppingList[index],
              removeList: () {
                shoppingList.removeAt(index);
                setState(() {});
              });
        },
      )

class ShoppingListItemTile extends StatelessWidget {
  final String text;
  final VoidCallback removeList;
  final VoidCallback addText;
  const ShoppingListItemTile(
      {Key? key,
      required this.text,
      required this.removeList,
      required this.addText})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    bool isEditable = false;
    return Padding(
      padding: const EdgeInsets.only(left: 24.0, right: 24, top: 16),
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(text,
                  style: AppTextStyle.latoMedium(
                      fontWeight: FontWeight.w400,
                      fontSize: 17,
                      color: AppColorPallet.zop_location_text)),
              InkWell(
                  onTap: () {
                    removeList();
                  },
                  child: SvgPicture.asset(AppIcons.close))
            ],
          ),
          Padding(
            padding: const EdgeInsets.only(top: 8.0),
            child: Divider(),
          )
        ],
      ),
    );
  }
}
im9ewurl

im9ewurl1#

尝试以下代码

return ShoppingListItemTile(
  addText: () {
    shoppingList[index] = '';
    setState(() {}); //check here
  },
  text: shoppingList[index],
  removeList: () {
    shoppingList.removeAt(index);
    setState(() {});
  },
);

相关问题