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 'view_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) { return Scaffold( resizeToAvoidBottomInset: false, 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("Discounts & 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).pushReplacement(MaterialPageRoute( builder: (context) => MyApp(), )); }, ), backgroundColor: Colors.white, elevation: 0.0, ), body: Stack( children: [SingleChildScrollView( child: Column( children: [ Padding( padding: EdgeInsets.all(10), child:Row( children: [ Text( 'Sponsored Adsense', style: new TextStyle(color: Colors.blue,fontWeight: FontWeight.bold),), Spacer(), FlatButton( padding: EdgeInsets.all(10.0), onPressed: () { Navigator.of(context).pushReplacement(MaterialPageRoute( builder: (context) => Advertise(latitude:latitude,longitude:longitude), )); }, child:Text( 'Advertise', style: new TextStyle(color: Colors.green,fontWeight: FontWeight.bold),) ) ] ) ), ads!=null?Column( children: add_ads()):Container( padding: EdgeInsets.all(20), child: Center(child: SizedBox( width: 10, height: 10, child: CircularProgressIndicator(valueColor: new AlwaysStoppedAnimation(Colors.blue),) )) //if is searching then show "Please wait" //else show search peopels text ), new SizedBox( height: 20.0, ), Padding( padding: EdgeInsets.all(10), child:Align( alignment: (Alignment.topLeft), child:Text( 'Discounts & Offers', style: new TextStyle(color: Colors.blue,fontWeight: FontWeight.bold),)) ), offers!=null?Column( children: add_offers()):Container( padding: EdgeInsets.all(20), child: Center(child: SizedBox( width: 10, height: 10, child: CircularProgressIndicator(valueColor: new AlwaysStoppedAnimation(Colors.blue),) )) //if is searching then show "Please wait" //else show search peopels text ), ] ), )], ), ); } List add_ads(){ List adList = []; for(int i=0;i[ Align( alignment: (Alignment.topLeft), child:Text( ads[pos].title, style: new TextStyle( fontSize: 14.0, color: Colors.black87, fontWeight: FontWeight.bold), )), new SizedBox( height: 5.0, ), FlatButton( padding: EdgeInsets.all(10.0), onPressed: () { _showImageDialog(ads[pos].image); }, child:Align( alignment: (Alignment.topLeft), child:FittedBox( child: Image.network(ads[pos].image), fit: BoxFit.fill, ))), new SizedBox( height: 5.0, ), Align( alignment: (Alignment.topLeft), child:new Text( ads[pos].desc, style: new TextStyle( fontSize: 12.0, color: Colors.black54, fontWeight: FontWeight.normal), )) ,] ), ), ); } List add_offers(){ List offerList = []; for(int i=0;i[ new SizedBox( height: 10.0, ), Align( alignment: (Alignment.topLeft), child:new Text( offers[pos].title, style: new TextStyle( fontSize: 14.0, color: Colors.black87, fontWeight: FontWeight.bold), )), new SizedBox( height: 10.0, ), FlatButton( padding: EdgeInsets.all(10.0), onPressed: () { _showImageDialog(offers[pos].image); }, child:Align( alignment: (Alignment.topLeft), child:FittedBox( child: Image.network(offers[pos].image), fit: BoxFit.fill,)) ), new SizedBox( height: 10.0, ), Align( alignment: (Alignment.topLeft), child:new Text( offers[pos].desc, style: new TextStyle( fontSize: 12.0, color: Colors.black54, fontWeight: FontWeight.normal), )), new SizedBox( height: 10.0, ), Align( alignment: (Alignment.topLeft), child:new Text( offers[pos].name, style: new TextStyle( fontSize: 14.0, color: Colors.blue, ), )), new SizedBox( height: 5.0, ), Align( alignment: (Alignment.topLeft), child:new Text( offers[pos].location, style: new TextStyle( fontSize: 14.0, color: Colors.black, ), )), new SizedBox( height: 10.0, ), Align( alignment: (Alignment.topLeft), child:FlatButton( padding: EdgeInsets.all(0.0), materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, onPressed: () { launch("tel://"+offers[pos].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[pos].phone, style: TextStyle( color: Colors.black, )), leading: SizedBox( width: 10.0, child:Icon( Icons.add_ic_call_rounded, color: const Color(0xFF273A48), )) ) )), Align( alignment: (Alignment.topLeft), child:FlatButton( padding: EdgeInsets.all(0.0), materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, onPressed: () { Navigator.of(context).pushReplacement(MaterialPageRoute( builder: (context) => Business(id:offers[pos].id,back:2), )); }, 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), )) ), )),] )); } void _showImageDialog(String url) async { await Navigator.of(context).push(new MaterialPageRoute( builder: (BuildContext context) { return 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 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.white), //onPressed:() => Navigator.pop(context, false), onPressed: () { Navigator.pop(context); }, ), // backgroundColor: Colors.black26, elevation: 0.0, ), extendBodyBehindAppBar: true, body: Stack( children: [ Container( color:Colors.black, child:Center(child:Image.network( url, ))), ]),//Put your screen design here! ); }, fullscreenDialog: true )); } }