import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart'; import 'package:flutter/cupertino.dart'; import 'package:numeral/numeral.dart'; import 'package:page_transition/page_transition.dart'; import 'package:provider/provider.dart'; import 'package:teso/Classes/API%20Clasess/ThirdPerson.dart'; import 'package:teso/Classes/TesoUser.dart'; import 'package:flutter/material.dart'; import 'package:teso/Classes/customTesoButton.dart'; import 'package:teso/Pages/PageWidgets/Posts/posted.dart'; import 'package:teso/Pages/PageWidgets/Third Person Profile/header.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:http/http.dart' as http; import 'package:teso/providers/user_provider.dart'; import 'package:teso/util/SizeConfig.dart'; import 'package:teso/util/consts.dart'; import 'package:teso/Pages/PageWidgets/Third Person Profile/Empty.dart'; import 'dart:convert'; import 'Notifications/ChatScreen.dart'; // ignore: must_be_immutable class UserProfileThirdPerson extends StatefulWidget { TesoUser user; final ThirdPerson fullProfile; UserProfileThirdPerson({Key key, this.user, this.fullProfile}) : super(key: key); @override _UserProfileThirdPersonState createState() => _UserProfileThirdPersonState(); } enum AppBarBehavior { normal, pinned, floating, snapping } class _UserProfileThirdPersonState extends State with TickerProviderStateMixin { final datakey = new GlobalKey(); ThirdPerson userProfile = new ThirdPerson(); int count; ScrollController _controller, control, scrollController; // AppBarBehavior _appBarBehavior = AppBarBehavior.pinned; Animation width; Animation heigth; AnimationController _containerController; double scale; bool available = false; bool scrolled = false; SharedPreferences prefs; bool addFriends = false; bool friends = false; bool pending = false; bool approve = false; bool loading = false; int request = 0; var _future; Future getProfile() async { prefs = await SharedPreferences.getInstance(); String token = prefs.getString("tokensTeso"); Map requestHeaders = { 'Content-type': 'application/json', 'Authorization': token }; var register = serverLocation + 'user_details/pullProfile'; var client = await http.post(Uri.parse(register), body: json.encode(widget.user.userGUID), headers: requestHeaders); if (client.statusCode == 200) { var profile = jsonDecode(client.body); userProfile = ThirdPerson.fromJSON(profile); third(userProfile); } } void _scrollListener() { if (_controller.offset >= _controller.position.maxScrollExtent && !_controller.position.outOfRange) { // fetchImages(); } } @override void dispose() { scrollController.dispose(); _controller.dispose(); _containerController.dispose(); super.dispose(); } @override void initState() { super.initState(); _controller = ScrollController(); _controller.addListener(_scrollListener); count = 0; if (widget.fullProfile != null) { third(widget.fullProfile); } else { _future = getProfile(); } scrollController = new ScrollController(); scrollController.addListener(() => setState(() {})); _containerController = new AnimationController( duration: new Duration(milliseconds: 2000), vsync: this); 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(); } void third(ThirdPerson fullProfile) async { switch (fullProfile.relation) { case "not related": setState(() { addFriends = true; }); break; case "sent pending": setState(() { pending = true; }); break; case "received pending": setState(() { approve = true; }); break; case "friends": setState(() { friends = true; }); break; } if (fullProfile.user != null) widget.user = widget.fullProfile.user; // trends = fullProfile.posts; // await fetchImages(); } void removeRelationship() async { setState(() { loading = true; addFriends = false; friends = false; pending = false; approve = false; loading = false; }); prefs = await SharedPreferences.getInstance(); String token = prefs.getString("tokensTeso"); Map requestHeaders = { 'Content-type': 'application/json', 'Authorization': token }; String searchValue = widget.user.userGUID; var register = serverLocation + 'relationships/unfriend'; var client = await http.post(Uri.parse(register), body: json.encode(searchValue), headers: requestHeaders); if (client.statusCode == 200) { setState(() { friends = false; pending = false; addFriends = !friends; }); } else { setState(() { friends = true; pending = false; addFriends = !friends; }); } setState(() { loading = false; }); } void addRelationship() async { setState(() { loading = true; addFriends = false; friends = false; pending = false; approve = false; loading = false; }); prefs = await SharedPreferences.getInstance(); String token = prefs.getString("tokensTeso"); Map requestHeaders = { 'Content-type': 'application/json', 'Authorization': token }; String searchValue = widget.user.userGUID; var register = serverLocation + 'relationships/friendrequest'; var client = await http.post(Uri.parse(register), body: json.encode(searchValue), headers: requestHeaders); if (client.statusCode == 200) { setState(() { request = 52; pending = true; friends = false; addFriends = friends; }); } else { setState(() { friends = false; pending = false; addFriends = true; }); } setState(() { loading = false; }); } void approveRelationship() async { setState(() { loading = true; addFriends = false; friends = false; pending = false; approve = false; loading = false; }); prefs = await SharedPreferences.getInstance(); String token = prefs.getString("tokensTeso"); Map requestHeaders = { 'Content-type': 'application/json', 'Authorization': token }; String searchValue = widget.user.userGUID; var register = serverLocation + 'relationships/friendapproval'; var client = await http.post(Uri.parse(register), body: json.encode(searchValue), headers: requestHeaders); if (client.statusCode == 200) { setState(() { pending = false; friends = true; addFriends = !friends; approve = false; }); } else { setState(() { friends = false; pending = false; addFriends = false; approve = true; }); } setState(() { loading = false; }); } void declineRelationship() async { setState(() { loading = true; addFriends = false; friends = false; pending = false; approve = false; loading = false; }); prefs = await SharedPreferences.getInstance(); String token = prefs.getString("tokensTeso"); Map requestHeaders = { 'Content-type': 'application/json', 'Authorization': token }; String searchValue = widget.user.userGUID; var register = serverLocation + 'relationships/frienddecline'; var client = await http.post(Uri.parse(register), body: json.encode(searchValue), headers: requestHeaders); if (client.statusCode == 200) { setState(() { pending = false; friends = false; addFriends = !friends; approve = false; }); } else { setState(() { friends = false; pending = false; addFriends = false; approve = true; }); } setState(() { loading = false; }); } void cancelRelationshipRequest() async { setState(() { request = 0; loading = true; addFriends = false; friends = false; pending = false; approve = false; loading = false; }); prefs = await SharedPreferences.getInstance(); String token = prefs.getString("tokensTeso"); Map requestHeaders = { 'Content-type': 'application/json', 'Authorization': token }; String searchValue = widget.user.userGUID; var register = serverLocation + 'relationships/cancelrequest'; var client = await http.post(Uri.parse(register), body: json.encode(searchValue), headers: requestHeaders); if (client.statusCode == 200) { setState(() { friends = false; pending = false; addFriends = !friends; }); } setState(() { loading = false; }); } @override Widget build(BuildContext context) { if (scrollController.hasClients) { scale = scrollController.offset / 250; scale = scale * 2; if (scale > 1.90) { scrolled = true; } else { scrolled = false; } } else { scale = 0.0; scrolled = false; } return Scaffold( appBar: PreferredSize( preferredSize: Size.fromHeight(238), child: Column( children: [ AppBar( backgroundColor: Theme.of(context).backgroundColor, elevation: 0.0, leadingWidth: 50, centerTitle: true, leading: IconButton( icon: Icon( Icons.arrow_back_ios, ), onPressed: () => Navigator.pop(context), ), title: Text( widget.user.username, style: TextStyle( fontSize: 15, ), ), actions: [ Visibility( visible: addFriends, child: Container( margin: EdgeInsets.symmetric(horizontal: 10), height: 30.0, width: 40.0, decoration: new BoxDecoration( shape: BoxShape.circle, border: Border.all(color: accentMain, width: 2.0), ), child: IconButton( icon: Icon( Icons.person_add_alt, color: accentMain, ), onPressed: addRelationship, ), ), ), IconButton( onPressed: () => statusDialog(context), icon: Icon( Icons.more_horiz, ), ) ], ), Container( width: width.value, child: buildHead( context: context, loading: loading, user: widget.user, posts: userProfile.posts != null ? Numeral(userProfile.posts.length).value().toString() : "0", friends: userProfile.friends, following: userProfile.posts, relation: friends, // unFriend: removeRelationship, // approve: approveRelationship, pending: pending, // approveRequest: approve, //cancelRequest: cancelRelationshipRequest, // decline: declineRelationship ), ), ], ), ), body: FutureBuilder( future: _future, builder: (context, snapshot) { if (snapshot.data == null && snapshot.connectionState == ConnectionState.waiting) { return Center( child: CupertinoActivityIndicator( animating: true, radius: 15, ), ); } else if (userProfile.posts.length == 0 && snapshot.connectionState == ConnectionState.done) { return buildEmpty3P(context); } else { return Container( height: MediaQuery.of(context).size.height, child: StaggeredGridView.count( physics: scrolled ? NeverScrollableScrollPhysics() : AlwaysScrollableScrollPhysics(), controller: _controller, crossAxisCount: 3, children: List.generate(userProfile.posts.length, (int index) { return buildPosted( context, userProfile.posts.elementAt(index), 0.325, widget.user, addFriends); }), staggeredTiles: List.generate( userProfile.posts.length, (int index) { return StaggeredTile.fit(1); }, ), ), ); } }, ), ); } void statusDialog(BuildContext context) { showModalBottomSheet( context: context, isScrollControlled: true, enableDrag: true, shape: RoundedRectangleBorder( borderRadius: BorderRadius.vertical(top: Radius.circular(30.0)), ), builder: (BuildContext bc) { return SizedBox( height: MediaQuery.of(context).size.height * 0.5, child: Column( children: [ new Container( margin: EdgeInsets.symmetric( vertical: 15.0, ), child: Center( child: Padding( padding: const EdgeInsets.symmetric(horizontal: 20.0), child: Container( width: 50, height: 4, color: Colors.grey, ), ), ), ), SizedBox( height: 5, ), Container( height: MediaQuery.of(context).size.height * 0.4, padding: EdgeInsets.symmetric(horizontal: 15), child: new ListView( scrollDirection: Axis.vertical, children: [ ListTile( title: Center( child: Text( "Block User", style: TextStyle( color: Colors.red[800], fontSize: SizeConfig.blockSizeHorizontal * 4.5, fontWeight: FontWeight.bold), ), ), onTap: () => blockDialog(context), ), Visibility(visible: friends, child: Divider()), Visibility( visible: friends, child: ListTile( title: Center( child: Text( "Send Message", style: TextStyle( color: Colors.blueAccent, fontSize: SizeConfig.blockSizeHorizontal * 4.5, fontWeight: FontWeight.bold, ), ), ), onTap: () => Navigator.pushReplacement( context, PageTransition( child: ChatScreen( user: widget.user, ), type: PageTransitionType.fade, ), ), ), ), Visibility(visible: addFriends, child: Divider()), Visibility( visible: addFriends, child: ListTile( title: Center( child: Text( "Add Friend", style: TextStyle( color: Colors.blueAccent, fontSize: SizeConfig.blockSizeHorizontal * 4.5, fontWeight: FontWeight.bold), ), ), onTap: () { addRelationship(); Navigator.pop(context); }), ), Visibility(visible: pending, child: Divider()), Visibility( visible: pending, child: ListTile( title: Center( child: Text( "Cancel Friend Request", style: TextStyle( color: Colors.red[800], fontSize: SizeConfig.blockSizeHorizontal * 4.5, fontWeight: FontWeight.bold), ), ), onTap: () { declineRelationship(); Navigator.pop(context); }), ), Visibility(visible: approve, child: Divider()), Visibility( visible: approve, child: ListTile( title: Center( child: Text( "Accept Friend Request", style: TextStyle( color: Colors.blueAccent, fontSize: SizeConfig.blockSizeHorizontal * 4.5, fontWeight: FontWeight.bold), ), ), onTap: approveRelationship, ), ), Visibility(visible: approve, child: Divider()), Visibility( visible: approve, child: ListTile( title: Center( child: Text( "Decline Friend Request", style: TextStyle( color: Colors.red[800], fontSize: SizeConfig.blockSizeHorizontal * 4.5, fontWeight: FontWeight.bold), ), ), onTap: () { declineRelationship(); Navigator.pop(context); }), ), Visibility(visible: friends, child: Divider()), Visibility( visible: friends, child: ListTile( title: Center( child: Text( "Unfriend", style: TextStyle( color: Colors.red[800], fontSize: SizeConfig.blockSizeHorizontal * 4.5, fontWeight: FontWeight.bold), ), ), onTap: () { removeRelationship(); Navigator.pop(context); }), ), ], ), ), ], ), ); }, ); } blockDialog(BuildContext context) { showModalBottomSheet( context: context, isScrollControlled: true, enableDrag: true, shape: RoundedRectangleBorder( borderRadius: BorderRadius.vertical(top: Radius.circular(30.0)), ), builder: (BuildContext bc) { return SizedBox( height: MediaQuery.of(context).size.height * 0.38, child: Column( children: [ new Container( margin: EdgeInsets.symmetric( vertical: 15.0, ), child: Center( child: Padding( padding: const EdgeInsets.symmetric(horizontal: 20.0), child: Container( width: 50, height: 4, color: Colors.grey, ), ), ), ), SizedBox( height: 5, ), Container( padding: EdgeInsets.symmetric(horizontal: 15), child: Text( "Block ${widget.user.username.toUpperCase()} ? ", style: TextStyle( fontWeight: FontWeight.bold, ), ), ), Divider(), Container( padding: EdgeInsets.symmetric(horizontal: 15), child: Text( "${widget.user.username} won't be able to message you or find your profile or post on Teso. Neither would you, " "they won't be notified that you have blocked them. You would have to unblock them to be able to interact with them.", style: TextStyle( color: Colors.grey, fontSize: SizeConfig.blockSizeHorizontal * 3.5, ), ), ), Container( margin: EdgeInsets.only(top: 20), width: MediaQuery.of(context).size.width - 20, height: 40, child: RaisedGradientButton( child: Text( "Continue", style: TextStyle(color: Colors.white, fontSize: 18), ), gradient: LinearGradient( begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [ // Colors.green[400], // Colors.green[600], Colors.red[800], Colors.redAccent, ], ), onPressed: () { Provider.of(context, listen: false) .blockUser(widget.user); Navigator.pop(context, true); Navigator.pop(context, true); }, width: MediaQuery.of(context).size.width - 20, height: 40, ), ) ], ), ); }, ); } }