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'; import 'package:photos/ui/viewer/search/search_results_suggestions.dart';
class SearchIconWidget extends StatefulWidget { class SearchIconWidget extends StatefulWidget {
final String searchQuery = '';
const SearchIconWidget({Key key}) : super(key: key); const SearchIconWidget({Key key}) : super(key: key);
@override @override
@ -14,6 +15,7 @@ class SearchIconWidget extends StatefulWidget {
} }
class _SearchIconWidgetState extends State<SearchIconWidget> { class _SearchIconWidgetState extends State<SearchIconWidget> {
final ValueNotifier<String> _searchQuery = ValueNotifier('');
bool showSearchWidget; bool showSearchWidget;
@override @override
void initState() { void initState() {
@ -23,8 +25,11 @@ class _SearchIconWidgetState extends State<SearchIconWidget> {
@override @override
Widget build(BuildContext context) { 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 return showSearchWidget
? Searchwidget(showSearchWidget) ? searchWidget(matchedCollections, matchedFiles)
: IconButton( : IconButton(
onPressed: () { onPressed: () {
setState( setState(
@ -36,86 +41,71 @@ class _SearchIconWidgetState extends State<SearchIconWidget> {
icon: const Icon(Icons.search), icon: const Icon(Icons.search),
); );
} }
}
// ignore: must_be_immutable Widget searchWidget(
class Searchwidget extends StatefulWidget { List<CollectionWithThumbnail> matchedCollections,
bool openSearch; List<File> matchedFiles,
final String searchQuery = ''; ) {
Searchwidget(this.openSearch, {Key key}) : super(key: key); return Column(
@override children: [
State<Searchwidget> createState() => _SearchwidgetState(); Row(
} children: [
const SizedBox(width: 12),
class _SearchwidgetState extends State<Searchwidget> { Flexible(
final ValueNotifier<String> _searchQ = ValueNotifier(''); child: Container(
@override color: Theme.of(context).colorScheme.defaultBackgroundColor,
Widget build(BuildContext context) { child: TextFormField(
List<CollectionWithThumbnail> matchedCollections; style: Theme.of(context).textTheme.subtitle1,
List<File> matchedFiles; decoration: InputDecoration(
return widget.openSearch filled: true,
? Column( contentPadding: const EdgeInsets.symmetric(
children: [ horizontal: 16,
Row( vertical: 14,
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,
),
border: UnderlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.circular(8),
),
prefixIcon: const Icon(Icons.search),
),
onChanged: (value) async {
matchedCollections = await CollectionsService.instance
.getFilteredCollectionsWithThumbnail(value);
matchedFiles = await FilesDB.instance
.getFilesOnFileNameSearch(value);
_searchQ.value = value;
},
autofocus: true,
),
), ),
border: UnderlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.circular(8),
),
prefixIcon: const Icon(Icons.search),
), ),
IconButton( onChanged: (value) async {
onPressed: () { matchedCollections = await CollectionsService.instance
setState(() { .getFilteredCollectionsWithThumbnail(value);
widget.openSearch = !widget.openSearch; matchedFiles =
}); await FilesDB.instance.getFilesOnFileNameSearch(value);
}, _searchQuery.value = value;
icon: const Icon(Icons.close), },
), autofocus: true,
], ),
), ),
const SizedBox(height: 20), ),
ValueListenableBuilder( IconButton(
valueListenable: _searchQ, onPressed: () {
builder: ( setState(() {
BuildContext context, showSearchWidget = !showSearchWidget;
String newQuery, });
Widget child, },
) { icon: const Icon(Icons.close),
return newQuery != '' ),
? SearchResultsSuggestions( ],
collectionsWithThumbnail: matchedCollections, ),
matchedFiles: matchedFiles, const SizedBox(height: 20),
) ValueListenableBuilder(
: const SizedBox.shrink(); valueListenable: _searchQuery,
}, builder: (
), BuildContext context,
], String newQuery,
) Widget child,
: const SearchIconWidget(); ) {
return newQuery != ''
? SearchResultsSuggestions(
collectionsWithThumbnail: matchedCollections,
matchedFiles: matchedFiles,
)
: const SizedBox.shrink();
},
),
],
);
} }
} }