import 'package:provider/provider.dart'; import 'package:teso/Classes/TesoUser.dart'; import 'package:teso/Pages/PageWidgets/Friends/friendTile.dart'; import 'package:teso/Pages/PageWidgets/Inbox/newMessageHeader.dart'; import 'package:teso/Pages/Sub_Pages/Notifications/ChatScreen.dart'; import 'package:teso/providers/pageAnimations.dart'; import 'package:flutter/material.dart'; import 'package:teso/Pages/PageWidgets/Friends/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/consts.dart'; import 'dart:convert'; class NewMessage extends StatefulWidget { @override _NewMessageState createState() => _NewMessageState(); } class _NewMessageState extends State { TextEditingController searchkey; List recipientMain = []; List recipient; SharedPreferences prefs; void clearText() { setState(() { // searchkey.clear(); // recipient = recipientMain; // recipient.sort((a, b) { // return b.userID.compareTo(a.userID); // }); }); } @override void initState() { searchkey = new TextEditingController(); Provider.of(context, listen: false).loadFriends(); super.initState(); } Future loadFriends() async { prefs = await SharedPreferences.getInstance(); String token = prefs.getString("tokensTeso"); Map requestHeaders = { 'Content-type': 'application/json', 'Authorization': token }; var register = serverLocation + 'relationships/friends'; var client = await http.get(Uri.parse(register), headers: requestHeaders); if (client.statusCode == 200) { var people = jsonDecode(client.body); recipientMain = List.from( people.map((model) => TesoUser.fromJSON(model)).toList()); } recipient = recipientMain; recipient.sort((a, b) { return b.firstname.compareTo(a.firstname); }); } updateList(String name) { setState(() { recipient = recipientMain .where((element) => element.username.toLowerCase().contains(name.toLowerCase())) .toList(); recipient.sort((a, b) { return b.firstname.compareTo(a.firstname); }); }); } @override Widget build(BuildContext context) { return Scaffold( appBar: PreferredSize( preferredSize: Size.fromHeight(150), child: Container( margin: EdgeInsets.only(top: 30), child: Column(children: [ buildNewHead(context, clearText, "New Message"), buildFriendsHeader(context, searchkey, updateList), ]), ), ), body: Consumer( builder: (context, value, child) { if (value.friends == null) { return Center( child: CircularProgressIndicator( valueColor: AlwaysStoppedAnimation( Theme.of(context).primaryColor), ), ); } else { if (recipientMain.length == 0) { recipientMain = value.friends; recipient = value.friends; } recipient.sort((a, b) { return b.firstname.compareTo(a.firstname); }); return ListView.builder( itemCount: recipient.length, itemBuilder: (context, int index) { return InkWell( onTap: () => Navigator.pushReplacement( context, PageTransition( child: ChatScreen(user: recipient.elementAt(index)), type: PageTransitionType.fade, ), ), child: buildFriend(context, recipient.elementAt(index)), ); }, ); } }, ), ); } }