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 { TextEditingController searchkey; List campaignMain; List campaign; var _future; void clearText() { setState(() { searchkey.clear(); }); } Future> getCampaigns() async { SharedPreferences prefs = await SharedPreferences.getInstance(); Map 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.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), ); }, ); } }, ), ), ), ], ), ), ); } }