import 'package:teso/Classes/API%20Clasess/BusinessProfile.dart'; import 'package:teso/Classes/TesoShop.dart'; import 'package:teso/Classes/TesoUser.dart'; import 'package:teso/Pages/PageWidgets/BusinessProfile/BusinessHead.dart'; import 'package:teso/Pages/Sub_Pages/Business/BusinessProducts.dart'; import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:http/http.dart' as http; import 'package:teso/util/consts.dart'; import 'package:teso/Classes/API Clasess/Product.dart'; import 'package:teso/Classes/API Clasess/CouponDetails.dart'; import 'dart:convert'; import 'package:flutter/cupertino.dart'; class BusinessProfile extends StatefulWidget { final TesoShop? shop; const BusinessProfile({Key? key, this.shop}) : super(key: key); @override _BusinessProfileState createState() => new _BusinessProfileState(shop: shop); } enum AppBarBehavior { normal, pinned, floating, snapping } class _BusinessProfileState extends State with TickerProviderStateMixin { late AnimationController _containerController; Animation? width; late Animation heigth; final TesoShop? shop; _BusinessProfileState({this.shop}); bool? subscribed = false; late SharedPreferences prefs; var stats; int productTotal = 0; int adsTotal = 0; int couponsTotal = 0; List? products; List? coupons; late BusinessProfileClass profile; List? subscribers; List? _randomChildren; Future subscribe() async { prefs = await SharedPreferences.getInstance(); String token = prefs.getString("tokensTeso")!; Map requestHeaders = { 'Content-type': 'application/json', 'Authorization': token }; var register = serverLocation + 'relationships/business-subscription'; var client = await http.post(Uri.parse(register), headers: requestHeaders, body: json.encode(shop!.shopID)); if (client.statusCode == 200) { setState(() { subscribed = true; }); } else { return null; } } Future unsubscribe() async { prefs = await SharedPreferences.getInstance(); String token = prefs.getString("tokensTeso")!; Map requestHeaders = { 'Content-type': 'application/json', 'Authorization': token }; var register = serverLocation + 'relationships/business-unsubscribe'; var client = await http.post(Uri.parse(register), headers: requestHeaders, body: json.encode(shop!.shopID)); if (client.statusCode == 200) { setState(() { subscribed = false; }); } else { return null; } } fetchProfile() async { prefs = await SharedPreferences.getInstance(); String token = prefs.getString("tokensTeso")!; Map requestHeaders = { 'Content-type': 'application/json', 'Authorization': token }; var register = serverLocation + 'tesobusiness/profile'; var client = await http.post(Uri.parse(register), headers: requestHeaders, body: json.encode(shop!.shopID)); if (client.statusCode == 200) { var details = jsonDecode(client.body); setState(() { profile = BusinessProfileClass.fromJSON(details); products = profile.products; coupons = profile.coupons; couponsTotal = coupons!.length; subscribed = profile.subscribed; subscribers = profile.subscribers; productTotal = products!.length; }); } else { return null; } } Future fetched() async { try { await fetchProfile(); return true; } catch (e) { return false; } } @override void initState() { stats = fetched(); _containerController = new AnimationController( duration: new Duration(milliseconds: 2000), vsync: this); super.initState(); width = new Tween( begin: 200.0, end: 220.0, ).animate( new CurvedAnimation( parent: _containerController, curve: Curves.ease, ), ); heigth = new Tween( begin: 400.0, end: 400.0, ).animate( new CurvedAnimation( parent: _containerController, curve: Curves.ease, ), ); heigth.addListener(() { setState(() { if (heigth.isCompleted) {} }); }); _containerController.forward(); } @override void dispose() { _containerController.dispose(); super.dispose(); } List? _randomHeightWidgets(BuildContext context) { _randomChildren ??= List.generate(1, (index) { return buildHead( context: context, shopSelected: shop!, subscribers: subscribers!.length, coupons: couponsTotal, products: productTotal); }); return _randomChildren; } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( backgroundColor: Colors.transparent, toolbarHeight: 50, automaticallyImplyLeading: false, title: Text(shop!.shopName!.toUpperCase()), leadingWidth: 40, leading: IconButton( icon: Icon(Icons.arrow_back_ios), onPressed: () => Navigator.pop(context), ), actions: [ FutureBuilder( future: stats, builder: (context, snapshot) { if (snapshot.data == null && snapshot.connectionState == ConnectionState.waiting) { return Container(); } else if (snapshot.data == null && snapshot.connectionState == ConnectionState.done) { return IconButton( icon: Icon( Icons.notifications_off, ), onPressed: null, ); } else { return IconButton( icon: Icon( subscribed! ? Icons.notifications_active : Icons.notifications_none, ), onPressed: () async { if (subscribed!) { await unsubscribe(); } else { await subscribe(); } }, ); } }, ), ], ), body: FutureBuilder( future: stats, builder: (context, snapshot) { if (snapshot.data == null) { return Container( child: Center( child: CupertinoActivityIndicator( animating: true, radius: 15, ), ), ); } else { return DefaultTabController( length: 1, child: NestedScrollView( headerSliverBuilder: (context, _) { return [ SliverList( delegate: SliverChildListDelegate( _randomHeightWidgets(context)!, ), ), ]; }, body: TabBarView( children: [ BusinessProducts( shopName: shop!.shopName, product: products, ), // BusinessPosts(), ], ), ), ); } }, ), ); } }