forked from gokadzev/Musify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_page.dart
201 lines (190 loc) · 6.85 KB
/
search_page.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import 'package:fluentui_system_icons/fluentui_system_icons.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:musify/API/musify.dart';
import 'package:musify/services/data_manager.dart';
import 'package:musify/style/app_themes.dart';
import 'package:musify/widgets/song_bar.dart';
import 'package:musify/widgets/spinner.dart';
class SearchPage extends StatefulWidget {
@override
_SearchPageState createState() => _SearchPageState();
}
List searchHistory = Hive.box('user').get('searchHistory', defaultValue: []);
class _SearchPageState extends State<SearchPage> {
final TextEditingController _searchBar = TextEditingController();
final ValueNotifier<bool> _fetchingSongs = ValueNotifier(false);
final FocusNode _inputNode = FocusNode();
List _searchResult = [];
List _suggestionsList = [];
Future<void> search() async {
final query = _searchBar.text;
if (query.isEmpty) {
_searchResult = [];
_suggestionsList = [];
setState(() {});
return;
}
if (!_fetchingSongs.value) {
_fetchingSongs.value = true;
}
if (!searchHistory.contains(query)) {
searchHistory.insert(0, query);
addOrUpdateData('user', 'searchHistory', searchHistory);
}
try {
_searchResult = await fetchSongsList(query);
} catch (e) {
debugPrint('Error while searching online songs: $e');
}
if (_fetchingSongs.value) {
_fetchingSongs.value = false;
}
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
AppLocalizations.of(context)!.search,
),
),
body: SingleChildScrollView(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(
top: 12,
bottom: 20,
left: 12,
right: 12,
),
child: TextField(
onSubmitted: (String value) {
search();
_suggestionsList = [];
FocusManager.instance.primaryFocus?.unfocus();
},
onChanged: (value) {
setState(() {
if (value != '') {
getSearchSuggestions(value)
.then((value) => _suggestionsList = value);
} else {
_suggestionsList = [];
}
});
},
textInputAction: TextInputAction.search,
controller: _searchBar,
focusNode: _inputNode,
style: TextStyle(
fontSize: 16,
color: colorScheme.primary,
),
cursorColor: Colors.green[50],
decoration: InputDecoration(
focusedBorder: OutlineInputBorder(
borderRadius: const BorderRadius.all(
Radius.circular(15),
),
borderSide: BorderSide(color: colorScheme.primary),
),
suffixIcon: ValueListenableBuilder<bool>(
valueListenable: _fetchingSongs,
builder: (_, value, __) {
if (value == true) {
return IconButton(
icon: const SizedBox(
height: 18,
width: 18,
child: Spinner(),
),
color: colorScheme.primary,
onPressed: () {
search();
FocusManager.instance.primaryFocus?.unfocus();
},
);
} else {
return IconButton(
icon: Icon(
FluentIcons.search_20_regular,
color: colorScheme.primary,
),
color: colorScheme.primary,
onPressed: () {
search();
FocusManager.instance.primaryFocus?.unfocus();
},
);
}
},
),
hintText: '${AppLocalizations.of(context)!.search}...',
hintStyle: TextStyle(
color: colorScheme.primary,
),
),
),
),
if (_searchResult.isEmpty)
ListView.builder(
shrinkWrap: true,
addAutomaticKeepAlives: false,
addRepaintBoundaries: false,
physics: const NeverScrollableScrollPhysics(),
itemCount: _suggestionsList.isEmpty
? searchHistory.length
: _suggestionsList.length,
itemBuilder: (BuildContext ctxt, int index) {
final suggestionsNotAvailable = _suggestionsList.isEmpty;
return Padding(
padding: const EdgeInsets.only(top: 8, bottom: 6),
child: Card(
child: ListTile(
leading: Icon(
FluentIcons.search_24_regular,
color: colorScheme.primary,
),
title: Text(
suggestionsNotAvailable
? searchHistory[index]
: _suggestionsList[index],
),
onTap: () async {
_searchBar.text = suggestionsNotAvailable
? searchHistory[index]
: _suggestionsList[index];
await search();
},
),
),
);
},
)
else
ListView.builder(
shrinkWrap: true,
addAutomaticKeepAlives: false,
addRepaintBoundaries: false,
physics: const NeverScrollableScrollPhysics(),
itemCount: _searchResult.length,
itemBuilder: (BuildContext ctxt, int index) {
return Padding(
padding: const EdgeInsets.only(top: 5, bottom: 5),
child: SongBar(
_searchResult[index],
true,
),
);
},
)
],
),
),
);
}
}