我正在学习在我的第一个Flutter中添加搜索功能,但是我无法继续进行工作... buildResults和buildSuggestions我知道我需要传递包含json数据的List数据,我特别是要做data [index] [address],它可以按地址搜索。因此,用户可以从“ 101 San Francisco”或“ 201 San Francisco”中搜索包含“ San Francisco”之类的地址中的任何内容的地址,只要它包含搜索关键字...
当然,请随意提高我的基本编码效率?感谢大家的投入。
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
void main() => runApp(new MaterialApp(
home: new HomePage(),
));
class HomePage extends StatefulWidget{
@override
HomePageState createState() => new HomePageState();
}
**class Search extends SearchDelegate {
@override
List<Widget> buildActions(BuildContext context) {
return <Widget> [
IconButton(
icon: Icon(Icons.clear),
onPressed: () {
query = "";
},
)
];
}
@override
Widget buildLeading(BuildContext context) {
return IconButton(onPressed: () {}, icon: Icon(Icons.arrow_back),);
}
String selectedResult;
@override
Widget buildResults(BuildContext context) {
return Container(
child: Center(
child: Text(selectedResult),
),
);
}
@override
Widget buildSuggestions(BuildContext context) {
// TODO: implement buildSuggestions
return null;
}
}**
class HomePageState extends State<HomePage>{
final String url = "https://myJson.json";
List data;
@override
void initState(){
super.initState();
this.getJsonData();
}
Future<String> getJsonData() async{
var response = await http.get(
Uri.encodeFull(url),
headers: {"Accept": "application/json"}
);
setState(() {
var convertDataToJson = jsonDecode(response.body);
data = convertDataToJson['places'];
});
return "Success";
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Welcome"),
actions: <Widget>[
IconButton(onPressed: () {
showSearch(context: context, delegate: Search());
},
icon: Icon(Icons.search),)
],
),
body: new ListView.builder(
itemCount: data == null ? 0 : data.length,
itemBuilder: (BuildContext context, int index) {
return new Container(
child: new Center(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
new Card(
child: new Container(
padding: EdgeInsets.all(20),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text(data[index]['address']),
],
)
),
)
],
),
),
);
},
),
);
}
}