You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
7.8 KiB

3 years ago
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<Desire> selected;
const NewDesire({Key key, this.selected}) : super(key: key);
@override
_NewDesireState createState() => _NewDesireState();
}
class _NewDesireState extends State<NewDesire> {
List<Desire> newdesire = <Desire>[];
List<Desire> newdesireMain = <Desire>[];
var search = new TextEditingController();
SharedPreferences prefs;
List<String> selectList = <String>[];
@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<String, String> 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<Desire>.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: <Widget>[
Expanded(
child: Material(
color: Colors.black54,
elevation: 0,
borderRadius: BorderRadius.circular(30.0),
child: Row(
children: <Widget>[
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();
}
}
},
),
);
}
}