Add: category on detail page and searching by API

This commit is contained in:
2024-06-04 15:29:41 +03:00
parent db39169907
commit eaa8b138a4
2 changed files with 34 additions and 20 deletions

View File

@@ -14,7 +14,6 @@ import 'package:gymlink_module_web/tools/relative.dart';
import 'package:gymlink_module_web/tools/routes.dart';
import 'package:lazy_load_scrollview/lazy_load_scrollview.dart';
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher.dart';
const List<Map<String, String>> testData = [
{
@@ -67,10 +66,10 @@ class MainPage extends StatefulWidget {
class _MainPageState extends State<MainPage> {
String searchText = '';
List<GymItem> filteredData = [];
List<GymItem> items = [];
int cartLength = 0;
int itemViewCount = 0;
bool isLoading = false;
List<GymCategory> categories = [];
@override
void initState() {
@@ -82,7 +81,6 @@ class _MainPageState extends State<MainPage> {
});
getItems(context).then((value) => setState(() {
filteredData = value;
items = value;
itemViewCount = min(5, value.length);
WidgetsBinding.instance.addPostFrameCallback((_) {
for (var element in filteredData.sublist(0, itemViewCount)) {
@@ -90,13 +88,9 @@ class _MainPageState extends State<MainPage> {
}
});
}));
}
Future<void> _goToPage() async {
final Uri url = Uri.parse('https://google.com');
if (!await launchUrl(url, webOnlyWindowName: '_blank')) {
throw 'Could not launch $url';
}
getCategories(context).then((value) => setState(() {
categories = value;
}));
}
void _onLoad() async {
@@ -112,12 +106,12 @@ class _MainPageState extends State<MainPage> {
}
}
void _onSearch() {
void _onSearch() async {
final data = await getItems(context, searchText: searchText);
setState(() {
filteredData = items
.where((element) => (element.title).contains(searchText))
.toList();
itemViewCount = min(filteredData.length, itemViewCount);
filteredData = data;
itemViewCount =
min(filteredData.length, searchText == '' ? 5 : itemViewCount);
});
WidgetsBinding.instance.addPostFrameCallback((_) {
for (var element in filteredData.sublist(0, itemViewCount)) {
@@ -216,10 +210,10 @@ class _MainPageState extends State<MainPage> {
child: Scrollbar(
child: ListView(
children: [
items.isEmpty
? const Center(child: CircularProgressIndicator())
filteredData.isEmpty && searchText != ''
? const Center(child: Text('Ничего не найдено'))
: filteredData.isEmpty
? const Center(child: Text('Ничего не найдено'))
? const Center(child: CircularProgressIndicator())
: GridView.builder(
physics: const NeverScrollableScrollPhysics(),
shrinkWrap: true,