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.

142 lines
4.3 KiB

3 years ago
import 'package:flutter/material.dart';
import 'PageWidgets/Campaigns/header.dart';
import 'package:teso/Classes/API Clasess/Campaign.dart';
import 'PageWidgets/Campaigns/campaignTile.dart';
import 'package:flutter/cupertino.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:teso/util/consts.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'dart:async';
class Campaigns extends StatefulWidget {
@override
_CampaignsState createState() => _CampaignsState();
}
class _CampaignsState extends State<Campaigns> {
TextEditingController searchkey;
List<Campaign> campaignMain;
List<Campaign> campaign;
var _future;
void clearText() {
setState(() {
searchkey.clear();
});
}
Future<List<Campaign>> getCampaigns() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
Map<String, String> requestHeaders = {
'Content-type': 'application/json',
'Authorization': prefs.getString('tokensTeso')
};
var register2 = serverLocation + 'adverts/businesscampaigns';
var client1 = await http.post(Uri.parse(register2),
body: json.encode(searchkey.text), headers: requestHeaders);
if (client1.statusCode == 200) {
var details = jsonDecode(client1.body);
if (mounted)
setState(() {
campaign = List<Campaign>.from(
details.map((model) => Campaign.fromJSON(model)).toList());
});
if (campaignMain == null) {
setState(() {
campaignMain = campaign;
});
}
}
return campaign;
}
@override
void initState() {
super.initState();
searchkey = new TextEditingController();
_future = getCampaigns();
searchkey.addListener(() async {
if (searchkey.text.isNotEmpty) {
getCampaigns();
} else {
setState(() {
campaign = campaignMain;
});
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.transparent,
automaticallyImplyLeading: true,
title: Text("Join a Campaign"),
centerTitle: true,
),
body: Container(
// padding: EdgeInsets.only(
// left: 10,
// right: 10,
// ),
child: Column(
children: [
buildCampaignHead(context, searchkey, clearText),
SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Container(
width: MediaQuery.of(context).size.width,
// height: MediaQuery.of(context).size.height,
child: FutureBuilder(
future: _future,
builder: (context, snapshot) {
if (snapshot.data == null &&
snapshot.connectionState == ConnectionState.waiting) {
return Container(
child: Center(
child: CupertinoActivityIndicator(
animating: true,
radius: 15,
),
),
);
} else if (snapshot.data == null &&
snapshot.connectionState == ConnectionState.done) {
return Container(
height: MediaQuery.of(context).size.width,
width: MediaQuery.of(context).size.width,
child: Center(
child: Text(
"Sorry there are no open campaigns at the moment"),
),
);
} else {
return ListView.builder(
primary: true,
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: campaign.length,
itemBuilder: (context, index) {
return buildCampaign(
context,
campaign.elementAt(index),
);
},
);
}
},
),
),
),
],
),
),
);
}
}