2022-02-27 20:45:12 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
|
|
import 'package:immich_mobile/modules/search/providers/search_page_state.provider.dart';
|
|
|
|
|
|
|
|
class SearchBar extends HookConsumerWidget with PreferredSizeWidget {
|
2022-06-25 22:12:47 +02:00
|
|
|
SearchBar(
|
|
|
|
{Key? key, required this.searchFocusNode, required this.onSubmitted})
|
|
|
|
: super(key: key);
|
2022-03-03 00:44:24 +02:00
|
|
|
|
|
|
|
final FocusNode searchFocusNode;
|
|
|
|
final Function(String) onSubmitted;
|
2022-02-27 20:45:12 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
|
|
|
final searchTermController = useTextEditingController(text: "");
|
|
|
|
final isSearchEnabled = ref.watch(searchPageStateProvider).isSearchEnabled;
|
|
|
|
|
|
|
|
return AppBar(
|
|
|
|
automaticallyImplyLeading: false,
|
|
|
|
leading: isSearchEnabled
|
|
|
|
? IconButton(
|
|
|
|
onPressed: () {
|
|
|
|
searchFocusNode.unfocus();
|
|
|
|
ref.watch(searchPageStateProvider.notifier).disableSearch();
|
2022-03-03 00:44:24 +02:00
|
|
|
searchTermController.clear();
|
2022-02-27 20:45:12 +02:00
|
|
|
},
|
|
|
|
icon: const Icon(Icons.arrow_back_ios_rounded))
|
|
|
|
: const Icon(Icons.search_rounded),
|
|
|
|
title: TextField(
|
|
|
|
controller: searchTermController,
|
|
|
|
focusNode: searchFocusNode,
|
|
|
|
autofocus: false,
|
|
|
|
onTap: () {
|
2022-03-03 00:44:24 +02:00
|
|
|
searchTermController.clear();
|
2022-02-27 20:43:29 +02:00
|
|
|
ref.watch(searchPageStateProvider.notifier).getSuggestedSearchTerms();
|
2022-02-27 20:45:12 +02:00
|
|
|
ref.watch(searchPageStateProvider.notifier).enableSearch();
|
2022-03-03 00:44:24 +02:00
|
|
|
ref.watch(searchPageStateProvider.notifier).setSearchTerm("");
|
|
|
|
|
2022-02-27 20:45:12 +02:00
|
|
|
searchFocusNode.requestFocus();
|
|
|
|
},
|
|
|
|
onSubmitted: (searchTerm) {
|
2022-03-03 00:44:24 +02:00
|
|
|
onSubmitted(searchTerm);
|
|
|
|
searchTermController.clear();
|
|
|
|
ref.watch(searchPageStateProvider.notifier).setSearchTerm("");
|
2022-02-27 20:45:12 +02:00
|
|
|
},
|
|
|
|
onChanged: (value) {
|
|
|
|
ref.watch(searchPageStateProvider.notifier).setSearchTerm(value);
|
|
|
|
},
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
hintText: 'Search your photos',
|
|
|
|
enabledBorder: UnderlineInputBorder(
|
|
|
|
borderSide: BorderSide(color: Colors.transparent),
|
|
|
|
),
|
|
|
|
focusedBorder: UnderlineInputBorder(
|
|
|
|
borderSide: BorderSide(color: Colors.transparent),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Size get preferredSize => const Size.fromHeight(kToolbarHeight);
|
|
|
|
}
|