gpt4 book ai didi

listview - 如何在 Flutter 中过滤 AlertDialog 中 ListView 中的数据?

转载 作者:IT王子 更新时间:2023-10-29 06:57:24 25 4
gpt4 key购买 nike

我在带有搜索(过滤器)的警报对话框中有 ListView。在正常屏幕中,列表搜索有效,但当我在 AlertDialog 中添加时,它不起作用。下面是我的代码,

return new AlertDialog(
contentPadding: EdgeInsets.all(0.0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(5.0))),
content: Container(
width: screenSize.width * 0.9,
height: screenSize.height * 0.5,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
new Container(
width: screenSize.width,
height: 40,
padding: EdgeInsets.only(left: 10, right: 10),
color: const Color(colorPrimary),
child: Align(
child: Text("Scanned Serial Number",
style: TextStyle(
fontSize: 14.0,
fontWeight: FontWeight.w500,
fontFamily: 'helvetica',
color: Colors.white)),
alignment: Alignment.centerLeft,
),
),
Container(
color: Color(colorAccent),
padding: EdgeInsets.all(10),
child: new Container(
height: 40,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(7)),
color: Colors.white),
child: new Row(
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Flexible(
child: Padding(
padding: EdgeInsets.only(left: 10),
child: new TextField(
controller:_search_controller,
decoration: new InputDecoration(
border: InputBorder.none,
hintStyle: TextStyle(color: Colors.grey),
hintText: "Search Label",
counterText: "",
),
onChanged: (value) {
filterSearchResults(value);
},
maxLines: 1,
maxLength: 50,
),
),
flex: 1,
),
Flexible(
child: IconButton(
onPressed: () {},
icon: Icon(
Icons.search,
color: const Color(colorPrimary),
)),
flex: 0,
)
],
),
),
),
Expanded(
child: widget.listStickerDisplay.length > 0
? new Container(
child: ListView.builder(
itemCount: widget.listStickerDisplay.length,
shrinkWrap: true,
itemBuilder: (BuildContext context, int index) {
return new Column(
children: <Widget>[
Container(
height: 35,
width: screenSize.width,
child: Align(
alignment: Alignment.center,
child: Text(
"#" +
listStickerDisplay[index]
.intRowNo
.toString() +
" " +
listStickerDisplay[index]
.varSticker,
style: TextStyle(
fontSize: 14.0,
fontWeight: FontWeight.w400,
fontFamily: 'helvetica',
)),
),
),
Container(
width: screenSize.width,
color: Colors.black12,
height: 1,
),
],
);
},
),
)
: Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image.asset(
"assets/nodata_icon.png",
height: 100,
width: 100,
),
Padding(
padding: EdgeInsets.only(top: 10),
child: Text(
"No Data Found",
style: prifixTxtStyle,
),
),
],
),
),
flex: 1,
),
Container(
width: screenSize.width,
height: 45,
child: Row(
children: <Widget>[
Expanded(
child: ButtonDialogWidgets(
buttonName: 'Close',
buttonColor: const Color(colorPrimary),
textColor: Colors.white,
onTap: () {
Navigator.of(context).pop();
},
),
flex: 1,
),
],
),
),
],
),
)
);

下面是过滤函数:

void filterSearchResults(String query) {
List<SalesSummarySKUStickerModel> dummySearchList =
List<SalesSummarySKUStickerModel>();
dummySearchList.addAll(widget.listSticker);
if (query.isNotEmpty) {
query = query.toLowerCase();
List<SalesSummarySKUStickerModel> dummyListData =
List<SalesSummarySKUStickerModel>();
dummySearchList.forEach((item) {
if (item.varSticker.toLowerCase().contains(query)) {
dummyListData.add(item);
}
});
if (mounted)
setState(() {
listStickerDisplay.clear();
listStickerDisplay.addAll(dummyListData);
});
} else {
if (mounted)
setState(() {
listStickerDisplay.clear();
listStickerDisplay.addAll(widget.listSticker);
});
}
}

最佳答案

为您的对话框创建另一个有状态的小部件。阅读this进一步了解。

关于listview - 如何在 Flutter 中过滤 AlertDialog 中 ListView 中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55687897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com