我正在将应用程序从离子迁移到Flutter,并停留在搜索功能上。我需要在ListViewbuilder中添加简单的搜索栏,但不知道如何。
我的代码是
class _HospitallistState extends State<Hospitallist> {
@override
void initState() {
doSomeAsyncStuff();
super.initState();
}
Future<List> doSomeAsyncStuff() async {
String url2 =
'http://api.igiinsurance.com.pk:8888/insurance_igitakaful/insurance-api/get_panel_hospitals.php?offset=0&limit=1000';
final response2 = await http.get(url2);
var Data = json.decode(response2.body);
print(Data["records"]);
return Data["records"];
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text('IGI GENERAL INSURANCE'),
),
body: FutureBuilder<List>(
future: doSomeAsyncStuff(),
builder: (BuildContext context, AsyncSnapshot<List> snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
return Padding(
padding: EdgeInsets.all(3),
child: Card(
child: Padding(
padding: EdgeInsets.all(8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new RichText(
text: new TextSpan(
style: new TextStyle(
fontSize: 14.0,
color: Colors.black,
),
children: <TextSpan>[
new TextSpan(text: 'Name' + ' ' + ':' + ' '),
new TextSpan(
text: snapshot.data[index]['name'],
style: new TextStyle(
fontWeight: FontWeight.bold,
color: Colors.blue)),
],
),
),
Text('Address : ' +
' ' +
snapshot.data[index]['address'])
],
),
),
),
);
},
);
}
}),
);
}
}
我听说列表视图生成器中有一些内置函数,可以从中添加搜索选项?不确定,或者是否还有其他方法可以做到这一点?谢谢
答案 0 :(得分:0)
有一个简单的blog。您可以检查以下代码:
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'ListView with Search'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
TextEditingController editingController = TextEditingController();
final duplicateItems = List<String>.generate(10000, (i) => "Item $i");
var items = List<String>();
@override
void initState() {
items.addAll(duplicateItems);
super.initState();
}
void filterSearchResults(String query) {
List<String> dummySearchList = List<String>();
dummySearchList.addAll(duplicateItems);
if(query.isNotEmpty) {
List<String> dummyListData = List<String>();
dummySearchList.forEach((item) {
if(item.contains(query)) {
dummyListData.add(item);
}
});
setState(() {
items.clear();
items.addAll(dummyListData);
});
return;
} else {
setState(() {
items.clear();
items.addAll(duplicateItems);
});
}
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: Container(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
onChanged: (value) {
filterSearchResults(value);
},
controller: editingController,
decoration: InputDecoration(
labelText: "Search",
hintText: "Search",
prefixIcon: Icon(Icons.search),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(25.0)))),
),
),
Expanded(
child: ListView.builder(
shrinkWrap: true,
itemCount: items.length,
itemBuilder: (context, index) {
return ListTile(
title: Text('${items[index]}'),
);
},
),
),
],
),
),
);
}
}