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.

291 lines
8.1 KiB

3 years ago
import 'package:teso/Pages/Sub_Pages/Explore/ExploreBusiness.dart';
import 'package:teso/Pages/Sub_Pages/Explore/ExplorePeople.dart';
import 'package:teso/Pages/Sub_Pages/Explore/ExploreProduct.dart';
import 'package:flutter/material.dart';
import 'package:teso/Classes/TesoUser.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:http/http.dart' as http;
import 'package:teso/util/consts.dart';
import 'dart:convert';
import 'package:teso/Classes/API Clasess/Product.dart';
import 'package:teso/Classes/TesoShop.dart';
class Lookup extends StatefulWidget {
@override
_LookupState createState() => _LookupState();
}
class _LookupState extends State<Lookup> with TickerProviderStateMixin {
List<TesoUser> people = [];
List<TesoUser> peopleAlert = [];
List<Product> product = [];
List<Product> productAlert = [];
List<TesoShop> businessAlert = [];
List<TesoShop> business = [];
TabController tabController;
var search = new TextEditingController();
SharedPreferences prefs;
var future;
@override
void initState() {
super.initState();
tabController = new TabController(length: 3, vsync: this);
tabController.addListener(() async {
if (search.text.isNotEmpty) {
switch (tabController.index) {
case 0:
await lookupUser();
break;
case 1:
await lookupProducts();
break;
case 2:
await lookupBusiness();
break;
default:
await lookupUser();
break;
}
}
});
search.addListener(() async {
if (search.text.isNotEmpty) {
switch (tabController.index) {
case 0:
await lookupUser();
break;
case 1:
await lookupProducts();
break;
case 2:
await lookupBusiness();
break;
default:
await lookupUser();
break;
}
} else {
setState(() {
people.clear();
product.clear();
business.clear();
});
}
});
}
lookupUser() async {
prefs = await SharedPreferences.getInstance();
String token = prefs.getString("tokensTeso");
Map<String, String> requestHeaders = {
'Content-type': 'application/json',
'Authorization': token
};
String searchValue = search.text;
var register = serverLocation + 'search/people';
var client = await http.post(Uri.parse(register),
body: json.encode(searchValue), headers: requestHeaders);
if (client.statusCode == 200) {
var people = jsonDecode(client.body);
this.people = List<TesoUser>.from(
people.map((model) => TesoUser.fromJSON(model)).toList());
updateList();
} else {
if (mounted) {
setState(() {
people.clear();
});
}
}
}
lookupProducts() async {
prefs = await SharedPreferences.getInstance();
String token = prefs.getString("tokensTeso");
Map<String, String> requestHeaders = {
'Content-type': 'application/json',
'Authorization': token
};
String searchValue = search.text;
var register = serverLocation + 'search/products';
var client = await http.post(Uri.parse(register),
body: json.encode(searchValue), headers: requestHeaders);
if (client.statusCode == 200) {
var productItem = jsonDecode(client.body);
this.product = List<Product>.from(
productItem.map((model) => Product.fromJson(model)).toList());
orderProductsList();
} else {
if (mounted) {
setState(() {
product.clear();
});
}
}
}
lookupBusiness() async {
prefs = await SharedPreferences.getInstance();
String token = prefs.getString("tokensTeso");
Map<String, String> requestHeaders = {
'Content-type': 'application/json',
'Authorization': token
};
String searchValue = search.text;
var register = serverLocation + 'search/business';
var client = await http.post(Uri.parse(register),
body: json.encode(searchValue), headers: requestHeaders);
if (client.statusCode == 200) {
var businessItem = jsonDecode(client.body);
this.business = List<TesoShop>.from(
businessItem.map((model) => TesoShop.fromJSON(model)).toList());
orderBusinessList();
} else {
if (mounted) {
setState(() {
product.clear();
});
}
}
}
updateList() {
if (mounted) {
setState(() {
peopleAlert = people;
});
}
}
orderProductsList() {
if (mounted) {
setState(() {
productAlert = product;
});
}
}
orderBusinessList() {
if (mounted) {
setState(() {
businessAlert = business;
});
}
}
@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),
),
],
),
),
),
Container(
margin: EdgeInsets.symmetric(horizontal: 20),
child: InkWell(
onTap: () {
Navigator.pop(context);
},
child: Center(
child: Text(
"Cancel",
style: TextStyle(fontSize: 15),
),
),
),
)
],
),
),
preferredSize: Size.fromHeight(50.0)),
body: Column(
children: <Widget>[
TabBar(
controller: tabController,
tabs: [
Tab(
text: "People",
),
Tab(
text: "Products",
),
Tab(
text: "Business",
),
],
),
Expanded(
child: TabBarView(
controller: tabController,
children: [
ExplorePeople(
people: peopleAlert,
),
ExploreProduct(
products: productAlert,
),
ExploreBusiness(
businesses: businessAlert,
),
],
),
),
],
),
);
}
}