-
Notifications
You must be signed in to change notification settings - Fork 14
/
multi_searcher_app.dart
163 lines (154 loc) · 5 KB
/
multi_searcher_app.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import 'package:flutter/material.dart';
import 'package:algolia_helper_flutter/algolia_helper_flutter.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Algolia Helpers for Flutter',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
// Create a multi searcher.
// The Searcher performs search requests and obtains search result
late MultiSearcher multiSearcher;
late FacetSearcher facetSearcher;
late HitsSearcher hitsSearcher;
_MyHomePageState() {
multiSearcher = MultiSearcher(
applicationID: 'latency',
apiKey: '1f6fd3a6fb973cb08419fe7d288fa4db',
);
hitsSearcher = multiSearcher.addHitsSearcher(
initialState: const SearchState(
indexName: 'instant_search',
),
);
facetSearcher = multiSearcher.addFacetSearcher(
initialState: const FacetSearchState(
facet: 'brand',
searchState: SearchState(
indexName: 'instant_search',
),
));
}
SizedBox header(String title) => SizedBox(
width: double.infinity,
child: Padding(
padding: const EdgeInsets.only(left: 15),
child: Text(
title,
textAlign: TextAlign.left,
style: const TextStyle(fontWeight: FontWeight.w800),
),
),
);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Container(
width: double.infinity,
height: 40,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(5),
),
child: Center(
child: TextField(
onChanged: (input) {
facetSearcher.query(input);
hitsSearcher.query(input);
}, // 3. Run your search operations
decoration: const InputDecoration(
prefixIcon: Icon(Icons.search),
hintText: 'Search...',
border: InputBorder.none,
),
),
),
),
),
body: Column(children: [
header('Brands'),
Expanded(
child: StreamBuilder<FacetSearchResponse>(
// Listen and display facet search results
stream: facetSearcher.responses,
builder: (BuildContext context,
AsyncSnapshot<FacetSearchResponse> snapshot) {
if (snapshot.hasData) {
final response = snapshot.data;
final facets = response?.facetHits.toList() ?? [];
return ListView.builder(
itemCount: facets.length,
itemBuilder: (BuildContext context, int index) {
final facet = facets[index];
return ListTile(
title: RichText(
text: TextSpan(
style: Theme.of(context).textTheme.titleSmall,
children: facet.getHighlightedString().toInlineSpans()
..add(TextSpan(text: '(${facet.count})')),
),
),
);
},
);
} else {
return const CircularProgressIndicator();
}
},
),
),
const Divider(),
header('Items'),
Expanded(
child: StreamBuilder<SearchResponse>(
// Listen and display hits search results
stream: hitsSearcher.responses,
builder:
(BuildContext context, AsyncSnapshot<SearchResponse> snapshot) {
if (snapshot.hasData) {
final response = snapshot.data;
final hits = response?.hits.toList() ?? [];
return ListView.builder(
itemCount: hits.length,
itemBuilder: (BuildContext context, int index) {
final hit = hits[index];
return ListTile(
title: RichText(
text: TextSpan(
style: Theme.of(context).textTheme.titleSmall,
children:
hit.getHighlightedString('name').toInlineSpans(),
),
),
);
},
);
} else {
return const CircularProgressIndicator();
}
},
),
)
]),
);
}
}