ente/lib/ui/viewer/search/search_widget.dart

156 lines
5.3 KiB
Dart
Raw Normal View History

2022-07-18 04:45:06 +00:00
import 'package:flutter/material.dart';
import 'package:photos/ente_theme_data.dart';
import 'package:photos/models/collection_items.dart';
import 'package:photos/models/file.dart';
2022-08-04 16:16:16 +00:00
import 'package:photos/models/search/album_search_result.dart';
import 'package:photos/models/search/file_search_result.dart';
2022-08-08 04:16:06 +00:00
import 'package:photos/models/search/location_search_result.dart';
2022-08-04 16:16:16 +00:00
import 'package:photos/models/search/search_results.dart';
import 'package:photos/services/search_service.dart';
2022-08-10 06:28:16 +00:00
import 'package:photos/ui/viewer/search/search_suggestions.dart';
import 'package:photos/utils/navigation_util.dart';
2022-07-18 04:45:06 +00:00
class SearchIconWidget extends StatefulWidget {
2022-07-28 05:08:20 +00:00
const SearchIconWidget({Key key}) : super(key: key);
2022-07-18 04:45:06 +00:00
@override
State<SearchIconWidget> createState() => _SearchIconWidgetState();
}
class _SearchIconWidgetState extends State<SearchIconWidget> {
2022-07-28 05:08:20 +00:00
@override
void initState() {
super.initState();
}
2022-07-18 04:45:06 +00:00
@override
Widget build(BuildContext context) {
2022-08-05 11:08:25 +00:00
return Hero(
2022-08-06 18:11:23 +00:00
tag: "search_icon",
2022-08-05 11:08:25 +00:00
child: IconButton(
onPressed: () {
setState(
() {
Navigator.push(
context,
TransparentRoute(
builder: (BuildContext context) => const SearchWidget(),
),
);
},
);
},
icon: const Icon(Icons.search),
),
);
2022-07-18 04:45:06 +00:00
}
}
2022-07-18 04:45:06 +00:00
class SearchWidget extends StatefulWidget {
final String searchQuery = '';
const SearchWidget({Key key}) : super(key: key);
@override
State<SearchWidget> createState() => _SearchWidgetState();
}
class _SearchWidgetState extends State<SearchWidget> {
final List<SearchResult> results = [];
@override
Widget build(BuildContext context) {
return SafeArea(
2022-08-06 08:15:42 +00:00
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 1.5),
child: Column(
children: [
const SizedBox(height: 8),
Row(
children: [
Flexible(
child: Container(
color: Theme.of(context).colorScheme.defaultBackgroundColor,
child: TextFormField(
style: Theme.of(context).textTheme.subtitle1,
decoration: InputDecoration(
2022-08-08 04:16:06 +00:00
hintText: 'Search for albums, locations & files',
2022-08-06 08:15:42 +00:00
filled: true,
contentPadding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 14,
),
border: UnderlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.circular(8),
),
focusedBorder: const UnderlineInputBorder(
borderSide: BorderSide.none,
),
prefixIcon: Hero(
2022-08-06 18:11:23 +00:00
tag: "search_icon",
2022-08-06 08:15:42 +00:00
child: Icon(
Icons.search,
color: Theme.of(context)
.colorScheme
.iconColor
.withOpacity(0.5),
),
),
suffixIcon: IconButton(
onPressed: () {
Navigator.pop(context);
},
icon: Icon(
Icons.close,
color: Theme.of(context)
.colorScheme
.iconColor
.withOpacity(0.5),
),
),
),
2022-08-06 08:15:42 +00:00
onChanged: (value) async {
2022-08-10 06:28:16 +00:00
final List<SearchResult> allResults =
await getSearchResultsForQuery(value);
if (mounted) {
setState(() {
results.clear();
results.addAll(allResults);
});
}
2022-08-06 08:15:42 +00:00
},
autofocus: true,
),
),
),
2022-08-06 08:15:42 +00:00
],
),
results.isNotEmpty
2022-08-10 06:28:16 +00:00
? SearchSuggestionsWidget(results)
2022-08-06 08:15:42 +00:00
: const SizedBox.shrink(),
],
),
),
);
2022-07-18 04:45:06 +00:00
}
2022-08-05 11:08:25 +00:00
2022-08-10 06:28:16 +00:00
Future<List<SearchResult>> getSearchResultsForQuery(String query) async {
final List<SearchResult> allResults = [];
2022-08-05 11:08:25 +00:00
2022-08-10 06:28:16 +00:00
final collectionResults =
2022-08-11 06:20:39 +00:00
await SearchService.instance.getCollectionSearchResults(query);
2022-08-10 06:28:16 +00:00
for (CollectionWithThumbnail collectionResult in collectionResults) {
allResults.add(AlbumSearchResult(collectionResult));
}
final locationResults =
await SearchService.instance.getLocationSearchResults(query);
2022-08-10 06:28:16 +00:00
for (LocationSearchResult result in locationResults) {
allResults.add(result);
}
final fileResults =
2022-08-11 06:20:39 +00:00
await SearchService.instance.getFileSearchResults(query);
2022-08-10 06:28:16 +00:00
for (File file in fileResults) {
allResults.add(FileSearchResult(file));
}
return allResults;
2022-08-05 11:08:25 +00:00
}
}