Compare commits
2 Commits
c8965dab4e
...
eaa8b138a4
| Author | SHA1 | Date | |
|---|---|---|---|
| eaa8b138a4 | |||
| db39169907 |
@@ -1,5 +1,27 @@
|
||||
import 'dart:convert';
|
||||
|
||||
class ItemsDataResponse {
|
||||
final List<GymItem> rows;
|
||||
|
||||
ItemsDataResponse({
|
||||
required this.rows,
|
||||
});
|
||||
|
||||
factory ItemsDataResponse.fromRawJson(String str) =>
|
||||
ItemsDataResponse.fromJson(json.decode(str));
|
||||
|
||||
String toRawJson() => json.encode(toJson());
|
||||
|
||||
factory ItemsDataResponse.fromJson(Map<String, dynamic> json) =>
|
||||
ItemsDataResponse(
|
||||
rows: List<GymItem>.from(json["rows"].map((x) => GymItem.fromJson(x))),
|
||||
);
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
"rows": List<dynamic>.from(rows.map((x) => x.toJson())),
|
||||
};
|
||||
}
|
||||
|
||||
class GymItem {
|
||||
final String id;
|
||||
final String externalId;
|
||||
@@ -44,7 +66,7 @@ class GymItem {
|
||||
"title": title,
|
||||
"description": description,
|
||||
"count": count,
|
||||
"price": price * 100,
|
||||
"price": price,
|
||||
"categoryId": categoryId,
|
||||
"images": List<dynamic>.from(images.map((x) => x.toJson())),
|
||||
};
|
||||
@@ -78,3 +100,51 @@ class GymImage {
|
||||
"url": url,
|
||||
};
|
||||
}
|
||||
|
||||
class CategoryDataResponse {
|
||||
final List<GymCategory> rows;
|
||||
|
||||
CategoryDataResponse({
|
||||
required this.rows,
|
||||
});
|
||||
|
||||
factory CategoryDataResponse.fromRawJson(String str) =>
|
||||
CategoryDataResponse.fromJson(json.decode(str));
|
||||
|
||||
String toRawJson() => json.encode(toJson());
|
||||
|
||||
factory CategoryDataResponse.fromJson(Map<String, dynamic> json) =>
|
||||
CategoryDataResponse(
|
||||
rows: List<GymCategory>.from(
|
||||
json["rows"].map((x) => GymCategory.fromJson(x))),
|
||||
);
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
"rows": List<dynamic>.from(rows.map((x) => x.toJson())),
|
||||
};
|
||||
}
|
||||
|
||||
class GymCategory {
|
||||
final String id;
|
||||
final String name;
|
||||
|
||||
GymCategory({
|
||||
required this.id,
|
||||
required this.name,
|
||||
});
|
||||
|
||||
factory GymCategory.fromRawJson(String str) =>
|
||||
GymCategory.fromJson(json.decode(str));
|
||||
|
||||
String toRawJson() => json.encode(toJson());
|
||||
|
||||
factory GymCategory.fromJson(Map<String, dynamic> json) => GymCategory(
|
||||
id: json["id"],
|
||||
name: json["name"],
|
||||
);
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
"id": id,
|
||||
"name": name,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -6,22 +6,30 @@ import 'package:gymlink_module_web/providers/main.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
Future<List<GymItem>> getItems(BuildContext context) async {
|
||||
Future<List<GymItem>> getItems(BuildContext context,
|
||||
{String searchText = '', String categoryId = ''}) async {
|
||||
final token = context.read<GymLinkProvider>().token;
|
||||
if (token != '') {
|
||||
final Uri url =
|
||||
Uri.http('gymlink.freemyip.com:8080', 'api/product/get-list');
|
||||
try {
|
||||
final response = await http.get(url, headers: {
|
||||
'Authorization': 'Bearer $token',
|
||||
});
|
||||
if (response.statusCode == 200) {
|
||||
final data =
|
||||
jsonDecode(utf8.decode(response.bodyBytes)) as List<dynamic>;
|
||||
final items = data.map((e) => GymItem.fromJson(e)).toList();
|
||||
final response = await http.post(url,
|
||||
headers: {
|
||||
'Authorization': 'Bearer $token',
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: jsonEncode({
|
||||
"search": searchText,
|
||||
"page": 0,
|
||||
"pageSize": 0,
|
||||
"filter": categoryId,
|
||||
"direction": 1
|
||||
}));
|
||||
if (response.statusCode == 201) {
|
||||
final items = ItemsDataResponse.fromRawJson(response.body).rows;
|
||||
return items;
|
||||
}
|
||||
throw Error();
|
||||
throw response.body;
|
||||
} catch (e) {
|
||||
debugPrint('error: $e');
|
||||
return await Future.delayed(
|
||||
@@ -64,3 +72,27 @@ Future<List<GymItem>> getItemsByIds(
|
||||
context.read<GymLinkProvider>().onError();
|
||||
return [];
|
||||
}
|
||||
|
||||
Future<List<GymCategory>> getCategories(BuildContext context) async {
|
||||
final token = context.read<GymLinkProvider>().token;
|
||||
if (token != '') {
|
||||
final Uri url = Uri.http(
|
||||
'gymlink.freemyip.com:8080', 'api/category/get-internal-categories');
|
||||
try {
|
||||
final response = await http.get(url, headers: {
|
||||
'Authorization': 'Bearer $token',
|
||||
});
|
||||
if (response.statusCode == 200) {
|
||||
final categories = CategoryDataResponse.fromRawJson(response.body).rows;
|
||||
return categories;
|
||||
}
|
||||
throw response.body;
|
||||
} catch (e) {
|
||||
debugPrint('error: $e');
|
||||
return await Future.delayed(
|
||||
const Duration(seconds: 5), () => getCategories(context));
|
||||
}
|
||||
}
|
||||
context.read<GymLinkProvider>().onError();
|
||||
return [];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user