code refactor

This commit is contained in:
ashilkn 2022-07-28 13:22:20 +05:30
parent 72f3c70804
commit b06fb763f0

View file

@ -7,6 +7,7 @@ import 'package:photos/services/collections_service.dart';
import 'package:photos/ui/viewer/search/search_results_suggestions.dart';
class SearchIconWidget extends StatefulWidget {
final String searchQuery = '';
const SearchIconWidget({Key key}) : super(key: key);
@override
@ -14,6 +15,7 @@ class SearchIconWidget extends StatefulWidget {
}
class _SearchIconWidgetState extends State<SearchIconWidget> {
final ValueNotifier<String> _searchQuery = ValueNotifier('');
bool showSearchWidget;
@override
void initState() {
@ -23,8 +25,11 @@ class _SearchIconWidgetState extends State<SearchIconWidget> {
@override
Widget build(BuildContext context) {
List<CollectionWithThumbnail> matchedCollections;
List<File> matchedFiles;
//when false - show the search icon, when true - show the textfield for search
return showSearchWidget
? Searchwidget(showSearchWidget)
? searchWidget(matchedCollections, matchedFiles)
: IconButton(
onPressed: () {
setState(
@ -36,33 +41,19 @@ class _SearchIconWidgetState extends State<SearchIconWidget> {
icon: const Icon(Icons.search),
);
}
}
// ignore: must_be_immutable
class Searchwidget extends StatefulWidget {
bool openSearch;
final String searchQuery = '';
Searchwidget(this.openSearch, {Key key}) : super(key: key);
@override
State<Searchwidget> createState() => _SearchwidgetState();
}
class _SearchwidgetState extends State<Searchwidget> {
final ValueNotifier<String> _searchQ = ValueNotifier('');
@override
Widget build(BuildContext context) {
List<CollectionWithThumbnail> matchedCollections;
List<File> matchedFiles;
return widget.openSearch
? Column(
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,
color: Theme.of(context).colorScheme.defaultBackgroundColor,
child: TextFormField(
style: Theme.of(context).textTheme.subtitle1,
decoration: InputDecoration(
@ -80,9 +71,9 @@ class _SearchwidgetState extends State<Searchwidget> {
onChanged: (value) async {
matchedCollections = await CollectionsService.instance
.getFilteredCollectionsWithThumbnail(value);
matchedFiles = await FilesDB.instance
.getFilesOnFileNameSearch(value);
_searchQ.value = value;
matchedFiles =
await FilesDB.instance.getFilesOnFileNameSearch(value);
_searchQuery.value = value;
},
autofocus: true,
),
@ -91,7 +82,7 @@ class _SearchwidgetState extends State<Searchwidget> {
IconButton(
onPressed: () {
setState(() {
widget.openSearch = !widget.openSearch;
showSearchWidget = !showSearchWidget;
});
},
icon: const Icon(Icons.close),
@ -100,7 +91,7 @@ class _SearchwidgetState extends State<Searchwidget> {
),
const SizedBox(height: 20),
ValueListenableBuilder(
valueListenable: _searchQ,
valueListenable: _searchQuery,
builder: (
BuildContext context,
String newQuery,
@ -115,7 +106,6 @@ class _SearchwidgetState extends State<Searchwidget> {
},
),
],
)
: const SearchIconWidget();
);
}
}