如何从本地json连接数据以实现列表中自动完成文本的搜索? [英] How to hook up data from local json to achieve search with autocomplete text in list?
问题描述
我正在尝试实现输入搜索功能,其中键入搜索文本将显示建议的文本,并且用户可以从列表中选择相关文本,然后单击搜索按钮以进入相应的屏幕.建议的文本在local json
中,我将其添加到assets/
文件夹下和pubspec.yaml
中.
搜索文本字段为:
I am trying to implement input search feature wherein typing a search text will display suggested text and user can select relevant text from list and hit search button to proceed to corresponding screen. The suggested text is in local json
and I added it under under assets/
folder and in pubspec.yaml
.
The search textfield is:
上面的代码是:
new TextField(
style: new TextStyle(
color: Colors.white,
fontSize: 16.0),
cursorColor: Colors.green,
decoration: new InputDecoration(
suffixIcon: Container(
width: 85.0,
height: 60.0,
color: Colors.green,
child: new IconButton(
icon: new Image.asset('assets/search_icon_ivory.png',color: Colors.white, height: 18.0,),
onPressed: () {
},
),
),
fillColor: Colors.black,
contentPadding: new EdgeInsets.fromLTRB(10.0, 30.0, 10.0, 20.0),
filled: true,
hintText: 'What Do You Need Help With?',
hintStyle: new TextStyle(
color: Colors.white
)
)
)
本地json数据示例为:
The local json data sample is:
我想使用已安装并导入的autocomplete_textfield
程序包并引用
I want to achieve above using autocomplete_textfield
package which I've installed and imported and referring this example.
我想知道如何开始并集成来自本地json的解析,使用autocomplete_textfield
程序包挂钩该数据以实现我的目标.我还没有在flutter中解析json,所以正在寻找如何做到这一点的指导.
我正在寻找的最终结果是这样的:
I would like to know how to get started with this and integrate parsing from local json, hook that data using autocomplete_textfield
package to achieve my goal. I haven't done parsing json in flutter yet so looking for guidance on how to do that.
The end result I am looking for is like this:
*****************编辑**************
***************** Edit **************
我现在可以使用演示应用程序解析local json
中的数据并将其显示在listView
中.为此,我创建了一个新的模型类`services.dart',如下所示:
I am now able to parse data from local json
and display it in a listView
using a demo app. For it, I created a new model class `services.dart' as below:
class Categories {
String serviceCategory;
String servCategoryDesc;
int id;
String autocompleteterm;
String category;
String desc;
Categories({
this.serviceCategory,
this.servCategoryDesc,
this.id,
this.autocompleteterm,
this.category,
this.desc
});
factory Categories.fromJson(Map<String, dynamic> parsedJson) {
return Categories(
serviceCategory:parsedJson['serviceCategory'] as String,
servCategoryDesc: parsedJson['serviceCategoryDesc'] as String,
id: parsedJson['serviceCategoryId'],
autocompleteterm: parsedJson['autocompleteTerm'] as String,
category: parsedJson['category'] as String,
desc: parsedJson['description'] as String
);
}
}
使用builder
函数来检索和显示listview
中的值,如下所示:
Used builder
function to retrieve and display value in listview
as below:
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Load local JSON file"),
),
body: new Container(
child: new Center(
// Use future builder and DefaultAssetBundle to load the local JSON file
child: new FutureBuilder(
future: DefaultAssetBundle
.of(context)
.loadString('assets/services.json'),
builder: (context, snapshot) {
// Decode the JSON
Map data = json.decode(snapshot.data
.toString());
print(data);
final List<Categories> items = (data['data'] as List).map((i) => new Categories.fromJson(i)).toList();
for (final item in items) {
print(item.category);
return new ListView.builder(
itemBuilder: (BuildContext context, int index) {
return new Card(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
new Text('Service Category: ' + items[index].category),
new Text('Category' + items[index].categoryDesc),
new Text('Auto complete term' + items[index].autocompleteterm),
new Text('Desc' + items[index].desc)
],
),
);
},
);
}
}
)
)
)
);
}
}
在我的目标应用程序中,添加了使用autocomplete_textfield
程序包的必需代码,该程序包显示了到目前为止的静态建议列表:
In my target app, added required code that uses autocomplete_textfield
package that shows a static list of suggestions as of now :
@override
Widget build(BuildContext context) {
textField = new AutoCompleteTextField<String>
(style: new TextStyle(
color: Colors.white,
fontSize: 16.0),
decoration: new InputDecoration(
suffixIcon: Container(
width: 85.0,
height: 60.0,
color: Colors.green,
child: new IconButton(
icon: new Image.asset('assets/search_icon_ivory.png',color: Colors.white,
height: 18.0,),
onPressed: (){},
),
),
fillColor: Colors.black,
contentPadding: new EdgeInsets.fromLTRB(10.0, 30.0, 10.0, 20.0),
filled: true,
hintText: 'What Do You Need Help With ?',
hintStyle: new TextStyle(
color: Colors.white
)
),
submitOnSuggestionTap: true,
clearOnSubmit: true,
textChanged: (item){
currentText = item;
},
textSubmitted: (item) {
setState(() {
currentText = item;
});
},
key: key,
suggestions: suggestions,
itemBuilder: (context, item) {
return new Padding(
padding: EdgeInsets.all(8.0), child: new Text(item));
},
itemSorter: (a, b) {
return a.compareTo(b);
},
itemFilter: (item, query) {
return item.toLowerCase().startsWith(query.toLowerCase());
});
Column body = new Column(children: [
new GestureDetector(
child: new ListTile(
title: textField,
onTap: () {
setState(() {
if (currentText != "") {
added.add(currentText);
textField.clear();
currentText = "";
}
});
}
)
)
]
);
body.children.addAll(added.map((item) {
return new ListTile(
title: new Text(item)
);
}));
return Scaffold(
resizeToAvoidBottomPadding: false,
backgroundColor: Color(0xFF13212C),
appBar: AppBar(
title: Text(''),
),
drawer: appDrawer(),
body: new Center(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
new Column(
children: <Widget>[
textField
上面的代码在UI中显示如下:
Above code shows in UI as below:
我现在想知道如何在目标应用程序中钩住builder
函数以检索json数据,以便该下拉列表将显示json的建议,而不是静态的字符串列表(如我的原始问题的屏幕截图中所述) .
I now would like to know how to hook the builder
function retrieving json data in my target app, so that instead of static list of strings, the dropdown would show suggestions from json (as posted in my original question's screenshot).
推荐答案
如果您发现手动操作过多,实际上是扑朔迷离包可以做到这一点.打包站点上也有两个示例.
If you found doing this manually it too much, this is actually a flutter package that does this. There are two examples on the package site too.
请注意,这是当前程序包中的错误(我提出了PR对其进行修复,但程序包所有者最近一直在忙于审查任何PR).根据您的使用方式,该错误可能不会影响您.
Do be warned, this is currently a bug in the package (I have raised a PR to fix it but the package owner has been too busy to review any PR recently). Depending on how you use it, the bug may not affect you.
这篇关于如何从本地json连接数据以实现列表中自动完成文本的搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!