Skip to content

Commit 90a4c55

Browse files
committed
text cases for serach bar completed
1 parent 01addcf commit 90a4c55

File tree

3 files changed

+206
-34
lines changed

3 files changed

+206
-34
lines changed

example/lib/main_temp.dart

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -447,38 +447,38 @@ class _MyHomePageState extends State<MyHomePage>
447447
// ),
448448
// ),
449449

450-
// GFSearchBar(
451-
// // searchBoxInputDecoration: InputDecoration(
452-
// // enabledBorder: OutlineInputBorder(
453-
// // borderSide: BorderSide(
454-
// // color: Colors.teal,
455-
// // ),
456-
// // borderRadius: BorderRadius.circular(50)
457-
// // ),
458-
// // ),
459-
// searchList: list,
460-
// // hideSearchBoxWhenItemSelected: false,
461-
// // overlaySearchListHeight: 100.0,
462-
// searchQueryBuilder: (query, list) => list
463-
// .where((item) =>
464-
// item.toLowerCase().contains(query.toLowerCase()))
465-
// .toList(),
466-
// overlaySearchListItemBuilder: (item) => Container(
467-
// padding: const EdgeInsets.all(8),
468-
// child: Text(
469-
// item,
470-
// style: const TextStyle(fontSize: 18),
471-
// ),
472-
// ),
473-
// // noItemsFoundWidget: Container(
474-
// // color: Colors.green,
475-
// // child: Text("no items found..."),
476-
// // ),
477-
// onItemSelected: (item) {
478-
// setState(() {
479-
// print('selected item $item');
480-
// });
481-
// }),
450+
GFSearchBar(
451+
// searchBoxInputDecoration: InputDecoration(
452+
// enabledBorder: OutlineInputBorder(
453+
// borderSide: BorderSide(
454+
// color: Colors.teal,
455+
// ),
456+
// borderRadius: BorderRadius.circular(50)
457+
// ),
458+
// ),
459+
searchList: list,
460+
// hideSearchBoxWhenItemSelected: false,
461+
// overlaySearchListHeight: 100.0,
462+
searchQueryBuilder: (query, list) => list
463+
.where((item) =>
464+
item.toLowerCase().contains(query.toLowerCase()))
465+
.toList(),
466+
overlaySearchListItemBuilder: (item) => Container(
467+
padding: const EdgeInsets.all(8),
468+
child: Text(
469+
item,
470+
style: const TextStyle(fontSize: 18),
471+
),
472+
),
473+
// noItemsFoundWidget: Container(
474+
// color: Colors.green,
475+
// child: Text("no items found..."),
476+
// ),
477+
onItemSelected: (item) {
478+
setState(() {
479+
print('selected item $item');
480+
});
481+
}),
482482

483483
// GFCard(
484484
// content: Column(

lib/components/search_bar/gf_search_bar.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ typedef QueryBuilder<T> = List<T> Function(
99
);
1010

1111
class GFSearchBar<T> extends StatefulWidget {
12-
/// search bar with variuos customization option
12+
/// search bar with various customization option
1313
const GFSearchBar({
1414
@required this.searchList,
1515
@required this.overlaySearchListItemBuilder,
@@ -21,7 +21,10 @@ class GFSearchBar<T> extends StatefulWidget {
2121
this.overlaySearchListHeight,
2222
this.noItemsFoundWidget,
2323
this.searchBoxInputDecoration,
24-
}) : super(key: key);
24+
}) : assert(searchList != null),
25+
assert(overlaySearchListItemBuilder != null),
26+
assert(searchQueryBuilder != null),
27+
super(key: key);
2528

2629
/// List of text or [Widget] reference for users
2730
final List<T> searchList;

