2021-12-23 00:26:33 -05:00
|
|
|
import 'package:collection/collection.dart';
|
2021-11-10 21:29:15 -05:00
|
|
|
import 'package:flutter/material.dart';
|
2022-01-05 14:33:01 -05:00
|
|
|
import 'package:flutter_mobx/flutter_mobx.dart';
|
2022-01-25 16:38:43 -05:00
|
|
|
import 'package:frosty/api/twitch_api.dart';
|
|
|
|
|
import 'package:frosty/core/auth/auth_store.dart';
|
2022-01-04 02:58:02 -05:00
|
|
|
import 'package:frosty/screens/home/search/search_results_categories.dart';
|
|
|
|
|
import 'package:frosty/screens/home/search/search_results_channels.dart';
|
2022-01-07 21:56:45 -05:00
|
|
|
import 'package:frosty/screens/home/stores/search_store.dart';
|
2022-06-28 10:32:16 -04:00
|
|
|
import 'package:frosty/widgets/alert_message.dart';
|
2022-01-02 19:12:39 -05:00
|
|
|
import 'package:frosty/widgets/section_header.dart';
|
2022-01-25 16:38:43 -05:00
|
|
|
import 'package:provider/provider.dart';
|
2021-11-10 21:29:15 -05:00
|
|
|
|
2022-06-29 06:48:52 -04:00
|
|
|
/// The search section that contians search history and search results for channels and categories.
|
2021-11-10 21:29:15 -05:00
|
|
|
class Search extends StatefulWidget {
|
2022-06-29 06:48:52 -04:00
|
|
|
// The scroll controller for handling scroll to top functionality.
|
|
|
|
|
final ScrollController scrollController;
|
|
|
|
|
|
|
|
|
|
const Search({
|
|
|
|
|
Key? key,
|
|
|
|
|
required this.scrollController,
|
|
|
|
|
}) : super(key: key);
|
2021-11-10 21:29:15 -05:00
|
|
|
|
|
|
|
|
@override
|
2022-05-12 04:58:03 -04:00
|
|
|
State<Search> createState() => _SearchState();
|
2021-11-10 21:29:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _SearchState extends State<Search> {
|
2022-01-25 16:38:43 -05:00
|
|
|
late final _searchStore = SearchStore(
|
|
|
|
|
authStore: context.read<AuthStore>(),
|
|
|
|
|
twitchApi: context.read<TwitchApi>(),
|
|
|
|
|
);
|
|
|
|
|
|
2022-01-25 15:43:26 -05:00
|
|
|
final _textEditingController = TextEditingController();
|
2022-01-05 09:38:05 -05:00
|
|
|
|
2021-11-10 21:29:15 -05:00
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
2022-01-02 19:12:39 -05:00
|
|
|
const headerPadding = EdgeInsets.fromLTRB(15.0, 20.0, 15.0, 10.0);
|
|
|
|
|
|
2021-11-14 04:21:39 -05:00
|
|
|
return Column(
|
|
|
|
|
children: [
|
|
|
|
|
Padding(
|
2021-11-21 03:02:44 -05:00
|
|
|
padding: const EdgeInsets.symmetric(vertical: 5.0, horizontal: 10.0),
|
2021-11-14 04:21:39 -05:00
|
|
|
child: TextField(
|
2022-01-25 15:43:26 -05:00
|
|
|
controller: _textEditingController,
|
2022-01-01 01:21:33 -05:00
|
|
|
autocorrect: false,
|
2021-12-06 02:31:30 -05:00
|
|
|
decoration: InputDecoration(
|
2022-06-29 19:30:24 -04:00
|
|
|
contentPadding: const EdgeInsets.all(0.0),
|
|
|
|
|
hintText: 'Find a channel or category',
|
|
|
|
|
prefixIcon: const Icon(Icons.search),
|
2021-12-28 19:12:55 -05:00
|
|
|
suffixIcon: IconButton(
|
2022-06-29 19:30:24 -04:00
|
|
|
icon: const Icon(Icons.clear),
|
2022-06-29 05:49:39 -04:00
|
|
|
tooltip: 'Clear search',
|
2022-01-05 09:38:05 -05:00
|
|
|
onPressed: () {
|
2022-01-19 17:36:31 -05:00
|
|
|
FocusScope.of(context).unfocus();
|
2022-01-05 09:38:05 -05:00
|
|
|
setState(() {
|
2022-01-25 15:43:26 -05:00
|
|
|
_textEditingController.clear();
|
2022-01-05 09:38:05 -05:00
|
|
|
});
|
|
|
|
|
},
|
2021-12-28 19:12:55 -05:00
|
|
|
),
|
2021-11-14 04:21:39 -05:00
|
|
|
),
|
2022-01-25 16:38:43 -05:00
|
|
|
onSubmitted: _searchStore.handleQuery,
|
2021-11-12 01:00:55 -05:00
|
|
|
),
|
2021-11-10 21:29:15 -05:00
|
|
|
),
|
2021-11-14 04:21:39 -05:00
|
|
|
Expanded(
|
2022-01-19 15:37:20 -05:00
|
|
|
child: Observer(
|
|
|
|
|
builder: (context) {
|
2022-01-25 16:38:43 -05:00
|
|
|
if (_textEditingController.text.isEmpty || _searchStore.channelFuture == null || _searchStore.categoryFuture == null) {
|
2022-06-28 10:32:16 -04:00
|
|
|
if (_searchStore.searchHistory.isEmpty) {
|
|
|
|
|
return const AlertMessage(
|
|
|
|
|
message: 'No recent searches',
|
|
|
|
|
icon: Icons.search_off,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-19 15:37:20 -05:00
|
|
|
return Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
2022-06-28 10:32:16 -04:00
|
|
|
const SectionHeader(
|
|
|
|
|
'History',
|
|
|
|
|
padding: headerPadding,
|
|
|
|
|
),
|
2022-01-19 15:37:20 -05:00
|
|
|
Expanded(
|
|
|
|
|
child: ListView(
|
2022-06-29 06:48:52 -04:00
|
|
|
controller: widget.scrollController,
|
2022-01-25 16:38:43 -05:00
|
|
|
children: _searchStore.searchHistory
|
2022-01-19 15:37:20 -05:00
|
|
|
.mapIndexed(
|
|
|
|
|
(index, searchTerm) => ListTile(
|
|
|
|
|
leading: const Icon(Icons.history),
|
|
|
|
|
title: Text(searchTerm),
|
|
|
|
|
trailing: IconButton(
|
|
|
|
|
tooltip: 'Remove',
|
|
|
|
|
icon: const Icon(Icons.cancel),
|
|
|
|
|
onPressed: () => setState(() {
|
2022-01-25 16:38:43 -05:00
|
|
|
_searchStore.searchHistory.removeAt(index);
|
2022-01-19 15:37:20 -05:00
|
|
|
}),
|
|
|
|
|
),
|
|
|
|
|
onTap: () {
|
|
|
|
|
setState(() {
|
2022-01-25 15:43:26 -05:00
|
|
|
_textEditingController.text = searchTerm;
|
2022-01-25 16:38:43 -05:00
|
|
|
_searchStore.handleQuery(searchTerm);
|
2022-01-19 15:37:20 -05:00
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.toList(),
|
2022-01-19 15:12:54 -05:00
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
);
|
2022-01-19 15:37:20 -05:00
|
|
|
}
|
|
|
|
|
return CustomScrollView(
|
2022-06-29 06:48:52 -04:00
|
|
|
controller: widget.scrollController,
|
2022-01-19 15:37:20 -05:00
|
|
|
slivers: [
|
|
|
|
|
const SliverToBoxAdapter(
|
|
|
|
|
child: SectionHeader(
|
|
|
|
|
'Channels',
|
|
|
|
|
padding: headerPadding,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
SearchResultsChannels(
|
2022-01-25 16:38:43 -05:00
|
|
|
searchStore: _searchStore,
|
2022-01-25 15:43:26 -05:00
|
|
|
query: _textEditingController.text,
|
2022-01-19 15:37:20 -05:00
|
|
|
),
|
|
|
|
|
const SliverToBoxAdapter(
|
|
|
|
|
child: SectionHeader(
|
|
|
|
|
'Categories',
|
|
|
|
|
padding: headerPadding,
|
|
|
|
|
),
|
|
|
|
|
),
|
2022-01-25 16:38:43 -05:00
|
|
|
SearchResultsCategories(searchStore: _searchStore),
|
2022-01-19 15:37:20 -05:00
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
},
|
2022-01-19 15:12:54 -05:00
|
|
|
),
|
2021-11-14 04:21:39 -05:00
|
|
|
),
|
|
|
|
|
],
|
2021-11-10 21:29:15 -05:00
|
|
|
);
|
|
|
|
|
}
|
2021-11-14 04:21:39 -05:00
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void dispose() {
|
2022-01-25 15:43:26 -05:00
|
|
|
_textEditingController.dispose();
|
2021-11-14 04:21:39 -05:00
|
|
|
super.dispose();
|
|
|
|
|
}
|
2021-11-10 21:29:15 -05:00
|
|
|
}
|