Add: search by category
This commit is contained in:
@@ -8,6 +8,7 @@ import 'package:gymlink_module_web/pages/basket.dart';
|
||||
import 'package:gymlink_module_web/pages/detail.dart';
|
||||
import 'package:gymlink_module_web/pages/order_history.dart';
|
||||
import 'package:gymlink_module_web/providers/cart.dart';
|
||||
import 'package:gymlink_module_web/providers/main.dart';
|
||||
import 'package:gymlink_module_web/tools/items.dart';
|
||||
import 'package:gymlink_module_web/tools/prefs.dart';
|
||||
import 'package:gymlink_module_web/tools/relative.dart';
|
||||
@@ -69,7 +70,9 @@ class _MainPageState extends State<MainPage> {
|
||||
int cartLength = 0;
|
||||
int itemViewCount = 0;
|
||||
bool isLoading = false;
|
||||
bool isSearching = false;
|
||||
List<GymCategory> categories = [];
|
||||
GymCategory? selectedCategory;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -79,17 +82,9 @@ class _MainPageState extends State<MainPage> {
|
||||
cartLength = value.length;
|
||||
});
|
||||
});
|
||||
getItems(context).then((value) => setState(() {
|
||||
filteredData = value;
|
||||
itemViewCount = min(5, value.length);
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
for (var element in filteredData.sublist(0, itemViewCount)) {
|
||||
precacheImage(NetworkImage(element.images[0].url), context);
|
||||
}
|
||||
});
|
||||
}));
|
||||
getCategories(context).then((value) => setState(() {
|
||||
categories = value;
|
||||
_onSearch();
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -106,18 +101,30 @@ class _MainPageState extends State<MainPage> {
|
||||
}
|
||||
}
|
||||
|
||||
void _onSearch() async {
|
||||
final data = await getItems(context, searchText: searchText);
|
||||
void _searchItems({String searchText = '', String categoryId = ''}) async {
|
||||
setState(() {
|
||||
isSearching = true;
|
||||
});
|
||||
final data =
|
||||
await getItems(context, searchText: searchText, categoryId: categoryId);
|
||||
setState(() {
|
||||
filteredData = data;
|
||||
itemViewCount =
|
||||
min(filteredData.length, searchText == '' ? 5 : itemViewCount);
|
||||
itemViewCount = min(filteredData.length, 5);
|
||||
debugPrint(itemViewCount.toString());
|
||||
});
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
for (var element in filteredData.sublist(0, itemViewCount)) {
|
||||
precacheImage(NetworkImage(element.images[0].url), context);
|
||||
}
|
||||
});
|
||||
setState(() {
|
||||
isSearching = false;
|
||||
});
|
||||
}
|
||||
|
||||
void _onSearch() async {
|
||||
final categoryId = selectedCategory == null ? '' : selectedCategory!.id;
|
||||
_searchItems(searchText: searchText, categoryId: categoryId);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -136,7 +143,7 @@ class _MainPageState extends State<MainPage> {
|
||||
Expanded(
|
||||
child: TextField(
|
||||
onChanged: (value) => setState(() {
|
||||
searchText = value;
|
||||
searchText = value.trim().toLowerCase();
|
||||
if (searchText == '') {
|
||||
_onSearch();
|
||||
}
|
||||
@@ -203,6 +210,43 @@ class _MainPageState extends State<MainPage> {
|
||||
],
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
height: 60,
|
||||
child: ListView.builder(
|
||||
scrollDirection: Axis.horizontal,
|
||||
shrinkWrap: true,
|
||||
itemCount: categories.length,
|
||||
itemBuilder: (context, index) {
|
||||
final category = categories[index];
|
||||
return GestureDetector(
|
||||
onTap: () {
|
||||
setState(() {
|
||||
selectedCategory =
|
||||
selectedCategory == category ? null : category;
|
||||
});
|
||||
_onSearch();
|
||||
},
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 10, vertical: 10),
|
||||
child: Chip(
|
||||
label: Text(category.name),
|
||||
//FIXME: проблема с цветом
|
||||
backgroundColor: selectedCategory == category
|
||||
? context
|
||||
.read<GymLinkProvider>()
|
||||
.theme
|
||||
.primaryColorLight
|
||||
: Colors.white,
|
||||
labelStyle: TextStyle(
|
||||
color: selectedCategory == category
|
||||
? Colors.white
|
||||
: Colors.black),
|
||||
),
|
||||
),
|
||||
);
|
||||
}),
|
||||
),
|
||||
Expanded(
|
||||
child: LazyLoadScrollView(
|
||||
onEndOfPage: _onLoad,
|
||||
@@ -210,9 +254,9 @@ class _MainPageState extends State<MainPage> {
|
||||
child: Scrollbar(
|
||||
child: ListView(
|
||||
children: [
|
||||
filteredData.isEmpty && searchText != ''
|
||||
filteredData.isEmpty && searchText != '' && !isSearching
|
||||
? const Center(child: Text('Ничего не найдено'))
|
||||
: filteredData.isEmpty
|
||||
: isSearching
|
||||
? const Center(child: CircularProgressIndicator())
|
||||
: GridView.builder(
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
@@ -247,7 +291,7 @@ class _MainPageState extends State<MainPage> {
|
||||
);
|
||||
},
|
||||
),
|
||||
itemViewCount > 0
|
||||
itemViewCount > 0 && !isSearching
|
||||
? Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 10),
|
||||
child: Center(
|
||||
|
||||
Reference in New Issue
Block a user