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

112 lines
3.5 KiB
Dart
Raw Normal View History

2022-07-18 04:45:06 +00:00
import 'package:flutter/material.dart';
import 'package:photos/db/files_db.dart';
2022-07-18 04:45:06 +00:00
import 'package:photos/ente_theme_data.dart';
import 'package:photos/models/collection_items.dart';
import 'package:photos/models/file.dart';
import 'package:photos/services/collections_service.dart';
import 'package:photos/ui/viewer/search/search_results_suggestions.dart';
2022-07-18 04:45:06 +00:00
class SearchIconWidget extends StatefulWidget {
2022-07-28 07:52:20 +00:00
final String searchQuery = '';
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 07:52:20 +00:00
final ValueNotifier<String> _searchQuery = ValueNotifier('');
2022-07-28 05:08:20 +00:00
bool showSearchWidget;
@override
void initState() {
super.initState();
showSearchWidget = false;
}
2022-07-18 04:45:06 +00:00
@override
Widget build(BuildContext context) {
2022-07-28 08:07:41 +00:00
List<CollectionWithThumbnail> matchedCollections = [];
List<File> matchedFiles = [];
2022-07-28 07:52:20 +00:00
//when false - show the search icon, when true - show the textfield for search
2022-07-28 05:08:20 +00:00
return showSearchWidget
2022-07-28 07:52:20 +00:00
? searchWidget(matchedCollections, matchedFiles)
2022-07-18 04:45:06 +00:00
: IconButton(
onPressed: () {
setState(
() {
2022-07-28 05:08:20 +00:00
showSearchWidget = !showSearchWidget;
2022-07-18 04:45:06 +00:00
},
);
},
icon: const Icon(Icons.search),
);
}
2022-07-28 07:52:20 +00:00
Widget searchWidget(
List<CollectionWithThumbnail> matchedCollections,
List<File> matchedFiles,
) {
return Column(
children: [
Row(
children: [
const SizedBox(width: 12),
Flexible(
child: Container(
color: Theme.of(context).colorScheme.defaultBackgroundColor,
child: TextFormField(
style: Theme.of(context).textTheme.subtitle1,
decoration: InputDecoration(
filled: true,
contentPadding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 14,
2022-07-18 04:45:06 +00:00
),
2022-07-28 07:52:20 +00:00
border: UnderlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.circular(8),
),
prefixIcon: const Icon(Icons.search),
),
2022-07-28 07:52:20 +00:00
onChanged: (value) async {
matchedCollections = await CollectionsService.instance
.getFilteredCollectionsWithThumbnail(value);
matchedFiles =
await FilesDB.instance.getFilesOnFileNameSearch(value);
_searchQuery.value = value;
},
autofocus: true,
),
),
2022-07-28 07:52:20 +00:00
),
IconButton(
onPressed: () {
setState(() {
showSearchWidget = !showSearchWidget;
});
},
icon: const Icon(Icons.close),
),
],
),
const SizedBox(height: 20),
ValueListenableBuilder(
valueListenable: _searchQuery,
builder: (
BuildContext context,
String newQuery,
Widget child,
) {
return newQuery != ''
? SearchResultsSuggestions(
collectionsWithThumbnail: matchedCollections,
matchedFiles: matchedFiles,
)
: const SizedBox.shrink();
},
),
],
);
2022-07-18 04:45:06 +00:00
}
}