List storedWishes = [
{
'title': 'Phone',
'description': 'a phone would be nice',
'price': 200.00,
'icon': Icon(Icons.phone)
},
{
'title': 'monitor',
'description': 'need it for a 2 screen setup',
'price': 350.00,
'icon': Icon(Icons.tv)
},
{
'title': 'headphones',
'description': 'need some high quality sound',
'price': 100.00,
'icon': Icon(Icons.headset)
},
];
final wishes = useState(storedWishes);
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('iWant'),
),
body: Column(
//map the wishes to a list of wish widgets
children: wishes.value
.map((wish) => Wish(
title: wish['title'] as String,
description: wish['description'] as String,
price: wish['price'] as double,
icon: wish['icon'],
onDelete: () {
wishes.value.remove(wish)
wishes.notifyListeners();
},
))
.toList(),
)));
wish.dart代码:
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
class Wish extends HookWidget {
const Wish(
{Key? key,
this.title = 'default title',
this.description = 'default description',
this.price = 0.00,
this.icon = const Icon(Icons.star),
this.onDelete})
: super(key: key);
final String title;
final String description;
final double price;
final dynamic icon;
final VoidCallback? onDelete;
@override
Widget build(BuildContext context) {
var wishes = useState({
'title': title,
'description': description,
'price': price,
'image': icon
});
return Card(
child: Row(children: [
wishes.value['image'] as Icon,
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(wishes.value['title'] as String),
Text(wishes.value['description'] as String),
],
),
),
Text((wishes.value['price'].toString()) + '\$'),
Column(
children: [
Icon(Icons.edit),
IconButton(
onPressed: () {
onDelete!();
},
icon: Icon(Icons.delete),
)
],
),
]));
}
}
// child: Card(
// child: Row(children: [
// ,
// Column(
// crossAxisAlignment: CrossAxisAlignment.start,
// children: [
// Text('Product name'),
// Text('Product description'),
// ],
// ),
// Column(
// crossAxisAlignment: CrossAxisAlignment.end,
// children: [
// Text('Price:'),
// Text('21\$'),
// ],
// ),
// ])),
// );
单击delete按钮总是在单击任何其他元素的delete时删除最后一个元素。
edit1:单击delete图标后,我已经删除了数组,数组被正确编辑,但是错误的小部件从屏幕上删除
这是一段视频:https://drive.google.com/file/d/1bEjHh4utynZk3QzSWw5gWv__YcpqKkb2/view?usp=sharing
edit2:我已经将钩子从愿望小部件中移除了,现在似乎正常工作了吗?无论如何,谢谢你的帮助:)
发布于 2022-05-01 23:19:12
我不知道为什么不适合你,但对我来说很好。因为我不知道你的愿望是什么,所以我做的是这样的:
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
void main() {
runApp(MyApp());
}
class MyApp extends HookWidget {
@override
Widget build(BuildContext context) {
List storedWishes = [
{
'title': 'Phone',
'description': 'a phone would be nice',
'price': 200.00,
'icon': Icon(Icons.phone)
},
{
'title': 'monitor',
'description': 'need it for a 2 screen setup',
'price': 350.00,
'icon': Icon(Icons.tv)
},
{
'title': 'headphones',
'description': 'need some high quality sound',
'price': 100.00,
'icon': Icon(Icons.headset)
},
];
final wishes = useState(storedWishes);
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('iWant'),
),
body: Column(
//map the wishes to a list of wish widgets
children: wishes.value
.map((wish) => TextButton(
child: Text(wish['title'] as String),
onPressed: () {
wishes.value.remove(wish);
wishes.notifyListeners();
},
))
.toList(),
)));
}
}
它像这样正确地工作着。
编辑:我可以用你的愿望代码复制你的问题。实际上,我对HookWidget并不熟悉,但是将代码更改为这一点似乎可以解决这个问题。我不知道useState
到底是用来做什么的,但是不使用它就可以修复它。你甚至可以让愿望只扩展StatelessWidget而不是HookWidget
class Wish extends StatelessWidget {
const Wish(
{Key? key,
this.title = 'default title',
this.description = 'default description',
this.price = 0.00,
this.icon = const Icon(Icons.star),
this.onDelete})
: super(key: key);
final String title;
final String description;
final double price;
final dynamic icon;
final VoidCallback? onDelete;
@override
Widget build(BuildContext context) {
return Card(
child: Row(children: [
icon,
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(title),
Text(description),
],
),
),
Text((price.toString()) + '\$'),
Column(
children: [
Icon(Icons.edit),
IconButton(
onPressed: onDelete,
icon: Icon(Icons.delete),
)
],
),
]));
}
}
发布于 2022-05-01 22:30:15
这是因为它实际上没有根据其内容来识别元素。当您调用列表上的remove
函数时,它会看到元素是一个Map对象,您的输入也是一个Map对象。因此,它只需删除最后一个元素。
您要做的是为每个愿望指定一个唯一的标识符。差不多是这样的:
{
'id': '0'
'title': 'headphones',
'description': 'need some high quality sound',
'price': 100.00,
'icon': Icon(Icons.headset)
},
或者,如果每个项目的title
都是唯一的,那么您也可以引用它。
然后简单地通过编辑删除函数来进行深入的比较:
onDelete: () {
wishes.value.removeWhere((e) => e["id"] == wish["id"]);
//OR
//wishes.value.removeWhere((e) => e["title"] == wish["title"]);
wishes.notifyListeners();
},
https://stackoverflow.com/questions/72082936
复制