Add: getting products from API
This commit is contained in:
@@ -3,11 +3,12 @@ import 'dart:math';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:gymlink_module_web/components/app_bar.dart';
|
||||
import 'package:gymlink_module_web/components/item_card.dart';
|
||||
import 'package:gymlink_module_web/interfaces/items.dart';
|
||||
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';
|
||||
import 'package:gymlink_module_web/tools/routes.dart';
|
||||
@@ -65,22 +66,22 @@ class MainPage extends StatefulWidget {
|
||||
|
||||
class _MainPageState extends State<MainPage> {
|
||||
String searchText = '';
|
||||
List<Map<String, String>> filteredData = [];
|
||||
List<GymItem> filteredData = [];
|
||||
List<GymItem> items = [];
|
||||
int cartLength = 0;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
filteredData = testData;
|
||||
getCart().then((value) {
|
||||
setState(() {
|
||||
cartLength = value.length;
|
||||
});
|
||||
}).whenComplete(() {
|
||||
if (mounted) {
|
||||
setState(() {});
|
||||
}
|
||||
});
|
||||
getItems(context).then((value) => setState(() {
|
||||
filteredData = value;
|
||||
items = value;
|
||||
}));
|
||||
}
|
||||
|
||||
Future<void> _goToPage() async {
|
||||
@@ -97,8 +98,8 @@ class _MainPageState extends State<MainPage> {
|
||||
|
||||
void _onSearch() {
|
||||
setState(() {
|
||||
filteredData = testData
|
||||
.where((element) => (element['name']!).contains(searchText))
|
||||
filteredData = items
|
||||
.where((element) => (element.title).contains(searchText))
|
||||
.toList();
|
||||
});
|
||||
}
|
||||
@@ -106,7 +107,6 @@ class _MainPageState extends State<MainPage> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final cartL = context.watch<CartProvider>().cartLength;
|
||||
final onError = context.read<GymLinkProvider>().onError;
|
||||
return Scaffold(
|
||||
appBar: const GymLinkAppBar(),
|
||||
body: Column(
|
||||
@@ -160,7 +160,6 @@ class _MainPageState extends State<MainPage> {
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
onError();
|
||||
Navigator.of(context).push(CustomPageRoute(
|
||||
builder: (context) => const HistoryPage(),
|
||||
));
|
||||
@@ -193,40 +192,48 @@ class _MainPageState extends State<MainPage> {
|
||||
onEndOfPage: _onLoad,
|
||||
child: Stack(
|
||||
children: [
|
||||
GridView.builder(
|
||||
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: min(
|
||||
(MediaQuery.sizeOf(context).width ~/ 200).toInt(), 8),
|
||||
),
|
||||
itemCount: filteredData.length,
|
||||
itemBuilder: (context, index) {
|
||||
final product = filteredData[index];
|
||||
return ProductCard(
|
||||
imagePath: Image(
|
||||
image: Image.network(
|
||||
'https://rus-sport.net/upload/iblock/311/topb85ez18pq0aavohpa5zipk2sbfxll.jpg')
|
||||
.image,
|
||||
width: 50,
|
||||
),
|
||||
name: product['name']!,
|
||||
price: product['price']!,
|
||||
onTap: () => Navigator.of(context).push(
|
||||
CustomPageRoute(
|
||||
builder: (context) => DetailPage(
|
||||
name: product['name']!,
|
||||
description: product['details']!,
|
||||
price: product['price']!,
|
||||
id: product['id']!,
|
||||
image: Image(
|
||||
image:
|
||||
AssetImage('assets/${product['image']!}'),
|
||||
width: 300),
|
||||
items.isEmpty
|
||||
? const Center(child: CircularProgressIndicator())
|
||||
: filteredData.isEmpty
|
||||
? const Center(child: Text('Ничего не найдено'))
|
||||
: GridView.builder(
|
||||
gridDelegate:
|
||||
SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: min(
|
||||
(MediaQuery.sizeOf(context).width ~/ 200)
|
||||
.toInt(),
|
||||
8),
|
||||
),
|
||||
itemCount: filteredData.length,
|
||||
itemBuilder: (context, index) {
|
||||
final product = filteredData[index];
|
||||
return ProductCard(
|
||||
imagePath: Image(
|
||||
image: Image.network(product.images[0].url)
|
||||
.image,
|
||||
width: 50,
|
||||
),
|
||||
name: product.title,
|
||||
price: product.price.toString(),
|
||||
onTap: () => Navigator.of(context).push(
|
||||
CustomPageRoute(
|
||||
builder: (context) => DetailPage(
|
||||
name: product.title,
|
||||
description: product.description,
|
||||
price: product.price.toString(),
|
||||
id: product.id,
|
||||
image: Image(
|
||||
image: Image.network(
|
||||
product.images[0].url)
|
||||
.image,
|
||||
width: 300,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user