test/search_bar_test.dart

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
import 'package:getwidget/getwidget.dart';
4+
5+
void main() {
6+
const List searchList = [
7+
'Flutter',
8+
'React',
9+
'Ionic',
10+
'Xamarin',
11+
'Flutter2',
12+
'React2',
13+
'Ionic2',
14+
'Xamarin2',
15+
];
16+
17+
final noItemsFound = Container(
18+
child: const Text('no items found...'),
19+
);
20+
21+
final decoration = InputDecoration(
22+
enabledBorder: OutlineInputBorder(
23+
borderSide: const BorderSide(
24+
color: Colors.teal,
25+
),
26+
borderRadius: BorderRadius.circular(50)),
27+
);
28+
29+
testWidgets(
30+
'GF SearchBar without searchList, overlaySearchListItemBuilder, searchQueryBuilder',
31+
(tester) async {
32+
// `GFSearchBar.searchList` null or `GFSearchBar.overlaySearchListItemBuilder` null
33+
// or `GFSearchBar.searchQueryBuilder` null.
34+
expect(
35+
() => GFSearchBar(
36+
searchList: null,
37+
overlaySearchListItemBuilder: null,
38+
searchQueryBuilder: null,
39+
),
40+
throwsAssertionError,
41+
);
42+
});
43+
44+
testWidgets('GF SearchBar can be constructed', (tester) async {
45+
final GFSearchBar searchBar = GFSearchBar(
46+
searchList: searchList,
47+
overlaySearchListItemBuilder: (item) => Container(
48+
padding: const EdgeInsets.all(8),
49+
child: Text(
50+
item,
51+
style: const TextStyle(fontSize: 18),
52+
),
53+
),
54+
searchQueryBuilder: (query, list) => list
55+
.where((item) => item.toLowerCase().contains(query.toLowerCase()))
56+
.toList(),
57+
);
58+
59+
final TestApp app = TestApp(searchBar);
60+
61+
await tester.pumpWidget(app);
62+
});
63+
64+
testWidgets('Can hide searchBox when item selected', (tester) async {
65+
final GFSearchBar searchBar = GFSearchBar(
66+
searchList: searchList,
67+
overlaySearchListItemBuilder: (item) => Container(
68+
padding: const EdgeInsets.all(8),
69+
child: Text(
70+
item,
71+
style: const TextStyle(fontSize: 18),
72+
),
73+
),
74+
hideSearchBoxWhenItemSelected: true,
75+
searchQueryBuilder: (query, list) => list
76+
.where((item) => item.toLowerCase().contains(query.toLowerCase()))
77+
.toList(),
78+
overlaySearchListHeight: 115,
79+
);
80+
81+
final TestApp app = TestApp(searchBar);
82+
83+
await tester.pumpWidget(app);
84+
85+
expect(app.searchBar.hideSearchBoxWhenItemSelected, isTrue);
86+
expect(app.searchBar.overlaySearchListHeight, 115);
87+
});
88+
89+
testWidgets('On item selected and when item not found in GFSearchBar List',
90+
(tester) async {
91+
final GFSearchBar searchBar = GFSearchBar(
92+
searchList: searchList,
93+
overlaySearchListItemBuilder: (item) => Container(
94+
padding: const EdgeInsets.all(8),
95+
child: Text(
96+
item,
97+
style: const TextStyle(fontSize: 18),
98+
),
99+
),
100+
hideSearchBoxWhenItemSelected: true,
101+
searchQueryBuilder: (query, list) => list
102+
.where((item) => item.toLowerCase().contains(query.toLowerCase()))
103+
.toList(),
104+
onItemSelected: (item) {
105+
print('selected item $item');
106+
},
107+
noItemsFoundWidget: noItemsFound,
108+
);
109+
110+
final TestApp app = TestApp(searchBar);
111+
112+
await tester.pumpWidget(app);
113+
114+
expect(app.searchBar.hideSearchBoxWhenItemSelected, isTrue);
115+
expect(app.searchBar.noItemsFoundWidget, noItemsFound);
116+
});
117+
118+
testWidgets('GFSearchBar with search box input decoration', (tester) async {
119+
final GFSearchBar searchBar = GFSearchBar(
120+
searchList: searchList,
121+
overlaySearchListItemBuilder: (item) => Container(
122+
padding: const EdgeInsets.all(8),
123+
child: Text(
124+
item,
125+
style: const TextStyle(fontSize: 18),
126+
),
127+
),
128+
hideSearchBoxWhenItemSelected: true,
129+
searchQueryBuilder: (query, list) => list
130+
.where((item) => item.toLowerCase().contains(query.toLowerCase()))
131+
.toList(),
132+
onItemSelected: (item) {
133+
print('selected item $item');
134+
},
135+
noItemsFoundWidget: noItemsFound,
136+
searchBoxInputDecoration: decoration,
137+
);
138+
139+
final TestApp app = TestApp(searchBar);
140+
141+
await tester.pumpWidget(app);
142+
143+
expect(app.searchBar.hideSearchBoxWhenItemSelected, isTrue);
144+
expect(app.searchBar.noItemsFoundWidget, noItemsFound);
145+
expect(app.searchBar.searchBoxInputDecoration, decoration);
146+
});
147+
}
148+
149+
class TestApp extends StatefulWidget {
150+
const TestApp(this.searchBar);
151+
152+
final GFSearchBar searchBar;
153+
154+
@override
155+
_TestAppState createState() => _TestAppState();
156+
}
157+
158+
class _TestAppState extends State<TestApp> {
159+
@override
160+
Widget build(BuildContext context) => MaterialApp(
161+
home: Scaffold(
162+
body: Column(
163+
children: [
164+
widget.searchBar,
165+
],
166+
),
167+
),
168+
);
169+
}

0 commit comments

Comments
 (0)