import 'package:teso/Classes/API%20Clasess/Desire.dart'; import 'package:flutter/material.dart'; import 'package:teso/Classes/customTesoButton.dart'; import 'package:teso/util/consts.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:http/http.dart' as http; import 'dart:convert'; import 'package:teso/Pages/PageWidgets/DesireComeTrue/DesireTile.dart'; import 'NotListed.dart'; import 'package:jiffy/jiffy.dart'; class NewDesire extends StatefulWidget { final List? selected; const NewDesire({Key? key, this.selected}) : super(key: key); @override _NewDesireState createState() => _NewDesireState(); } class _NewDesireState extends State { List newdesire = []; List newdesireMain = []; var search = new TextEditingController(); late SharedPreferences prefs; List selectList = []; @override void initState() { super.initState(); search.addListener(() async { if (search.text.isNotEmpty) { lookItem(); } else { setState(() { newdesire.clear(); }); } }); selectList = widget.selected!.map((e) => e.productID).toList(); } lookItem() async { prefs = await SharedPreferences.getInstance(); String token = prefs.getString("tokensTeso")!; Map requestHeaders = { 'Content-type': 'application/json', 'Authorization': token }; String searchValue = search.text.toString(); var register = serverLocation + 'products/desire-product'; var client = await http.post(Uri.parse(register), body: json.encode(searchValue), headers: requestHeaders); if (client.statusCode == 200) { var items = jsonDecode(client.body); newdesireMain = List.from( items.map((model) => Desire.fromJSON(model)).toList()); updateList(); } else { if (mounted) { setState(() { newdesireMain.clear(); }); } } } updateList() { if (mounted) { setState(() { newdesireMain .removeWhere((element) => selectList.contains(element.productID)); newdesire = newdesireMain; newdesire.sort((a, b) { return b.productName!.compareTo(a.productName!); }); }); } } addItem(Desire item) { if (mounted) setState(() { widget.selected!.add(item); selectList.add(item.productID); newdesire.remove(item); }); } @override void dispose() { search.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( appBar: PreferredSize( child: Container( padding: EdgeInsets.symmetric(horizontal: 10), margin: EdgeInsets.only(top: 30), child: Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ Expanded( child: Material( color: Colors.black54, elevation: 0, borderRadius: BorderRadius.circular(30.0), child: Row( children: [ new Expanded( child: TextField( autofocus: false, textAlign: TextAlign.start, controller: search, style: TextStyle( color: Colors.white, ), decoration: InputDecoration( border: InputBorder.none, contentPadding: EdgeInsets.symmetric(horizontal: 14.0), hintText: "Search", hintStyle: TextStyle(color: Colors.grey), ), ), ), Container( child: InkWell( onTap: () => search.clear(), child: Icon( Icons.close_outlined, color: Colors.white, ), ), margin: EdgeInsets.symmetric(horizontal: 20), ), ], ), ), ), SizedBox( width: 15, ), Container( child: RaisedGradientButton( child: Text( "Done", style: TextStyle(color: Colors.white, fontSize: 15), ), gradient: LinearGradient( begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [ tesoGold, tesoGold, ], ), onPressed: () { SharedPreferences.getInstance().then((value) { var jiffy = Jiffy()..add(months: 1); value.setString("desire" + jiffy.format("MMMM, yyyy"), widget.selected.toString()); }); Navigator.pop(context, true); }, width: 70, height: 50, ), ), ], ), ), preferredSize: Size.fromHeight(50.0)), body: newdesire.length == 0 && search.text.length != 0 ? InkWell( onTap: () async { Desire? unlistedDesire = await showDialog( context: context, builder: (BuildContext bc) { return AlertDialog( content: NotListed( productName: search.text, ), ); }); if (unlistedDesire != null) widget.selected!.add(unlistedDesire); search.clear(); }, child: Container( padding: EdgeInsets.all(20), width: MediaQuery.of(context).size.width, child: Center( child: Container( child: Wrap( //direction: Axis.vertical, children: [ Container( child: Text( "Sorry, the product you entered has not been posted by any business. ", textAlign: TextAlign.center, style: TextStyle(fontSize: 16), ), ), Text( "Click here to have the product enlisted and sent to the appropriate businesses.", textAlign: TextAlign.center, style: TextStyle(color: tesoBlue, fontSize: 16.5), ), ], ), ), ), ), ) : ListView.builder( itemCount: newdesire.length, itemBuilder: (context, int index) { if (newdesire.length == 0 && search.text.length == 0) { return Container(); } else { if (!widget.selected!.contains(newdesire.elementAt(index))) { return buildProductDesire( context, newdesire.elementAt(index), addItem); } else { return Container(); } } }, ), ); } }