Skip to content

Commit baea8ae

Browse files
authored
Merge pull request #347 from sudhar08/feat-Add-a-search/filter-for-collection-pane-#305-
feat: Add a search/filter for collection pane
2 parents 345f71a + 8a8ded7 commit baea8ae

File tree

4 files changed

+125
-40
lines changed

4 files changed

+125
-40
lines changed

lib/providers/ui_providers.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ final nameTextFieldFocusNodeProvider =
2323
});
2424
return focusNode;
2525
});
26+
27+
final searchQueryProvider = StateProvider<String>((ref) => '');

lib/screens/home_page/collection_pane.dart

Lines changed: 82 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,39 @@ class CollectionPane extends ConsumerWidget {
6969
],
7070
),
7171
),
72-
kVSpacer8,
72+
kVSpacer10,
73+
Container(
74+
height: 30,
75+
margin: const EdgeInsets.only(right: 8),
76+
decoration: BoxDecoration(
77+
borderRadius: kBorderRadius8,
78+
border: Border.all(
79+
color: Theme.of(context).colorScheme.surfaceVariant,
80+
),
81+
),
82+
child: Row(
83+
children: [
84+
kHSpacer5,
85+
Icon(
86+
Icons.filter_alt,
87+
size: 18,
88+
color: Theme.of(context).colorScheme.secondary,
89+
),
90+
kHSpacer5,
91+
Expanded(
92+
child: RawTextField(
93+
style: Theme.of(context).textTheme.bodyMedium,
94+
hintText: "Filter by name or URL",
95+
onChanged: (value) {
96+
ref.read(searchQueryProvider.notifier).state =
97+
value.toLowerCase();
98+
},
99+
),
100+
),
101+
],
102+
),
103+
),
104+
kVSpacer10,
73105
const Expanded(
74106
child: RequestList(),
75107
),
@@ -109,41 +141,61 @@ class _RequestListState extends ConsumerState<RequestList> {
109141
final requestItems = ref.watch(collectionStateNotifierProvider)!;
110142
final alwaysShowCollectionPaneScrollbar = ref.watch(settingsProvider
111143
.select((value) => value.alwaysShowCollectionPaneScrollbar));
144+
final filterQuery = ref.watch(searchQueryProvider).trim();
112145

113146
return Scrollbar(
114147
controller: controller,
115148
thumbVisibility: alwaysShowCollectionPaneScrollbar ? true : null,
116149
radius: const Radius.circular(12),
117-
child: ReorderableListView.builder(
118-
padding: kPe8,
119-
scrollController: controller,
120-
buildDefaultDragHandles: false,
121-
itemCount: requestSequence.length,
122-
onReorder: (int oldIndex, int newIndex) {
123-
if (oldIndex < newIndex) {
124-
newIndex -= 1;
125-
}
126-
if (oldIndex != newIndex) {
127-
ref
128-
.read(collectionStateNotifierProvider.notifier)
129-
.reorder(oldIndex, newIndex);
130-
}
131-
},
132-
itemBuilder: (context, index) {
133-
var id = requestSequence[index];
134-
return ReorderableDragStartListener(
135-
key: ValueKey(id),
136-
index: index,
137-
child: Padding(
138-
padding: kP1,
139-
child: RequestItem(
140-
id: id,
141-
requestModel: requestItems[id]!,
142-
),
150+
child: filterQuery.isEmpty
151+
? ReorderableListView.builder(
152+
padding: kPe8,
153+
scrollController: controller,
154+
buildDefaultDragHandles: false,
155+
itemCount: requestSequence.length,
156+
onReorder: (int oldIndex, int newIndex) {
157+
if (oldIndex < newIndex) {
158+
newIndex -= 1;
159+
}
160+
if (oldIndex != newIndex) {
161+
ref
162+
.read(collectionStateNotifierProvider.notifier)
163+
.reorder(oldIndex, newIndex);
164+
}
165+
},
166+
itemBuilder: (context, index) {
167+
var id = requestSequence[index];
168+
return ReorderableDragStartListener(
169+
key: ValueKey(id),
170+
index: index,
171+
child: Padding(
172+
padding: kP1,
173+
child: RequestItem(
174+
id: id,
175+
requestModel: requestItems[id]!,
176+
),
177+
),
178+
);
179+
},
180+
)
181+
: ListView(
182+
padding: kPe8,
183+
controller: controller,
184+
children: requestSequence.map((id) {
185+
var item = requestItems[id]!;
186+
if (item.url.toLowerCase().contains(filterQuery) ||
187+
item.name.toLowerCase().contains(filterQuery)) {
188+
return Padding(
189+
padding: kP1,
190+
child: RequestItem(
191+
id: id,
192+
requestModel: item,
193+
),
194+
);
195+
}
196+
return const SizedBox();
197+
}).toList(),
143198
),
144-
);
145-
},
146-
),
147199
);
148200
}
149201
}

lib/widgets/textfields.dart

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,39 @@ class JsonSearchField extends StatelessWidget {
9494

9595
@override
9696
Widget build(BuildContext context) {
97-
return TextField(
97+
return RawTextField(
9898
controller: controller,
9999
onChanged: onChanged,
100100
style: kCodeStyle,
101-
decoration: const InputDecoration(
101+
hintText: 'Search..',
102+
);
103+
}
104+
}
105+
106+
class RawTextField extends StatelessWidget {
107+
const RawTextField({
108+
super.key,
109+
this.onChanged,
110+
this.controller,
111+
this.hintText,
112+
this.style,
113+
});
114+
115+
final void Function(String)? onChanged;
116+
final TextEditingController? controller;
117+
final String? hintText;
118+
final TextStyle? style;
119+
120+
@override
121+
Widget build(BuildContext context) {
122+
return TextField(
123+
controller: controller,
124+
onChanged: onChanged,
125+
style: style,
126+
decoration: InputDecoration(
102127
isDense: true,
103128
border: InputBorder.none,
104-
hintText: 'Search..',
129+
hintText: hintText,
105130
),
106131
);
107132
}

test/providers/ui_providers_test.dart

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,13 @@ void main() {
214214
});
215215

216216
group("Testing selectedIdEditStateProvider", () {
217-
testWidgets(
218-
'selectedIdEditStateProvider should have an initial value of null',
219-
(tester) async {
217+
testWidgets('It should have an initial value of null', (tester) async {
220218
await tester.pumpWidget(
221219
const ProviderScope(
222220
child: MaterialApp(
223-
home: CollectionPane(),
221+
home: Scaffold(
222+
body: CollectionPane(),
223+
),
224224
),
225225
),
226226
);
@@ -237,7 +237,9 @@ void main() {
237237
await tester.pumpWidget(
238238
const ProviderScope(
239239
child: MaterialApp(
240-
home: CollectionPane(),
240+
home: Scaffold(
241+
body: CollectionPane(),
242+
),
241243
),
242244
),
243245
);
@@ -267,7 +269,9 @@ void main() {
267269
await tester.pumpWidget(
268270
const ProviderScope(
269271
child: MaterialApp(
270-
home: CollectionPane(),
272+
home: Scaffold(
273+
body: CollectionPane(),
274+
),
271275
),
272276
),
273277
);
@@ -303,7 +307,9 @@ void main() {
303307
await tester.pumpWidget(
304308
const ProviderScope(
305309
child: MaterialApp(
306-
home: CollectionPane(),
310+
home: Scaffold(
311+
body: CollectionPane(),
312+
),
307313
),
308314
),
309315
);

0 commit comments

Comments
 (0)