@@ -26,88 +26,72 @@ class ConnectedThingSearchField extends StatelessWidget {
26
26
27
27
@override
28
28
Widget build (BuildContext context) {
29
- return TextField (
30
- controller: _textController,
31
- onSubmitted: (_) => _submit (),
32
- decoration: InputDecoration (
33
- hintText: 'Enter Item Number' ,
34
- prefixIcon: const Icon (Icons .numbers),
35
- suffixIcon: IconButton (
36
- tooltip: 'Add Item' ,
37
- onPressed: () => _submit (),
38
- icon: const Icon (Icons .add_rounded),
39
- ),
40
- ),
41
- inputFormatters: [
42
- FilteringTextInputFormatter .digitsOnly,
43
- ],
44
- );
29
+ return ListenableBuilder (
30
+ listenable: controller,
31
+ builder: (context, child) {
32
+ return TextField (
33
+ controller: _textController,
34
+ onSubmitted: (_) => _submit (),
35
+ decoration: InputDecoration (
36
+ errorText: controller.errorText,
37
+ hintText: 'Enter Item Number' ,
38
+ prefixIcon: const Icon (Icons .numbers),
39
+ suffixIcon: IconButton (
40
+ tooltip: 'Add Item' ,
41
+ onPressed: () => _submit (),
42
+ icon: const Icon (Icons .add_rounded),
43
+ ),
44
+ ),
45
+ inputFormatters: [
46
+ FilteringTextInputFormatter .digitsOnly,
47
+ ],
48
+ );
49
+ });
45
50
}
46
51
}
47
52
48
- class ThingSearchController {
53
+ class ThingSearchController extends ChangeNotifier {
49
54
final BuildContext context;
55
+ final List <ItemModel > items;
50
56
final InventoryRepository repository;
51
57
final void Function (ItemModel ) onMatchFound;
52
58
53
59
bool isLoading = false ;
54
60
61
+ String ? errorText;
62
+
55
63
ThingSearchController ({
56
64
required this .context,
65
+ required this .items,
57
66
required this .repository,
58
67
required this .onMatchFound,
59
68
});
60
69
61
70
Future <void > search (String value) async {
71
+ final itemNumber = int .parse (value);
72
+
73
+ if (items.any ((t) => t.number == itemNumber)) {
74
+ errorText = '#$value is already added to this loan.' ;
75
+ notifyListeners ();
76
+ return ;
77
+ }
78
+
62
79
isLoading = true ;
63
- final match = await repository.getItem (number: int . parse (value) );
80
+ final match = await repository.getItem (number: itemNumber );
64
81
isLoading = false ;
65
82
66
- if (match != null ) {
67
- if (! match.available) {
68
- _showThingCheckedOutDialog (match);
69
- } else {
70
- onMatchFound (match);
71
- }
72
- } else {
73
- _showUnknownThingDialog (value);
83
+ if (match == null ) {
84
+ errorText = '#$value could not be found.' ;
85
+ notifyListeners ();
86
+ return ;
74
87
}
75
- }
76
88
77
- void _showThingCheckedOutDialog (ItemModel thing) {
78
- showDialog (
79
- context: context,
80
- builder: (context) {
81
- return AlertDialog (
82
- title: const Text ("Item Unavailable" ),
83
- content: Text (
84
- "Item #${thing .number } is checked out or not available for lending." ),
85
- actions: [
86
- TextButton (
87
- child: const Text ("OK" ),
88
- onPressed: () => Navigator .pop (context),
89
- )
90
- ],
91
- );
92
- },
93
- );
94
- }
89
+ if (! match.available) {
90
+ errorText = '#$value is unavailable.' ;
91
+ notifyListeners ();
92
+ return ;
93
+ }
95
94
96
- void _showUnknownThingDialog (String searchValue) {
97
- showDialog (
98
- context: context,
99
- builder: (context) {
100
- return AlertDialog (
101
- title: Text ("Item #$searchValue does not exist." ),
102
- content: const Text ("Try another number." ),
103
- actions: [
104
- TextButton (
105
- child: const Text ("OK" ),
106
- onPressed: () => Navigator .pop (context),
107
- )
108
- ],
109
- );
110
- },
111
- );
95
+ onMatchFound (match);
112
96
}
113
97
}
0 commit comments