gpt4 book ai didi

list - Flutter 将搜索数据传递给 SearchDelegate

转载 作者:行者123 更新时间:2023-12-04 01:04:17 26 4
gpt4 key购买 nike

我创建了带有标签的产品,因此当用户按下标签按钮时,它将打开 SearchDelegate 并搜索该标签,

那么如何将标签名称传递给搜索委托(delegate)查询

我的代码:

class DataSearch extends SearchDelegate<Product> {
final suggestions = new ProductsRepository().fetchAllProducts();
final lastOnes = ['suggest'];

@override
List<Widget> buildActions(BuildContext context) {
return [
IconButton(icon: Icon(Icons.clear), onPressed: () {
query = '';
showSuggestions(context);
}),
];
}

@override
Widget buildLeading(BuildContext context) {
return IconButton(
icon: AnimatedIcon(
icon: AnimatedIcons.menu_arrow,
progress: transitionAnimation,
),
onPressed: () {
close(context, null);
});
}

@override
Widget buildResults(BuildContext context) {
final suggestionList = query.isEmpty
? []
: suggestions.where((p) => p.title.contains(query)).toList();
double _gridSize =
MediaQuery.of(context).size.height * 0.58; //88% of screen
double childAspectRatio = MediaQuery.of(context).size.width /
(MediaQuery.of(context).size.height / 1.0);

return Column(children: <Widget>[
new Container(
height: _gridSize + 100,
decoration: BoxDecoration(
color: const Color(0xFFeeeeee),),
padding: EdgeInsets.only(left: 10, right: 10),
child: new Column(children: <Widget>[
new Container(
margin: EdgeInsets.only(top: 40),
child: new Column(children: <Widget>[
new Container(
height: _gridSize - 60,
margin: EdgeInsets.only(top: 0),
child: new PhysicalModel(
color: Colors.transparent,
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(_gridSize / 10 - 10),
bottomRight:
Radius.circular(_gridSize / 10 - 10)),
clipBehavior: Clip.antiAlias,
child: GridView.builder(
itemCount: suggestionList.length,
gridDelegate:
new SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: childAspectRatio),
itemBuilder: (BuildContext context, int index) {
return new Padding(
padding: EdgeInsets.only(
top: index % 2 == 0 ? 20 : 0,
right: index % 2 == 0 ? 5 : 0,
left: index % 2 == 1 ? 5 : 0,
bottom: index % 2 == 1 ? 20 : 0),
child:
ProductWidget(
product: suggestionList[index]));
})

)),

]))
])),
// new MinimalCart(_gridSize)
]);
}

@override
Widget buildSuggestions(BuildContext context) {
final suggestionList = query.isEmpty
? []
: suggestions.where((p) => p.title.contains(query)).toList();
return ListView.builder(
itemBuilder: (context, index) =>
ListTile(
leading: Icon(Icons.add),
onTap: () => showResults(context),
title: RichText(
text: TextSpan(
text: suggestionList[index].title.substring(
0, query.length),
style: TextStyle(
color: Colors.black, fontWeight: FontWeight.bold),
children: [
TextSpan(
text: suggestionList[index].title.substring(
query.length),
style: TextStyle(
color: Colors.grey))
])),
),
itemCount: suggestionList.length,
);
}
}

我的标签按钮:

 child: FlatButton(
child: Text(widget.product.tags[index]),
onPressed: () {
showSearch(
context: context,
delegate: DataSearch(),
);

})

我尝试传递 query 变量,但没有成功,因为 query 位于 Widgets 内部,还有其他方法吗?

最佳答案

您可以使用参数 q 作为 String 和 final 的 DataSearch 构造函数。然后将 this.q 设置为您想要的查询。并在按钮中,使用产品标签作为参数初始化 DataSearch

试试这个代码,告诉我它是否有效。

class DataSearch extends SearchDelegate<Product> {
final suggestions = new ProductsRepository().fetchAllProducts();
final lastOnes = ['suggest'];

final String q;

DataSearch(this.q) {
query = this.q;
}

@override
List<Widget> buildActions(BuildContext context) {
return [
IconButton(icon: Icon(Icons.clear), onPressed: () {
query = '';
showSuggestions(context);
}),
];
}

@override
Widget buildLeading(BuildContext context) {
return IconButton(
icon: AnimatedIcon(
icon: AnimatedIcons.menu_arrow,
progress: transitionAnimation,
),
onPressed: () {
close(context, null);
});
}

@override
Widget buildResults(BuildContext context) {
final suggestionList = query.isEmpty
? []
: suggestions.where((p) => p.title.contains(query)).toList();
double _gridSize =
MediaQuery.of(context).size.height * 0.58; //88% of screen
double childAspectRatio = MediaQuery.of(context).size.width /
(MediaQuery.of(context).size.height / 1.0);

return Column(children: <Widget>[
new Container(
height: _gridSize + 100,
decoration: BoxDecoration(
color: const Color(0xFFeeeeee),),
padding: EdgeInsets.only(left: 10, right: 10),
child: new Column(children: <Widget>[
new Container(
margin: EdgeInsets.only(top: 40),
child: new Column(children: <Widget>[
new Container(
height: _gridSize - 60,
margin: EdgeInsets.only(top: 0),
child: new PhysicalModel(
color: Colors.transparent,
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(_gridSize / 10 - 10),
bottomRight:
Radius.circular(_gridSize / 10 - 10)),
clipBehavior: Clip.antiAlias,
child: GridView.builder(
itemCount: suggestionList.length,
gridDelegate:
new SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: childAspectRatio),
itemBuilder: (BuildContext context, int index) {
return new Padding(
padding: EdgeInsets.only(
top: index % 2 == 0 ? 20 : 0,
right: index % 2 == 0 ? 5 : 0,
left: index % 2 == 1 ? 5 : 0,
bottom: index % 2 == 1 ? 20 : 0),
child:
ProductWidget(
product: suggestionList[index]));
})

)),

]))
])),
// new MinimalCart(_gridSize)
]);
}

@override
Widget buildSuggestions(BuildContext context) {
final suggestionList = query.isEmpty
? []
: suggestions.where((p) => p.title.contains(query)).toList();
return ListView.builder(
itemBuilder: (context, index) =>
ListTile(
leading: Icon(Icons.add),
onTap: () => showResults(context),
title: RichText(
text: TextSpan(
text: suggestionList[index].title.substring(
0, query.length),
style: TextStyle(
color: Colors.black, fontWeight: FontWeight.bold),
children: [
TextSpan(
text: suggestionList[index].title.substring(
query.length),
style: TextStyle(
color: Colors.grey))
])),
),
itemCount: suggestionList.length,
);
}
}

还有你的按钮

child: FlatButton(
child: Text(widget.product.tags[index]),
onPressed: () {
showSearch(
context: context,
delegate: DataSearch(widget.product.tags[index]),
);

})

关于list - Flutter 将搜索数据传递给 SearchDelegate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57351054/

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