如何从另一个有状态小部件的列表中删除其自己的有状态小部件中的 ListTile?
How to I delete a ListTile in its own stateful widget from a list in another stateful widget?
我已经构建了一个 ListView.builder(),让我们称之为 StatefulWidget1,如下所示,只要 List listTile 的索引为:
下方的 StatefulWidget1:
ListView.builder(
itemCount: listTile.length,
itemBuilder: (context, index) {
// CUSTOM WIDGET
return CustomTile(
custom: listTile.reversed.toList()[index]);
},
)
CustomTile 在 StatefulWidget2 中包含一个 ListTile(),我希望能够在 ListTile() 中有一个 onPressed 事件,该事件调用 StatefulWidget1 中的一个函数,该函数将从屏幕上删除 CustomTile 并将其自身从列表中删除listTile 索引。
如何在 StatefulWidget2 中调用 StatefulWidget1 的函数?
以及如何确保删除正确索引的 CustomTile?
编辑:
或者是否有另一种方法可以删除带有前导的 CustomTile:onPressed() {}
下面的StatefulWidget2:
Card(
margin: EdgeInsets.fromLTRB(20.0, 6.0, 20.0, 0.0),
child: ListTile(
leading: IconButton(
onPressed: () {},
icon: Icon(
Icons.close,
color: Colors.red,
),
),
title: Text(
'${widget.produce.name} - $${widget.produce.tilePrice}',
style: TextStyle(fontWeight: FontWeight.bold),
),
subtitle: Padding(
padding: const EdgeInsets.fromLTRB(0.0, 8.0, 0.0, 3.0),
child: Text(
'Weight: ${widget.produce.weight} lbs,\nPrice per lb: $${widget.produce.pricePerLb}'),
),
trailing: Column(
children: [
TextButton(
onPressed: () {},
child: Text(
'edit',
style: TextStyle(color: Colors.green[600]),
),
),
],
),
),
),
试试这个解决方案
ListView.builder(
itemCount: listTile.length,
itemBuilder: (context, index) {
// CUSTOM WIDGET
return InkWell(
onTap: (){
setState(() {
listTile.remove(index);
});
},
child: CustomTile(
custom: listTile.reversed.toList()[index]),
);
},
),
ListView.builder(
itemCount: listTile.length,
itemBuilder: (context, index) {
// CUSTOM WIDGET
return InkWell(
child:Inkwell(
onTap: () => setState(() {
listTile.reversed.toList()
[index])).remove(index);
});
CustomTile(
custom:
listTile.reversed.toList()[index])),
);
},
),
首先在你的 StatefulWidget1
中创建一个函数,就像这样
void _removetile(int index){
setState(() {
listTile.reversed.toList().removeAt(index);
});
}
将此函数作为参数发送到您的 StatefulWidget2
中,就像
CustomTile(custom: listTile.reversed.toList()[index], removeFunct :
_removetile, index: index)
像这样在您的 customTile 中声明
class CustomTile extends StatefulWidget{
final List<YorList> custom;
final void Function removeFunc;
final int index;
CustomTile(this.custom,this.removeFunc,this.index);
}
在您的前导按钮中使用此功能就像
leading: IconButton(
onPressed: () {
widget.removeFunc(widget.index);
},
我已经构建了一个 ListView.builder(),让我们称之为 StatefulWidget1,如下所示,只要 List listTile 的索引为:
下方的 StatefulWidget1:
ListView.builder(
itemCount: listTile.length,
itemBuilder: (context, index) {
// CUSTOM WIDGET
return CustomTile(
custom: listTile.reversed.toList()[index]);
},
)
CustomTile 在 StatefulWidget2 中包含一个 ListTile(),我希望能够在 ListTile() 中有一个 onPressed 事件,该事件调用 StatefulWidget1 中的一个函数,该函数将从屏幕上删除 CustomTile 并将其自身从列表中删除listTile 索引。
如何在 StatefulWidget2 中调用 StatefulWidget1 的函数? 以及如何确保删除正确索引的 CustomTile?
编辑: 或者是否有另一种方法可以删除带有前导的 CustomTile:onPressed() {}
下面的StatefulWidget2:
Card(
margin: EdgeInsets.fromLTRB(20.0, 6.0, 20.0, 0.0),
child: ListTile(
leading: IconButton(
onPressed: () {},
icon: Icon(
Icons.close,
color: Colors.red,
),
),
title: Text(
'${widget.produce.name} - $${widget.produce.tilePrice}',
style: TextStyle(fontWeight: FontWeight.bold),
),
subtitle: Padding(
padding: const EdgeInsets.fromLTRB(0.0, 8.0, 0.0, 3.0),
child: Text(
'Weight: ${widget.produce.weight} lbs,\nPrice per lb: $${widget.produce.pricePerLb}'),
),
trailing: Column(
children: [
TextButton(
onPressed: () {},
child: Text(
'edit',
style: TextStyle(color: Colors.green[600]),
),
),
],
),
),
),
试试这个解决方案
ListView.builder(
itemCount: listTile.length,
itemBuilder: (context, index) {
// CUSTOM WIDGET
return InkWell(
onTap: (){
setState(() {
listTile.remove(index);
});
},
child: CustomTile(
custom: listTile.reversed.toList()[index]),
);
},
),
ListView.builder(
itemCount: listTile.length,
itemBuilder: (context, index) {
// CUSTOM WIDGET
return InkWell(
child:Inkwell(
onTap: () => setState(() {
listTile.reversed.toList()
[index])).remove(index);
});
CustomTile(
custom:
listTile.reversed.toList()[index])),
);
},
),
首先在你的 StatefulWidget1
中创建一个函数,就像这样
void _removetile(int index){
setState(() {
listTile.reversed.toList().removeAt(index);
});
}
将此函数作为参数发送到您的 StatefulWidget2
中,就像
CustomTile(custom: listTile.reversed.toList()[index], removeFunct :
_removetile, index: index)
像这样在您的 customTile 中声明
class CustomTile extends StatefulWidget{
final List<YorList> custom;
final void Function removeFunc;
final int index;
CustomTile(this.custom,this.removeFunc,this.index);
}
在您的前导按钮中使用此功能就像
leading: IconButton(
onPressed: () {
widget.removeFunc(widget.index);
},