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-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
|
|
|
|
|
|
|
|
class Search extends StatefulWidget {
|
2022-01-25 16:38:43 -05:00
|
|
|
const Search({Key? key}) : super(key: key);
|
2021-11-10 21:29:15 -05:00
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
_SearchState createState() => _SearchState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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(
|
2021-11-14 04:21:39 -05:00
|
|
|
isDense: true,
|
2022-04-06 00:40:11 -04:00
|
|
|
labelText: 'Find a channel or category',
|
2021-12-06 02:31:30 -05:00
|
|
|
contentPadding: const EdgeInsets.all(10.0),
|
|
|
|
|
border: const OutlineInputBorder(
|
2022-01-16 23:48:34 -05:00
|
|
|
borderRadius: BorderRadius.all(Radius.circular(15.0)),
|
2021-11-14 04:21:39 -05:00
|
|
|
),
|
2021-12-28 19:12:55 -05:00
|
|
|
suffixIcon: IconButton(
|
2022-01-06 04:31:52 -05: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
|
|
|
icon: const Icon(Icons.clear),
|
|
|
|
|
),
|
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-01-19 15:37:20 -05:00
|
|
|
return Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
2022-01-25 16:38:43 -05:00
|
|
|
if (_searchStore.searchHistory.isNotEmpty)
|
2022-03-30 19:29:51 -04:00
|
|
|
const SectionHeader(
|
|
|
|
|
'History',
|
|
|
|
|
padding: headerPadding,
|
|
|
|
|
fontWeight: FontWeight.bold,
|
2022-01-19 15:12:54 -05:00
|
|
|
),
|
2022-01-19 15:37:20 -05:00
|
|
|
Expanded(
|
|
|
|
|
child: ListView(
|
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(
|
|
|
|
|
slivers: [
|
|
|
|
|
const SliverToBoxAdapter(
|
|
|
|
|
child: SectionHeader(
|
|
|
|
|
'Channels',
|
|
|
|
|
padding: headerPadding,
|
2022-03-30 19:29:51 -04:00
|
|
|
fontWeight: FontWeight.bold,
|
2022-01-19 15:37:20 -05:00
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
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-03-30 19:29:51 -04:00
|
|
|
fontWeight: FontWeight.bold,
|
2022-01-19 15:37:20 -05:00
|
|
|
),
|
|
|
|
|
),
|
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
|
|
|
}
|