Skip to content

Commit 2c0669f

Browse files
committed
🐛 Fix custom filter updates in the example
1 parent 73115af commit 2c0669f

File tree

5 files changed

+356
-306
lines changed

5 files changed

+356
-306
lines changed
Lines changed: 10 additions & 277 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import 'package:flutter/material.dart';
22
import 'package:photo_manager/photo_manager.dart';
3-
import 'package:photo_manager_example/page/custom_filter/order_by_action.dart';
3+
4+
import 'order_by_action.dart';
5+
import 'where_action.dart';
46

57
class AdvancedCustomFilterPage extends StatefulWidget {
68
const AdvancedCustomFilterPage({
@@ -16,15 +18,14 @@ class AdvancedCustomFilterPage extends StatefulWidget {
1618
}
1719

1820
class _AdvancedCustomFilterPageState extends State<AdvancedCustomFilterPage> {
19-
final List<OrderByItem> _orderBy = [
21+
List<WhereConditionItem> _where = [];
22+
List<OrderByItem> _orderBy = [
2023
OrderByItem.named(
2124
column: CustomColumns.base.createDate,
2225
isAsc: false,
2326
),
2427
];
2528

26-
final List<WhereConditionItem> _where = [];
27-
2829
late CustomFilter filter;
2930

3031
@override
@@ -48,14 +49,14 @@ class _AdvancedCustomFilterPageState extends State<AdvancedCustomFilterPage> {
4849
title: const Text('Advanced Custom Filter Example'),
4950
actions: [
5051
WhereAction(
51-
where: _where,
52+
items: _where,
5253
onChanged: (value) {
5354
if (!mounted) {
5455
return;
5556
}
5657
setState(() {
57-
_where.clear();
58-
_where.addAll(value);
58+
_where = value;
59+
filter = _createFilter();
5960
});
6061
},
6162
),
@@ -66,8 +67,8 @@ class _AdvancedCustomFilterPageState extends State<AdvancedCustomFilterPage> {
6667
return;
6768
}
6869
setState(() {
69-
_orderBy.clear();
70-
_orderBy.addAll(values);
70+
_orderBy = values;
71+
filter = _createFilter();
7172
});
7273
},
7374
),
@@ -83,271 +84,3 @@ class _AdvancedCustomFilterPageState extends State<AdvancedCustomFilterPage> {
8384
);
8485
}
8586
}
86-
87-
class WhereAction extends StatelessWidget {
88-
const WhereAction({
89-
super.key,
90-
required this.where,
91-
required this.onChanged,
92-
// required this
93-
});
94-
95-
final List<WhereConditionItem> where;
96-
final ValueChanged<List<WhereConditionItem>> onChanged;
97-
98-
@override
99-
Widget build(BuildContext context) {
100-
return IconButton(
101-
icon: const Icon(Icons.filter_alt),
102-
onPressed: () {
103-
_WhereConditionPage._saveItems = null;
104-
Navigator.push<List<WhereConditionItem>>(
105-
context,
106-
MaterialPageRoute(
107-
builder: (context) => _WhereConditionPage(where: where),
108-
),
109-
).then((value) {
110-
value ??= _WhereConditionPage._saveItems;
111-
if (value != null) {
112-
onChanged(value);
113-
}
114-
});
115-
},
116-
);
117-
}
118-
}
119-
120-
class _WhereConditionPage extends StatefulWidget {
121-
const _WhereConditionPage({
122-
required this.where,
123-
});
124-
125-
final List<WhereConditionItem> where;
126-
127-
static List<WhereConditionItem>? _saveItems;
128-
129-
@override
130-
State<_WhereConditionPage> createState() => _WhereConditionPageState();
131-
}
132-
133-
class _WhereConditionPageState extends State<_WhereConditionPage> {
134-
final List<WhereConditionItem> _where = [];
135-
136-
bool isChanged = false;
137-
138-
@override
139-
void initState() {
140-
super.initState();
141-
_where.addAll(widget.where);
142-
_WhereConditionPage._saveItems = _where;
143-
}
144-
145-
@override
146-
Widget build(BuildContext context) {
147-
return Scaffold(
148-
appBar: AppBar(
149-
title: const Text('Where Condition'),
150-
actions: [
151-
IconButton(
152-
icon: const Icon(Icons.add),
153-
onPressed: _createNew,
154-
),
155-
],
156-
),
157-
body: buildList(context),
158-
floatingActionButton: FloatingActionButton(
159-
child: const Icon(Icons.done),
160-
onPressed: () {
161-
Navigator.of(context).pop(_where);
162-
},
163-
),
164-
);
165-
}
166-
167-
Future<void> _createNew() async {
168-
final result = await showDialog<WhereConditionItem>(
169-
context: context,
170-
builder: (context) {
171-
return const _CreateWhereDialog();
172-
},
173-
);
174-
if (result != null) {
175-
setState(() {
176-
isChanged = true;
177-
_where.add(result);
178-
});
179-
}
180-
}
181-
182-
Widget buildList(BuildContext context) {
183-
return ListView.builder(
184-
itemCount: _where.length,
185-
itemBuilder: (context, index) {
186-
final item = _where[index];
187-
return ListTile(
188-
title: Text(item.display()),
189-
trailing: IconButton(
190-
icon: const Icon(Icons.delete),
191-
onPressed: () {
192-
setState(() {
193-
isChanged = true;
194-
_where.removeAt(index);
195-
});
196-
},
197-
),
198-
);
199-
},
200-
);
201-
}
202-
}
203-
204-
class _CreateWhereDialog extends StatefulWidget {
205-
const _CreateWhereDialog();
206-
207-
@override
208-
State<_CreateWhereDialog> createState() => _CreateWhereDialogState();
209-
}
210-
211-
class _CreateWhereDialogState extends State<_CreateWhereDialog> {
212-
List<String> keys() {
213-
return CustomColumns.platformValues();
214-
}
215-
216-
late String column = keys().first;
217-
String condition = '==';
218-
TextEditingController textValueController = TextEditingController();
219-
220-
DateTime _date = DateTime.now();
221-
222-
WhereConditionItem createItem() {
223-
final cond = condition;
224-
225-
if (isDateColumn()) {
226-
return DateColumnWhereCondition(
227-
column: column,
228-
operator: condition,
229-
value: _date,
230-
);
231-
}
232-
233-
final value = '$column $cond ${textValueController.text}';
234-
final item = WhereConditionItem.text(value);
235-
return item;
236-
}
237-
238-
bool isDateColumn() {
239-
final dateColumns = CustomColumns.dateColumns();
240-
return dateColumns.contains(column);
241-
}
242-
243-
@override
244-
Widget build(BuildContext context) {
245-
return AlertDialog(
246-
title: const Text('Create Where Condition'),
247-
content: Container(
248-
width: double.maxFinite,
249-
padding: const EdgeInsets.all(16.0),
250-
child: Column(
251-
mainAxisSize: MainAxisSize.min,
252-
children: [
253-
DropdownButton<String>(
254-
items: keys().map((e) {
255-
return DropdownMenuItem(
256-
value: e,
257-
child: Text(e),
258-
);
259-
}).toList(),
260-
onChanged: (value) {
261-
if (value == null) {
262-
return;
263-
}
264-
setState(() {
265-
column = value;
266-
});
267-
},
268-
value: column,
269-
),
270-
DropdownButton<String>(
271-
hint: const Text('Condition'),
272-
items: WhereConditionItem.platformConditions.map((e) {
273-
return DropdownMenuItem(
274-
value: e,
275-
child: Text(e),
276-
);
277-
}).toList(),
278-
onChanged: (value) {
279-
if (value == null) {
280-
return;
281-
}
282-
setState(() {
283-
condition = value;
284-
});
285-
},
286-
value: condition,
287-
),
288-
if (!isDateColumn())
289-
TextField(
290-
controller: textValueController,
291-
decoration: const InputDecoration(
292-
hintText: 'Input condition',
293-
),
294-
onChanged: (value) {
295-
setState(() {});
296-
},
297-
)
298-
else
299-
_datePicker(),
300-
const SizedBox(
301-
height: 16,
302-
),
303-
Text(
304-
createItem().text,
305-
style: TextStyle(
306-
color: Theme.of(context).primaryColor,
307-
fontSize: 20,
308-
),
309-
),
310-
],
311-
),
312-
),
313-
actions: [
314-
TextButton(
315-
onPressed: () {
316-
Navigator.of(context).pop();
317-
},
318-
child: const Text('Cancel'),
319-
),
320-
TextButton(
321-
onPressed: () {
322-
Navigator.of(context).pop(createItem());
323-
},
324-
child: const Text('OK'),
325-
),
326-
],
327-
);
328-
}
329-
330-
Widget _datePicker() {
331-
return Column(
332-
children: [
333-
TextButton(
334-
onPressed: () async {
335-
final date = await showDatePicker(
336-
context: context,
337-
initialDate: _date,
338-
firstDate: DateTime(1970),
339-
lastDate: DateTime(2100),
340-
);
341-
if (date == null) {
342-
return;
343-
}
344-
setState(() {
345-
_date = date;
346-
});
347-
},
348-
child: Text(_date.toIso8601String()),
349-
),
350-
],
351-
);
352-
}
353-
}

example/lib/page/custom_filter/order_by_action.dart

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,36 +38,34 @@ class OrderByAction extends StatelessWidget {
3838
Widget build(BuildContext context) {
3939
return Stack(
4040
children: [
41-
Align(
42-
alignment: Alignment.center,
43-
child: IconButton(
44-
onPressed: () async {
45-
await changeOrderBy(context, items, onChanged);
46-
},
47-
icon: const Icon(Icons.sort),
48-
),
41+
IconButton(
42+
onPressed: () async {
43+
await changeOrderBy(context, items, onChanged);
44+
},
45+
icon: const Icon(Icons.sort),
4946
),
50-
Positioned(
51-
right: 5,
52-
top: 5,
53-
child: Container(
54-
width: 15,
55-
height: 15,
56-
decoration: const BoxDecoration(
57-
color: Colors.red,
58-
shape: BoxShape.circle,
59-
),
60-
child: Center(
61-
child: Text(
62-
items.length.toString(),
63-
style: const TextStyle(
64-
color: Colors.white,
65-
fontSize: 8,
47+
if (items.isNotEmpty)
48+
Positioned(
49+
right: 5,
50+
top: 5,
51+
child: Container(
52+
width: 15,
53+
height: 15,
54+
decoration: const BoxDecoration(
55+
color: Colors.red,
56+
shape: BoxShape.circle,
57+
),
58+
child: Center(
59+
child: Text(
60+
items.length.toString(),
61+
style: const TextStyle(
62+
color: Colors.white,
63+
fontSize: 8,
64+
),
6665
),
6766
),
6867
),
6968
),
70-
),
7169
],
7270
);
7371
}
@@ -81,6 +79,7 @@ class OrderByActionPage extends StatefulWidget {
8179

8280
final List<OrderByItem> items;
8381
static List<OrderByItem>? _saveItems;
82+
8483
@override
8584
State<OrderByActionPage> createState() => _OrderByActionPageState();
8685
}

0 commit comments

Comments
 (0)