import 'package:flutter/material.dart'; import 'dart:convert'; import 'config.dart'; import 'package:location/location.dart'; import 'dart:async'; import 'dart:developer'; import 'functions.dart'; import 'package:http/http.dart' as http; import 'package:url_launcher/url_launcher.dart'; import 'offer_business_profile.dart'; import 'advertise.dart'; import 'main.dart'; class Ads{ String title; String image; String id; String desc; Ads({this.title,this.image,this.desc,this.id}); factory Ads.fromJson(Map json) { return Ads( title: json['title'] as String, image: json['image'] as String, id: json['id'] as String, desc: json['desc'] as String, ); } } class Offer{ String title; String image; String id; String desc; String name; String phone; String location; Offer({this.title,this.image,this.desc,this.name,this.phone,this.location,this.id}); factory Offer.fromJson(Map json) { return Offer( title: json['title'] as String, image: json['image'] as String, id: json['id'] as String, desc: json['desc'] as String, name: json['name'] as String, phone: json['phone'] as String, location: json['location'] as String, ); } } class Offers extends StatelessWidget { @override Widget build(BuildContext context) { return new MaterialApp( debugShowCheckedModeBanner: false, title: 'Flutter Demo', theme: new ThemeData( primarySwatch: Colors.blue, ), home: new MyHomePage(title: 'Offers'), ); } } class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key); final String title; @override _MyHomePageState createState() => new _MyHomePageState(); } class _MyHomePageState extends State { StreamSubscription locationSubscription; List parseAds(String responseBody) { final parsed = json.decode(responseBody).cast>(); return parsed.map((json) => Ads.fromJson(json)).toList(); } List parseOffers(String responseBody) { final parsed = json.decode(responseBody).cast>(); return parsed.map((json) => Offer.fromJson(json)).toList(); } List ads; List offers; String latitude = ""; String longitude = ""; void get_data() async { Location location = new Location(); bool _serviceEnabled; PermissionStatus _permissionGranted; _serviceEnabled = await location.serviceEnabled(); if (!_serviceEnabled) { _serviceEnabled = await location.requestService(); if (!_serviceEnabled) { return; } } _permissionGranted = await location.hasPermission(); if (_permissionGranted == PermissionStatus.denied) { _permissionGranted = await location.requestPermission(); if (_permissionGranted != PermissionStatus.granted) { return; } } await location.getLocation().then((res) { latitude = res.latitude.toString(); longitude = res.longitude.toString(); }); locationSubscription = location.onLocationChanged.listen((res) { latitude = res.latitude.toString(); longitude = res.longitude.toString(); }); if(latitude.isNotEmpty) { print("Latitude:" + latitude + " Longitude:" + longitude); var res = await http.post( new Config().ads_url(latitude, longitude)); //in query there might be unwant character so, we encode the query to url if (res.statusCode == 200) { if (mounted) { setState(() { ads = parseAds(res.body); log(res.body); //update data value and UI }); } } var offers_res = await http.post( new Config().offers_url(latitude, longitude)); //in query there might be unwant character so, we encode the query to url if (offers_res.statusCode == 200) { if (mounted) { setState(() { offers = parseOffers(offers_res.body); log(offers_res.body); //update data value and UI }); } } } } @override initState() { get_data(); super.initState(); } @override Widget build(BuildContext context) { final _width = MediaQuery.of(context).size.width; final _height = MediaQuery.of(context).size.height; Widget headerList = Expanded( child:Container( padding: EdgeInsets.all(20), child: Center(child: CircularProgressIndicator()) //if is searching then show "Please wait" //else show search peopels text )); if(ads !=null) { headerList = new ListView.builder( itemBuilder: (context, index) { EdgeInsets padding = index == 0 ? const EdgeInsets.only( left: 10.0, right: 10.0, top: 4.0, bottom: 10.0) : const EdgeInsets.only( left: 10.0, right: 10.0, top: 4.0, bottom: 10.0); return new Padding( padding: padding, child: new InkWell( onTap: () { new Functions().openBrowser(new Config().ad_clicked_url(ads[index].id)); }, child: new Container( height: 100.0, width: 280.0, child: new ListTile( title: new Column( children: [ new Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ new Container( height: 72.0, width: 72.0, decoration: new BoxDecoration( color: Colors.lightGreen, boxShadow: [ new BoxShadow( color: Colors.black.withAlpha(70), offset: const Offset(2.0, 2.0), blurRadius: 2.0) ], image: new DecorationImage( image: NetworkImage( ads[index].image, ), fit: BoxFit.fill, )), ), new SizedBox( width: 8.0, ), new Expanded( child: new Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ new Text( ads[index].title, style: new TextStyle( fontSize: 14.0, color: Colors.black87, fontWeight: FontWeight.bold), ), new SizedBox( height: 5.0, ), new Text( ads[index].desc, style: new TextStyle( fontSize: 12.0, color: Colors.black54, fontWeight: FontWeight.normal), ) ], )), ], ), ], ), ), ), ), ); }, scrollDirection: Axis.horizontal, itemCount: ads.length, ); } Widget body = new Scaffold( appBar: AppBar( // Here we take the value from the MyHomePage object that was created by // the App.build method, and use it to set our appbar title. title: Text("Offers",style: TextStyle( color: Colors.black)), automaticallyImplyLeading: true, //`true` if you want Flutter to automatically add Back Button when needed, //or `false` if you want to force your own back button every where leading: IconButton(icon:Icon(Icons.arrow_back, color: Colors.black), //onPressed:() => Navigator.pop(context, false), onPressed:(){ Navigator.of(context).push(MaterialPageRoute( builder: (context) => MyApp(), )); }, ), backgroundColor: Colors.white, elevation: 0.0, ), body: new Container( padding: EdgeInsets.all(20), child: Center(child: CircularProgressIndicator()) //if is searching then show "Please wait" //else show search peopels text ) ); if((ads!=null)&&(offers!=null)) { body = new Scaffold( appBar: AppBar( // Here we take the value from the MyHomePage object that was created by // the App.build method, and use it to set our appbar title. title: Text("Offers",style: TextStyle( color: Colors.black)), automaticallyImplyLeading: true, //`true` if you want Flutter to automatically add Back Button when needed, //or `false` if you want to force your own back button every where leading: IconButton(icon:Icon(Icons.arrow_back, color: Colors.black), //onPressed:() => Navigator.pop(context, false), onPressed:(){ Navigator.of(context).push(MaterialPageRoute( builder: (context) => MyApp(), )); }, ), backgroundColor: Colors.white, elevation: 0.0, ), body: new Container( child: new Stack( children: [ new Padding( padding: new EdgeInsets.only(top: 10.0), child: SingleChildScrollView( padding: EdgeInsets.all(10.0), child:new Column( children: [ new SizedBox( height: 30.0, ), Text( 'Sponsored Adsense', style: new TextStyle(color: Colors.black,fontWeight: FontWeight.bold), ), new SizedBox( height: 20.0, ), headerList, new SizedBox( height: 10.0, ), FlatButton( padding: EdgeInsets.all(0.0), materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, onPressed: () { Navigator.of(context).pushReplacement(MaterialPageRoute( builder: (context) => Advertise(latitude:latitude,longitude:longitude), )); }, child: Container(child: new Text( "Advertise", style: new TextStyle( color: Colors.blue),)) ), new SizedBox( height: 10.0, ), Text( 'Offers', style: new TextStyle(color: Colors.black,fontWeight: FontWeight.bold), ), new Expanded(child: ListView.builder(itemBuilder: (context, index) { return new ListTile( title: new Column( children: [ new Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ new Container( width: MediaQuery.of(context).size.width, height: 200, decoration: BoxDecoration( image: DecorationImage( fit: BoxFit.fill, image: NetworkImage(offers[index].image), ), ), ), new SizedBox( height: 10.0, ), new Text( offers[index].title, style: new TextStyle( fontSize: 14.0, color: Colors.black87, fontWeight: FontWeight.bold), ), new SizedBox( height: 10.0, ), new Text( offers[index].desc, style: new TextStyle( fontSize: 12.0, color: Colors.black54, fontWeight: FontWeight.normal), ), new SizedBox( height: 10.0, ), new Text( offers[index].name, style: new TextStyle( fontSize: 14.0, color: Colors.blue, ), ), new Text( offers[index].location, style: new TextStyle( fontSize: 14.0, color: Colors.black, ), ), new SizedBox( height: 10.0, ), FlatButton( padding: EdgeInsets.all(0.0), materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, onPressed: () { launch("tel://"+offers[index].phone); }, child:ListTile( dense:true, contentPadding: EdgeInsets.only(left: 0.0, right: 0.0), visualDensity: VisualDensity(horizontal: 0, vertical: -4), minLeadingWidth: 20, title: new Text(offers[index].phone, style: TextStyle( color: Colors.black, )), leading: SizedBox( width: 10.0, child:Icon( Icons.add_ic_call_rounded, color: const Color(0xFF273A48), )) ) ), FlatButton( padding: EdgeInsets.all(0.0), materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, onPressed: () { Navigator.of(context).pushReplacement(MaterialPageRoute( builder: (context) => Business(id:offers[index].id), )); }, child:ListTile( dense:true, contentPadding: EdgeInsets.only(left: 0.0, right: 0.0), visualDensity: VisualDensity(horizontal: 0, vertical: -4), minLeadingWidth: 20, title: new Text("View Business Profile", style: TextStyle( color: Colors.black, )), leading: SizedBox( width: 10.0, child:Icon( Icons.account_circle_outlined, color: const Color(0xFF273A48), )) ), ) ], ), new Divider(), ], ), ); }, itemCount: offers.length )) ], )), ), ], ), ), ); } return new Container( child: new Stack( children: [ body, ], ), ); } }