Add: category on detail page and searching by API
This commit is contained in:
@@ -9,6 +9,7 @@ import 'package:gymlink_module_web/interfaces/items.dart';
|
||||
import 'package:gymlink_module_web/pages/basket.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/routes.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
@@ -29,6 +30,7 @@ class _DetailPageState extends State<DetailPage> {
|
||||
bool isInCart = false;
|
||||
int quantity = 0;
|
||||
GymItem? item;
|
||||
String categoryName = '';
|
||||
final CarouselController _carouselController = CarouselController();
|
||||
int _currentImage = 0;
|
||||
|
||||
@@ -47,21 +49,33 @@ class _DetailPageState extends State<DetailPage> {
|
||||
super.initState();
|
||||
}
|
||||
|
||||
void _getItem() async {
|
||||
Future<void> _getItem() async {
|
||||
final Uri url =
|
||||
Uri.http('gymlink.freemyip.com:8080', 'api/product/get/${widget.id}');
|
||||
final response = await http.get(url, headers: {
|
||||
'Authorization': 'Bearer ${context.read<GymLinkProvider>().token}',
|
||||
});
|
||||
if (response.statusCode == 200) {
|
||||
final data =
|
||||
GymItem.fromJson(jsonDecode(utf8.decode(response.bodyBytes)));
|
||||
setState(() {
|
||||
item = GymItem.fromJson(jsonDecode(utf8.decode(response.bodyBytes)));
|
||||
item = data;
|
||||
});
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
for (var element in item!.images) {
|
||||
precacheImage(NetworkImage(element.url), context);
|
||||
}
|
||||
});
|
||||
if (mounted) {
|
||||
getCategories(context).then((value) {
|
||||
categoryName = value
|
||||
.firstWhere(
|
||||
(element) => element.id == (item!.categoryId),
|
||||
orElse: () => GymCategory(id: item!.categoryId, name: ''),
|
||||
)
|
||||
.name;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -249,6 +263,12 @@ class _DetailPageState extends State<DetailPage> {
|
||||
: Image.network(item!.images[0].url,
|
||||
width: min(
|
||||
550, MediaQuery.sizeOf(context).width)),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 10),
|
||||
child: Center(
|
||||
child: Text(
|
||||
'Категория: ${categoryName == "" ? "Без категории" : categoryName}'),
|
||||
)),
|
||||
item!.description != ''
|
||||
? Padding(
|
||||
padding: const EdgeInsetsDirectional.all(30),
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user