【发布时间】:2019-10-28 04:32:01
【问题描述】:
我在更新主页时遇到问题: 我想在我的页面中包含一个带有 itemBuilder 的 StatelessWidget。 Widget 使用 Cards 构建一个 ListView。 LongPress 应该删除一张卡片并更新页面,但它没有更新。这不可能发生,因为它是一个无状态的 Widget。
我已经尝试将 StatlessWidget 变成 StatefullWidget,但是在 itemBuilder 中我收到一个错误,指出返回类型“ProductList(..)”不是小部件
List<String> artikelNamen = [];
List<int> barcodes = [];
class WiederaufnahmePage extends StatefulWidget {
@override
_WiederaufnahmePageState createState() => _WiederaufnahmePageState();
}
class ProductList extends StatelessWidget {
final String artb;
final int brcde;
final int indx;
ProductList(this.artb, this.brcde, this.indx);
@override
Widget build(BuildContext context) {
return new Card(
child: ListTile(
title: Text(artb,
style: TextStyle(fontWeight: FontWeight.w500, fontSize: 20)),
subtitle: Text(brcde.toString()),
leading: Icon(
Icons.fastfood,
color: Colors.blue[500],
),
trailing: Icon(Icons.keyboard_arrow_right),
onTap: () {
Navigator.pushNamed(context, "/BarcodePage");
},
onLongPress: () {
_showSnackBar(artb);
artikelNamen.removeAt(indx);
barcodes.removeAt(indx);
//setState(() {}); //This isn't working bc StatelessWidget
},
));
}
}
class _WiederaufnahmePageState extends State<WiederaufnahmePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
child: Expanded(
child: SizedBox(
height: 200.0,
child: new ListView.builder(
reverse: false,
itemBuilder: (_, int index) => ProductList(
artikelNamen[index], barcodes[index], index),
itemCount: artikelNamen.length,
)
)),
))
}
}
我不知道如何解决这个问题。显示的代码是实际的工作代码,我想在 ListTile 上的每个 LongPress 之后实现整个页面都会更新,因此删除的 Tile 不再可见。
【问题讨论】: