import 'dart:convert'; import 'package:teso/Pages/Sub_Pages/Explore/Latest/AllLatest.dart'; import 'package:camera/camera.dart'; import 'package:http/http.dart' as http; import 'package:teso/Classes/API%20Clasess/Product.dart'; import 'package:teso/Classes/API%20Clasess/ExploreObject.dart'; import 'package:teso/Classes/categories.dart'; import 'package:teso/Pages/PageWidgets/Explore/categoriesTile.dart'; import 'package:teso/Pages/Sub_Pages/@Generic/productDetails.dart'; import 'package:teso/Pages/Sub_Pages/Explore/Categories/AllCategories.dart'; import 'package:teso/Pages/Sub_Pages/Explore/Categories/ExploreCategory.dart'; import 'package:teso/Pages/Sub_Pages/Explore/Trending/AllTrending.dart'; import 'package:flutter/material.dart'; import 'package:teso/Pages/PageWidgets/Explore/header.dart'; import 'package:teso/Pages/PageWidgets/Explore/popular.dart'; import 'package:teso/Pages/PageWidgets/Explore/trending.dart'; import 'package:teso/util/consts.dart'; import 'package:flutter/cupertino.dart'; import 'package:page_transition/page_transition.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'dart:io'; import 'Sub_Pages/Explore/ML/FindProduct.dart'; import 'Sub_Pages/@Generic/Camera/Picture/TakePicture.dart'; import 'package:pull_to_refresh/pull_to_refresh.dart'; class Explore extends StatefulWidget { final List connectedCameras; const Explore({Key key, this.connectedCameras}) : super(key: key); @override _ExploreState createState() => _ExploreState(); } class _ExploreState extends State with TickerProviderStateMixin { var search = new TextEditingController(); // List trends; List showTrends = []; List categories = Category.category; // List arrivals; List showarrivals = []; File _image; String thumb; SharedPreferences prefs; var _future; ExploreClass deadExplore; RefreshController _refreshController = RefreshController(initialRefresh: false); Future loadPage() async { try { prefs = await SharedPreferences.getInstance(); String token = prefs.getString("tokensTeso"); Map requestHeaders = { 'Content-type': 'application/json', 'Authorization': token }; var register = serverLocation + 'explore/main'; var client = await http.get(Uri.parse(register), headers: requestHeaders); if (client.statusCode == 200) { ExploreClass exploreClass; Map products = jsonDecode(client.body); SharedPreferences.getInstance() .then((value) => value.setString("explore", client.body)); setState(() { exploreClass = ExploreClass.fromJSON(products); showTrends = exploreClass.trending; showarrivals = exploreClass.latest; }); return exploreClass; } else { return null; } } catch (e) { print(e); return null; } } void _onRefresh() async { await loadPage(); _refreshController.refreshCompleted(); } @override void initState() { super.initState(); SharedPreferences.getInstance().then((value) { if (value.getString("explore") != null) { var products = jsonDecode(value.getString("explore")); setState(() { deadExplore = ExploreClass.fromJSON(products); showTrends = deadExplore.trending; showarrivals = deadExplore.latest; }); } }); _future = loadPage(); } pickImage() async { final pickedFile = await Navigator.push( context, PageTransition( type: PageTransitionType.leftToRight, child: TakePicture( connectedCameras: widget.connectedCameras, ), )); if (pickedFile != null) { setState(() { _image = File(pickedFile.path); }); Navigator.push( context, PageTransition( type: PageTransitionType.leftToRight, child: MLFindProduct( searchImage: _image, ), )); } else { print('No image selected.'); } } @override Widget build(BuildContext context) { return Scaffold( appBar: PreferredSize( child: Container( padding: EdgeInsets.only(top: 25.0), child: buildSearch(context, search, pickImage), ), preferredSize: Size.fromHeight(150.0), ), body: FutureBuilder( initialData: deadExplore, future: _future, builder: (context, snapshot) { if (snapshot.data == null && deadExplore == null && snapshot.connectionState == ConnectionState.waiting) { return Container( child: Center( child: CupertinoActivityIndicator( animating: true, radius: 15, ), ), ); } else if (snapshot.data == null && deadExplore == null && snapshot.connectionState == ConnectionState.done) { return SmartRefresher( enablePullDown: true, enablePullUp: false, header: ClassicHeader(), controller: _refreshController, onRefresh: _onRefresh, child: Container( child: Center( child: Text("An error occurred"), ), ), ); } else { // trends = snapshot.data != null // ? snapshot.data.trending // : deadExplore.trending; // showTrends = trends; // arrivals = snapshot.data != null // ? snapshot.data.latest // : deadExplore.latest; // showarrivals = arrivals; return SmartRefresher( enablePullDown: true, enablePullUp: false, header: ClassicHeader(), controller: _refreshController, onRefresh: _onRefresh, child: ListView( children: [ Container( padding: EdgeInsets.symmetric(horizontal: 20.0), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( 'Trending', style: TextStyle( fontSize: 18.0, fontWeight: FontWeight.bold, letterSpacing: 1.5, ), ), GestureDetector( onTap: () => Navigator.push( context, PageTransition( child: TrendingAll( products: showTrends, ), type: PageTransitionType.leftToRight), ), child: Text( "See More", style: TextStyle( color: slimAccent, fontWeight: FontWeight.bold, fontSize: 16.5, ), ), ), ], ), ), Container( width: double.infinity, height: MediaQuery.of(context).size.width * 0.65, child: ListView.builder( scrollDirection: Axis.horizontal, itemCount: showTrends.length > 6 ? 6 : showTrends.length, itemBuilder: (context, index) { return InkWell( onTap: () => Navigator.push( context, PageTransition( type: PageTransitionType.fade, child: ProductDetails( product: showTrends.elementAt(index), ), ), ), child: buildTrend( context, showTrends.elementAt(index), ), ); }, ), ), SizedBox( height: 20, ), Container( padding: EdgeInsets.symmetric(horizontal: 20.0), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( 'Categories', style: TextStyle( fontSize: 18.0, fontWeight: FontWeight.bold, letterSpacing: 1.5, ), ), GestureDetector( onTap: () => Navigator.push( context, PageTransition( child: CategoriesAll(), type: PageTransitionType.leftToRight), ), child: Text( "See All", style: TextStyle( color: slimAccent, fontWeight: FontWeight.bold, fontSize: 16.5, ), ), ), ], ), ), Container( width: double.infinity, height: 80, padding: EdgeInsets.only(left: 10.0), child: ListView.builder( scrollDirection: Axis.horizontal, itemCount: 10, itemBuilder: (context, index) { return InkWell( onTap: () => Navigator.push( context, PageTransition( child: ExploreCategory( selectedCategory: categories.elementAt(index), ), type: PageTransitionType.leftToRight), ), child: buildCategory( context, categories.elementAt(index), ), ); }, ), ), SizedBox( height: 20, ), Container( padding: EdgeInsets.symmetric(horizontal: 20.0), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( 'New Arrivals', style: TextStyle( fontSize: 18.0, fontWeight: FontWeight.bold, letterSpacing: 1.5, ), ), GestureDetector( onTap: () => Navigator.push( context, PageTransition( child: NewArrivals( products: showarrivals, ), type: PageTransitionType.leftToRight), ), child: Text( "See All", style: TextStyle( color: slimAccent, fontWeight: FontWeight.bold, fontSize: 16.5, ), ), ), ], ), ), Container( height: MediaQuery.of(context).size.height, child: GridView.count( physics: NeverScrollableScrollPhysics(), crossAxisCount: 2, crossAxisSpacing: 5.0, mainAxisSpacing: 5.0, children: List.generate( showarrivals.length > 6 ? 6 : showarrivals.length, (index) { return buildPopularItem( context, showarrivals.elementAt(index), ); }, ), ), ), ], ), ); } }, ), ); } }