frosty/lib/screens/search/search.dart

122 lines
4.6 KiB
Dart
Raw Normal View History

2021-12-23 00:26:33 -05:00
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
2021-11-14 04:21:39 -05:00
import 'package:flutter_mobx/flutter_mobx.dart';
2022-01-02 19:12:39 -05:00
import 'package:frosty/screens/search/search_results_categories.dart';
import 'package:frosty/screens/search/search_results_channels.dart';
2021-11-21 23:29:47 -05:00
import 'package:frosty/screens/search/search_store.dart';
2022-01-02 19:12:39 -05:00
import 'package:frosty/widgets/section_header.dart';
class Search extends StatefulWidget {
2021-11-24 04:39:46 -05:00
final SearchStore searchStore;
2021-11-22 01:26:17 -05:00
const Search({Key? key, required this.searchStore}) : super(key: key);
@override
_SearchState createState() => _SearchState();
}
class _SearchState extends State<Search> {
@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-24 04:39:46 -05:00
final searchStore = widget.searchStore;
2021-11-14 04:21:39 -05:00
return Column(
children: [
Padding(
padding: const EdgeInsets.symmetric(vertical: 5.0, horizontal: 10.0),
2021-11-14 04:21:39 -05:00
child: TextField(
2021-11-24 04:39:46 -05:00
controller: searchStore.textController,
autocorrect: false,
2021-12-06 02:31:30 -05:00
decoration: InputDecoration(
2021-11-14 04:21:39 -05:00
isDense: true,
2022-01-02 19:12:39 -05:00
hintText: 'Search for a channel or category',
2021-12-06 02:31:30 -05:00
contentPadding: const EdgeInsets.all(10.0),
border: const OutlineInputBorder(
2021-11-14 20:52:29 -05:00
borderRadius: BorderRadius.all(Radius.circular(10.0)),
2021-11-14 04:21:39 -05:00
),
2021-12-28 19:12:55 -05:00
suffixIcon: IconButton(
onPressed: searchStore.clearSearch,
icon: const Icon(Icons.clear),
),
2021-11-14 04:21:39 -05:00
),
2021-11-24 04:39:46 -05:00
onSubmitted: searchStore.handleQuery,
),
),
2021-11-14 04:21:39 -05:00
Expanded(
child: RefreshIndicator(
2021-11-24 04:39:46 -05:00
onRefresh: () => searchStore.handleQuery(searchStore.textController.text),
child: GestureDetector(
onTap: () => FocusManager.instance.primaryFocus?.unfocus(),
child: Observer(
builder: (_) {
2021-12-23 00:26:33 -05:00
if (searchStore.textController.text.isEmpty) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (searchStore.searchHistory.isNotEmpty)
const Padding(
padding: EdgeInsets.all(15.0),
child: Text(
'HISTORY',
style: TextStyle(fontWeight: FontWeight.bold),
),
2021-12-23 00:26:33 -05:00
),
Expanded(
child: ListView(
children: searchStore.searchHistory
.mapIndexed((index, searchTerm) => ListTile(
leading: const Icon(Icons.history),
title: Text(searchTerm),
trailing: IconButton(
icon: const Icon(Icons.cancel),
onPressed: () => searchStore.searchHistory.removeAt(index),
),
onTap: () {
searchStore.textController.text = searchTerm;
searchStore.handleQuery(searchTerm);
},
))
.toList(),
),
),
],
);
}
2022-01-02 19:12:39 -05:00
return CustomScrollView(
slivers: [
const SliverToBoxAdapter(
child: SectionHeader(
'Channels',
padding: headerPadding,
2021-12-28 19:12:55 -05:00
),
),
2022-01-02 19:12:39 -05:00
SearchResultsChannels(searchStore: searchStore),
if (searchStore.categorySearchResults.isNotEmpty) ...[
const SliverToBoxAdapter(
child: SectionHeader(
'Categories',
padding: headerPadding,
),
),
SearchResultsCategories(categories: searchStore.categorySearchResults),
]
2021-12-28 19:12:55 -05:00
],
);
},
),
),
2021-11-14 04:21:39 -05:00
),
),
],
);
}
2021-11-14 04:21:39 -05:00
@override
void dispose() {
2021-11-24 04:39:46 -05:00
widget.searchStore.dispose();
2021-11-14 04:21:39 -05:00
super.dispose();
}
}