-
Notifications
You must be signed in to change notification settings - Fork 6
/
search_page.dart
93 lines (85 loc) · 2.48 KB
/
search_page.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import 'package:flutter_search/services/search_service.dart';
import 'package:flutter/material.dart';
import 'dart:convert';
class SearchPage extends StatefulWidget {
@override
_SearchPageState createState() => _SearchPageState();
}
class _SearchPageState extends State<SearchPage> {
List<dynamic> searchResults = [];
searchDjango(value) async {
SearchService.searchDjangoApi(value).then((responseBody) {
List<dynamic> data = jsonDecode(responseBody);
setState(() {
data.forEach((value) {
searchResults.add(value);
});
});
print(data.runtimeType.toString());
print(searchResults.runtimeType.toString());
});
}
@override
Widget build(BuildContext context) {
print("build function: ${searchResults.toString()}");
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: Text("Django API Search"),
centerTitle: true,
),
body: ListView(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(10.0),
child: TextField(
onChanged: (val) {
searchResults.clear();
searchDjango(val);
},
decoration: InputDecoration(
contentPadding: EdgeInsets.only(left: 25.0),
hintText: 'Search by Document Field',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(4.0),
),
suffixIcon: IconButton(
icon: Icon(Icons.search),
onPressed: () {
// initiate search on press
},
),
),
),
),
SizedBox(
height: 10.0,
),
ListView.builder(
shrinkWrap: true,
itemCount: searchResults.length,
itemBuilder: (BuildContext context, int index) {
return buildResultCard(searchResults[index]);
},
),
],
),
),
);
}
}
Widget buildResultCard(data) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: <Widget>[
ListTile(
title: Text(data['question_text']),
subtitle: Text(data['author']),
),
Divider(color: Colors.black)
],
),
);
}