Compare commits
2 Commits
c8965dab4e
...
eaa8b138a4
| Author | SHA1 | Date | |
|---|---|---|---|
| eaa8b138a4 | |||
| db39169907 |
@@ -1,5 +1,27 @@
|
|||||||
import 'dart:convert';
|
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 {
|
class GymItem {
|
||||||
final String id;
|
final String id;
|
||||||
final String externalId;
|
final String externalId;
|
||||||
@@ -44,7 +66,7 @@ class GymItem {
|
|||||||
"title": title,
|
"title": title,
|
||||||
"description": description,
|
"description": description,
|
||||||
"count": count,
|
"count": count,
|
||||||
"price": price * 100,
|
"price": price,
|
||||||
"categoryId": categoryId,
|
"categoryId": categoryId,
|
||||||
"images": List<dynamic>.from(images.map((x) => x.toJson())),
|
"images": List<dynamic>.from(images.map((x) => x.toJson())),
|
||||||
};
|
};
|
||||||
@@ -78,3 +100,51 @@ class GymImage {
|
|||||||
"url": url,
|
"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/pages/basket.dart';
|
||||||
import 'package:gymlink_module_web/providers/cart.dart';
|
import 'package:gymlink_module_web/providers/cart.dart';
|
||||||
import 'package:gymlink_module_web/providers/main.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/prefs.dart';
|
||||||
import 'package:gymlink_module_web/tools/routes.dart';
|
import 'package:gymlink_module_web/tools/routes.dart';
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
@@ -29,6 +30,7 @@ class _DetailPageState extends State<DetailPage> {
|
|||||||
bool isInCart = false;
|
bool isInCart = false;
|
||||||
int quantity = 0;
|
int quantity = 0;
|
||||||
GymItem? item;
|
GymItem? item;
|
||||||
|
String categoryName = '';
|
||||||
final CarouselController _carouselController = CarouselController();
|
final CarouselController _carouselController = CarouselController();
|
||||||
int _currentImage = 0;
|
int _currentImage = 0;
|
||||||
|
|
||||||
@@ -47,21 +49,33 @@ class _DetailPageState extends State<DetailPage> {
|
|||||||
super.initState();
|
super.initState();
|
||||||
}
|
}
|
||||||
|
|
||||||
void _getItem() async {
|
Future<void> _getItem() async {
|
||||||
final Uri url =
|
final Uri url =
|
||||||
Uri.http('gymlink.freemyip.com:8080', 'api/product/get/${widget.id}');
|
Uri.http('gymlink.freemyip.com:8080', 'api/product/get/${widget.id}');
|
||||||
final response = await http.get(url, headers: {
|
final response = await http.get(url, headers: {
|
||||||
'Authorization': 'Bearer ${context.read<GymLinkProvider>().token}',
|
'Authorization': 'Bearer ${context.read<GymLinkProvider>().token}',
|
||||||
});
|
});
|
||||||
if (response.statusCode == 200) {
|
if (response.statusCode == 200) {
|
||||||
|
final data =
|
||||||
|
GymItem.fromJson(jsonDecode(utf8.decode(response.bodyBytes)));
|
||||||
setState(() {
|
setState(() {
|
||||||
item = GymItem.fromJson(jsonDecode(utf8.decode(response.bodyBytes)));
|
item = data;
|
||||||
});
|
});
|
||||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
for (var element in item!.images) {
|
for (var element in item!.images) {
|
||||||
precacheImage(NetworkImage(element.url), context);
|
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,
|
: Image.network(item!.images[0].url,
|
||||||
width: min(
|
width: min(
|
||||||
550, MediaQuery.sizeOf(context).width)),
|
550, MediaQuery.sizeOf(context).width)),
|
||||||
|
Padding(
|
||||||
|
padding: const EdgeInsets.symmetric(vertical: 10),
|
||||||
|
child: Center(
|
||||||
|
child: Text(
|
||||||
|
'Категория: ${categoryName == "" ? "Без категории" : categoryName}'),
|
||||||
|
)),
|
||||||
item!.description != ''
|
item!.description != ''
|
||||||
? Padding(
|
? Padding(
|
||||||
padding: const EdgeInsetsDirectional.all(30),
|
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:gymlink_module_web/tools/routes.dart';
|
||||||
import 'package:lazy_load_scrollview/lazy_load_scrollview.dart';
|
import 'package:lazy_load_scrollview/lazy_load_scrollview.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
import 'package:url_launcher/url_launcher.dart';
|
|
||||||
|
|
||||||
const List<Map<String, String>> testData = [
|
const List<Map<String, String>> testData = [
|
||||||
{
|
{
|
||||||
@@ -67,10 +66,10 @@ class MainPage extends StatefulWidget {
|
|||||||
class _MainPageState extends State<MainPage> {
|
class _MainPageState extends State<MainPage> {
|
||||||
String searchText = '';
|
String searchText = '';
|
||||||
List<GymItem> filteredData = [];
|
List<GymItem> filteredData = [];
|
||||||
List<GymItem> items = [];
|
|
||||||
int cartLength = 0;
|
int cartLength = 0;
|
||||||
int itemViewCount = 0;
|
int itemViewCount = 0;
|
||||||
bool isLoading = false;
|
bool isLoading = false;
|
||||||
|
List<GymCategory> categories = [];
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
@@ -82,7 +81,6 @@ class _MainPageState extends State<MainPage> {
|
|||||||
});
|
});
|
||||||
getItems(context).then((value) => setState(() {
|
getItems(context).then((value) => setState(() {
|
||||||
filteredData = value;
|
filteredData = value;
|
||||||
items = value;
|
|
||||||
itemViewCount = min(5, value.length);
|
itemViewCount = min(5, value.length);
|
||||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
for (var element in filteredData.sublist(0, itemViewCount)) {
|
for (var element in filteredData.sublist(0, itemViewCount)) {
|
||||||
@@ -90,13 +88,9 @@ class _MainPageState extends State<MainPage> {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
}
|
getCategories(context).then((value) => setState(() {
|
||||||
|
categories = value;
|
||||||
Future<void> _goToPage() async {
|
}));
|
||||||
final Uri url = Uri.parse('https://google.com');
|
|
||||||
if (!await launchUrl(url, webOnlyWindowName: '_blank')) {
|
|
||||||
throw 'Could not launch $url';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void _onLoad() async {
|
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(() {
|
setState(() {
|
||||||
filteredData = items
|
filteredData = data;
|
||||||
.where((element) => (element.title).contains(searchText))
|
itemViewCount =
|
||||||
.toList();
|
min(filteredData.length, searchText == '' ? 5 : itemViewCount);
|
||||||
itemViewCount = min(filteredData.length, itemViewCount);
|
|
||||||
});
|
});
|
||||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
for (var element in filteredData.sublist(0, itemViewCount)) {
|
for (var element in filteredData.sublist(0, itemViewCount)) {
|
||||||
@@ -216,10 +210,10 @@ class _MainPageState extends State<MainPage> {
|
|||||||
child: Scrollbar(
|
child: Scrollbar(
|
||||||
child: ListView(
|
child: ListView(
|
||||||
children: [
|
children: [
|
||||||
items.isEmpty
|
filteredData.isEmpty && searchText != ''
|
||||||
? const Center(child: CircularProgressIndicator())
|
|
||||||
: filteredData.isEmpty
|
|
||||||
? const Center(child: Text('Ничего не найдено'))
|
? const Center(child: Text('Ничего не найдено'))
|
||||||
|
: filteredData.isEmpty
|
||||||
|
? const Center(child: CircularProgressIndicator())
|
||||||
: GridView.builder(
|
: GridView.builder(
|
||||||
physics: const NeverScrollableScrollPhysics(),
|
physics: const NeverScrollableScrollPhysics(),
|
||||||
shrinkWrap: true,
|
shrinkWrap: true,
|
||||||
|
|||||||
@@ -6,22 +6,30 @@ import 'package:gymlink_module_web/providers/main.dart';
|
|||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
import 'package:provider/provider.dart';
|
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;
|
final token = context.read<GymLinkProvider>().token;
|
||||||
if (token != '') {
|
if (token != '') {
|
||||||
final Uri url =
|
final Uri url =
|
||||||
Uri.http('gymlink.freemyip.com:8080', 'api/product/get-list');
|
Uri.http('gymlink.freemyip.com:8080', 'api/product/get-list');
|
||||||
try {
|
try {
|
||||||
final response = await http.get(url, headers: {
|
final response = await http.post(url,
|
||||||
|
headers: {
|
||||||
'Authorization': 'Bearer $token',
|
'Authorization': 'Bearer $token',
|
||||||
});
|
'Content-Type': 'application/json'
|
||||||
if (response.statusCode == 200) {
|
},
|
||||||
final data =
|
body: jsonEncode({
|
||||||
jsonDecode(utf8.decode(response.bodyBytes)) as List<dynamic>;
|
"search": searchText,
|
||||||
final items = data.map((e) => GymItem.fromJson(e)).toList();
|
"page": 0,
|
||||||
|
"pageSize": 0,
|
||||||
|
"filter": categoryId,
|
||||||
|
"direction": 1
|
||||||
|
}));
|
||||||
|
if (response.statusCode == 201) {
|
||||||
|
final items = ItemsDataResponse.fromRawJson(response.body).rows;
|
||||||
return items;
|
return items;
|
||||||
}
|
}
|
||||||
throw Error();
|
throw response.body;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
debugPrint('error: $e');
|
debugPrint('error: $e');
|
||||||
return await Future.delayed(
|
return await Future.delayed(
|
||||||
@@ -64,3 +72,27 @@ Future<List<GymItem>> getItemsByIds(
|
|||||||
context.read<GymLinkProvider>().onError();
|
context.read<GymLinkProvider>().onError();
|
||||||
return [];
|
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