forked from md-siam/widget_of_the_day
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshow_search.dart
102 lines (95 loc) · 2.25 KB
/
show_search.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
import 'package:flutter/material.dart';
class MyShowSearch extends StatefulWidget {
const MyShowSearch({Key? key}) : super(key: key);
@override
State<MyShowSearch> createState() => _MyShowSearchState();
}
class _MyShowSearchState extends State<MyShowSearch> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Show Search'),
actions: [
IconButton(
onPressed: () {
showSearch(
context: context,
delegate: CustomSearchDelegate(),
);
},
icon: const Icon(Icons.search),
),
],
),
);
}
}
class CustomSearchDelegate extends SearchDelegate {
List<String> searchTerms = [
'Apple',
'Banana',
'Pear',
'Watermelons',
'Oranges',
'Blueberries',
'Strawberries',
'Raspberries'
];
@override
List<Widget> buildActions(BuildContext context) {
return [
IconButton(
onPressed: () {
query = '';
},
icon: const Icon(Icons.clear),
),
];
}
@override
Widget? buildLeading(BuildContext context) {
return IconButton(
onPressed: () {
close(context, null);
},
icon: const Icon(Icons.arrow_back),
);
}
@override
Widget buildResults(BuildContext context) {
List<String> matchQuery = [];
for (var fruit in searchTerms) {
if (fruit.toLowerCase().contains(query.toLowerCase())) {
matchQuery.add(fruit);
}
}
return ListView.builder(
itemCount: matchQuery.length,
itemBuilder: (BuildContext context, int index) {
var result = matchQuery[index];
return ListTile(
title: Text(result),
);
},
);
}
@override
Widget buildSuggestions(BuildContext context) {
List<String> matchQuery = [];
for (var fruit in searchTerms) {
if (fruit.toLowerCase().contains(query.toLowerCase())) {
matchQuery.add(fruit);
}
}
return ListView.builder(
itemCount: matchQuery.length,
itemBuilder: (BuildContext context, int index) {
var result = matchQuery[index];
return ListTile(
title: Text(result),
);
},
);
}
}