dcoupon_sdk_flutter_plugin 0.0.2 dcoupon_sdk_flutter_plugin: ^0.0.2 copied to clipboard
dcoupon sdk flutter plugin project. Support flutter (Android/iOS) applications to integrate dcoupon web services for flutter projects.
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:dcoupon_sdk_flutter_plugin/dcoupon_listener.dart';
import 'package:dcoupon_sdk_flutter_plugin/dcoupon_sdk_flutter_plugin.dart';
import 'package:dcoupon_sdk_flutter_plugin_example/app_config.dart';
import 'package:dcoupon_sdk_flutter_plugin_example/translations.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'constants.dart';
void main({String env}) async {
// load config
final config = await AppConfig.forEnvironment(env);
runApp(ExampleApp(config: config));
}
class ExampleApp extends StatelessWidget {
// This widget is the root of your example application.
final AppConfig config;
ExampleApp({Key key, this.config}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
),
localizationsDelegates: [
const TranslationsDelegate(),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: [
const Locale('en'),
const Locale('es'),
],
debugShowCheckedModeBanner: false,
home: HomePage(config: config));
}
}
class HomePage extends StatefulWidget {
final AppConfig config;
HomePage({Key key, this.config}) : super(key: key);
@override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<HomePage> {
_LoginPageState();
Dcoupon dcoupon;
bool layoutLoginVisible = true, layoutLogged = false;
var email, externalId, alias, gender, birthdate;
var _formKey = GlobalKey<FormState>();
String _responseMsg;
String couponId = '0',
start = '0',
limit = '0',
sortBy,
promotionToken,
latitude,
longitude,
source,
transId,
crmId,
textSearch,
zipcode,
radius,
orderBy;
List<String> promotionTokens, categoryIds, storeIds, retailerTokens, companyTokens;
bool startVisible = false,
limitVisible = false,
sortByVisible = false,
couponIdVisible = false,
promotionTokensVisible = false,
promotionTokenVisible = false,
longitudeVisible = false,
latitudeVisible = false,
sourceVisible = false,
transIdVisible = false,
crmIdVisible = false,
categoryIdsVisible = false,
storeIdsVisible = false,
textSearchVisible = false,
zipcodeVisible = false,
radiusVisible = false,
retailerTokensVisible = false,
companyTokensVisible = false,
orderByVisible = false;
Future<String> loadString() async{
var contents = await rootBundle.loadString("lib/assets/config/dev.json");
return contents;
}
@override
initState() {
super.initState();
dcoupon = new Dcoupon(context);
loadString().then((onValue){
dcoupon.loadConfig(onValue);
});
print('Config: ' +
"\nclientToken->" +
widget.config.clientToken +
"\nclientSecretKey->" +
widget.config.clientSecretKey +
"\nclientCountry->" +
widget.config.clientCountry +
"\nclientDomain->" +
widget.config.clientDomain +
"\nclientPublisherApiToken->" +
widget.config.clientPublisherApiToken);
email = "flutter-example@email.com";
externalId = "flutter-example-externalId";
alias = "flutter-example-alias";
gender = "MALE";
birthdate = "1990-12-25";
}
void _printMessage(String method, DcouponListener response) {
JsonEncoder encoder = JsonEncoder.withIndent(' ');
setState(() {
_responseMsg = method + ' -> ' + response.code.toString();
try {
var prettyString = encoder.convert(jsonDecode(response.message));
prettyString.split('\n').forEach((element) => _responseMsg += '\n' + element);
} catch (ex) {
_responseMsg += '\n' + response.message;
}
});
}
Future<void> login() async {
// do login
dcoupon.login(email, externalId, alias: alias, gender: gender, birthdate: birthdate).then((response) {
_printMessage('login', response);
if (dcoupon.isLogged()) {
print("example.app.isLogged->" + dcoupon.isLogged().toString());
}
}).catchError((ex) {
print("example.app.login.catchError: " + ex.toString());
});
}
@override
void setState(fn) {
super.setState(fn);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: new Center(
child: _bodyForm(),
),
);
}
String _currentMethod;
void _changedDrowDownCurrentMethod(String itemSelected) {
setState(() {
_currentMethod = itemSelected;
startVisible = false;
limitVisible = false;
sortByVisible = false;
couponIdVisible = false;
promotionTokensVisible = false;
promotionTokenVisible = false;
longitudeVisible = false;
latitudeVisible = false;
sourceVisible = false;
transIdVisible = false;
crmIdVisible = false;
categoryIdsVisible = false;
storeIdsVisible = false;
textSearchVisible = false;
zipcodeVisible = false;
radiusVisible = false;
retailerTokensVisible = false;
companyTokensVisible = false;
orderByVisible = false;
switch (_currentMethod) {
case Constants.GET_COUPONS:
startVisible = true;
limitVisible = true;
sortByVisible = true;
break;
case Constants.GET_COUPON_DETAIL:
couponIdVisible = true;
break;
case Constants.ACTIVATE_COUPON:
couponIdVisible = true;
break;
case Constants.DEACTIVATE_COUPON:
couponIdVisible = true;
break;
case Constants.GET_REDEMPTION_CODE:
break;
case Constants.CREATE_COUPONS:
promotionTokensVisible = true;
longitudeVisible = true;
latitudeVisible = true;
sourceVisible = true;
transIdVisible = true;
crmIdVisible = true;
break;
case Constants.GET_FILTERS:
startVisible = true;
limitVisible = true;
break;
case Constants.GET_PROMOTIONS:
startVisible = true;
limitVisible = true;
categoryIdsVisible = true;
storeIdsVisible = true;
longitudeVisible = true;
latitudeVisible = true;
textSearchVisible = true;
retailerTokensVisible = true;
companyTokensVisible = true;
break;
case Constants.GET_PROMOTION_DETAIL:
promotionTokenVisible = true;
break;
case Constants.GET_STORES:
startVisible = true;
limitVisible = true;
promotionTokenVisible = true;
zipcodeVisible = true;
radiusVisible = true;
latitudeVisible = true;
longitudeVisible = true;
retailerTokensVisible = true;
orderByVisible = true;
break;
default:
break;
}
});
}
void _launchMethod() {
setState(() {
_responseMsg = "";
try {
couponId = int.parse(couponId).toString();
} catch (ex) {
couponId = '0';
}
try {
start = int.parse(start).toString();
} catch (ex) {
start = '0';
}
try {
limit = int.parse(limit).toString();
} catch (ex) {
limit = '0';
}
switch (_currentMethod) {
case Constants.GET_COUPONS:
dcoupon.getCoupons(start: int.parse(start), limit: int.parse(limit), sortBy: sortBy).then((response) {
_printMessage(Constants.GET_COUPONS, response);
}).catchError((ex) {
print("example.app.getCoupons.catchError: " + ex.toString());
});
break;
case Constants.GET_COUPON_DETAIL:
dcoupon.getCouponDetail(int.parse(couponId)).then((response) {
_printMessage(Constants.GET_COUPON_DETAIL, response);
}).catchError((ex) {
print("example.app.getCouponDetail.catchError: " + ex.toString());
});
break;
case Constants.ACTIVATE_COUPON:
dcoupon.activateCoupon(int.parse(couponId)).then((response) {
_printMessage(Constants.ACTIVATE_COUPON, response);
}).catchError((ex) {
print("example.app.activateCoupon.catchError: " + ex.toString());
});
break;
case Constants.DEACTIVATE_COUPON:
dcoupon.deactivateCoupon(int.parse(couponId)).then((response) {
_printMessage(Constants.DEACTIVATE_COUPON, response);
}).catchError((ex) {
print("example.app.deactivateCoupon.catchError: " + ex.toString());
});
break;
case Constants.GET_REDEMPTION_CODE:
dcoupon.getRedemptionCode().then((response) {
_printMessage(Constants.GET_REDEMPTION_CODE, response);
}).catchError((ex) {
print("example.app.getRedemptionCode.catchError: " + ex.toString());
});
break;
case Constants.CREATE_COUPONS:
dcoupon
.createCoupons(promotionTokens,
latitude: latitude, longitude: longitude, source: source, transId: transId, crmId: crmId)
.then((response) {
_printMessage(Constants.CREATE_COUPONS, response);
}).catchError((ex) {
print("example.app.createCoupons.catchError: " + ex.toString());
});
break;
case Constants.GET_FILTERS:
dcoupon.getFilters(start: int.parse(start), limit: int.parse(limit)).then((response) {
_printMessage(Constants.GET_FILTERS, response);
}).catchError((ex) {
print("example.app.getFilters.catchError: " + ex.toString());
});
break;
case Constants.GET_PROMOTIONS:
dcoupon
.getPromotions(
start: int.parse(start),
limit: int.parse(limit),
longitude: longitude,
latitude: latitude,
zipcode: zipcode,
retailerTokens: retailerTokens,
orderBy: orderBy,
radius: radius,
textSearch: textSearch,
companyTokens: companyTokens,
storeIds: storeIds,
categoryIds: categoryIds)
.then((response) {
_printMessage(Constants.GET_PROMOTIONS, response);
}).catchError((ex) {
print("example.app.getPromotions.catchError: " + ex.toString());
});
break;
case Constants.GET_PROMOTION_DETAIL:
dcoupon.getPromotionDetail(promotionToken).then((response) {
_printMessage(Constants.GET_PROMOTION_DETAIL, response);
}).catchError((ex) {
print("dcoupon.getPromotionDetail.catchError: " + ex.toString());
});
break;
case Constants.GET_STORES:
dcoupon
.getStores(
start: int.parse(start),
limit: int.parse(limit),
promotionToken: promotionToken,
retailerTokens: retailerTokens,
zipcode: zipcode,
latitude: latitude,
longitude: longitude,
radius: radius)
.then((response) {
_printMessage(Constants.GET_STORES, response);
}).catchError((ex) {
print("example.app.getStores.catchError: " + ex.toString());
});
break;
default:
break;
}
});
}
Widget _bodyForm() {
return SingleChildScrollView(
scrollDirection: Axis.vertical,
padding: EdgeInsets.all(8.0),
child: Column(
children: <Widget>[
new Image(
fit: BoxFit.scaleDown,
image: new AssetImage('lib/assets/images/example_dcoupon_logo.png'),
),
Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
child: Container(
child: Column(
children: <Widget>[
TextFormField(
initialValue: email,
decoration: InputDecoration(labelText: Translations.of(context).text('example_email')),
onChanged: (text) {
email = text;
},
validator: (value) {
if (value.isEmpty) {
return (Translations.of(context).text('example_missed_parameter') +
" " +
Translations.of(context).text('example_email'));
}
return null;
},
),
TextFormField(
initialValue: externalId,
decoration:
InputDecoration(labelText: Translations.of(context).text('example_externalId')),
onChanged: (text) {
externalId = text;
},
validator: (value) {
if (value.isEmpty) {
return (Translations.of(context).text('example_missed_parameter') +
" " +
Translations.of(context).text('example_externalId'));
}
return null;
},
),
TextFormField(
initialValue: alias,
decoration: InputDecoration(labelText: Translations.of(context).text('example_alias')),
onChanged: (text) {
alias = text;
},
validator: (value) {
if (value.isEmpty) {
return (Translations.of(context).text('example_missed_parameter') +
" " +
Translations.of(context).text('example_alias'));
}
return null;
},
),
TextFormField(
initialValue: gender,
decoration: InputDecoration(labelText: Translations.of(context).text('example_gender')),
onChanged: (text) {
gender = text;
},
validator: (value) {
if (value.isEmpty) {
return (Translations.of(context).text('example_missed_parameter') +
" " +
Translations.of(context).text('example_gender'));
}
return null;
},
),
TextFormField(
initialValue: birthdate,
decoration:
InputDecoration(labelText: Translations.of(context).text('example_birthdate')),
onChanged: (text) {
birthdate = text;
},
validator: (value) {
if (value.isEmpty) {
return (Translations.of(context).text('example_missed_parameter') +
" " +
Translations.of(context).text('example_birthdate'));
}
return null;
},
),
],
),
),
)
],
),
Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
onPressed: login,
color: Colors.blue,
child: Row(
children: <Widget>[Text(Translations.of(context).text('example_button_login'))],
),
)
],
),
Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
child: Container(
child: Column(
children: <Widget>[
Visibility(
visible: startVisible,
child: TextFormField(
initialValue: start,
decoration: InputDecoration(labelText: 'start'),
onChanged: (text) {
start = text;
},
),
),
Visibility(
visible: limitVisible,
child: TextFormField(
initialValue: limit,
decoration: InputDecoration(labelText: 'limit'),
onChanged: (text) {
limit = text;
},
),
),
Visibility(
visible: sortByVisible,
child: TextFormField(
initialValue: sortBy,
decoration: InputDecoration(labelText: 'sortBy'),
onChanged: (text) {
sortBy = text;
},
),
),
Visibility(
visible: couponIdVisible,
child: TextFormField(
initialValue: couponId,
decoration: InputDecoration(labelText: 'couponId'),
onChanged: (text) {
couponId = text;
},
),
),
Visibility(
visible: promotionTokensVisible,
child: TextFormField(
initialValue: promotionTokens == null ? '' : promotionTokens.join(","),
decoration: InputDecoration(labelText: 'promotionTokens'),
onChanged: (text) {
promotionTokens = text.split(",");
},
),
),
Visibility(
visible: promotionTokenVisible,
child: TextFormField(
initialValue: promotionToken,
decoration: InputDecoration(labelText: 'promotionToken'),
onChanged: (text) {
promotionToken = text;
},
),
),
Visibility(
visible: longitudeVisible,
child: TextFormField(
initialValue: longitude,
decoration: InputDecoration(labelText: 'longitude'),
onChanged: (text) {
longitude = text;
},
),
),
Visibility(
visible: latitudeVisible,
child: TextFormField(
initialValue: latitude,
decoration: InputDecoration(labelText: 'latitude'),
onChanged: (text) {
latitude = text;
},
),
),
Visibility(
visible: sourceVisible,
child: TextFormField(
initialValue: source,
decoration: InputDecoration(labelText: 'source'),
onChanged: (text) {
source = text;
},
),
),
Visibility(
visible: transIdVisible,
child: TextFormField(
initialValue: transId,
decoration: InputDecoration(labelText: 'transId'),
onChanged: (text) {
transId = text;
},
),
),
Visibility(
visible: crmIdVisible,
child: TextFormField(
initialValue: crmId,
decoration: InputDecoration(labelText: 'crmId'),
onChanged: (text) {
crmId = text;
},
),
),
Visibility(
visible: categoryIdsVisible,
child: TextFormField(
initialValue: categoryIds == null ? '0' : categoryIds.join(","),
decoration: InputDecoration(labelText: 'categoryIds'),
onChanged: (text) {
categoryIds = text.split(",");
},
),
),
Visibility(
visible: storeIdsVisible,
child: TextFormField(
initialValue: storeIds == null ? '' : storeIds.join(","),
decoration: InputDecoration(labelText: 'storeIds'),
onChanged: (text) {
storeIds = text.split(",");
},
),
),
Visibility(
visible: textSearchVisible,
child: TextFormField(
initialValue: textSearch,
decoration: InputDecoration(labelText: 'textSearch'),
onChanged: (text) {
textSearch = text;
},
),
),
Visibility(
visible: zipcodeVisible,
child: TextFormField(
initialValue: zipcode,
decoration: InputDecoration(labelText: 'zipcode'),
onChanged: (text) {
zipcode = text;
},
),
),
Visibility(
visible: radiusVisible,
child: TextFormField(
initialValue: radius,
decoration: InputDecoration(labelText: 'radius'),
onChanged: (text) {
radius = text;
},
),
),
Visibility(
visible: retailerTokensVisible,
child: TextFormField(
initialValue: retailerTokens == null ? '' : retailerTokens.join(","),
decoration: InputDecoration(labelText: 'radius'),
onChanged: (text) {
retailerTokens = text.split(",");
},
),
),
Visibility(
visible: companyTokensVisible,
child: TextFormField(
initialValue: companyTokens == null ? '' : companyTokens.join(","),
decoration: InputDecoration(labelText: 'companyTokens'),
onChanged: (text) {
companyTokens = text.split(",");
},
),
),
Visibility(
visible: orderByVisible,
child: TextFormField(
initialValue: orderBy,
decoration: InputDecoration(labelText: 'orderBy'),
onChanged: (text) {
orderBy = text;
},
),
),
],
),
),
)
],
),
Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
child: Container(
child: Column(
children: <Widget>[
new DropdownButton(
isExpanded: true,
hint: new Text(Translations.of(context).text('example_select_hint')),
value: _currentMethod,
items: [
DropdownMenuItem(
value: Constants.GET_COUPONS,
child: Text(Constants.GET_COUPONS),
),
DropdownMenuItem(
value: Constants.GET_COUPON_DETAIL,
child: Text(Constants.GET_COUPON_DETAIL),
),
DropdownMenuItem(
value: Constants.ACTIVATE_COUPON,
child: Text(Constants.ACTIVATE_COUPON),
),
DropdownMenuItem(
value: Constants.DEACTIVATE_COUPON,
child: Text(Constants.DEACTIVATE_COUPON),
),
DropdownMenuItem(
value: Constants.GET_REDEMPTION_CODE,
child: Text(Constants.GET_REDEMPTION_CODE),
),
DropdownMenuItem(
value: Constants.CREATE_COUPONS,
child: Text(Constants.CREATE_COUPONS),
),
DropdownMenuItem(
value: Constants.GET_FILTERS,
child: Text(Constants.GET_FILTERS),
),
DropdownMenuItem(
value: Constants.GET_PROMOTIONS,
child: Text(Constants.GET_PROMOTIONS),
),
DropdownMenuItem(
value: Constants.GET_PROMOTION_DETAIL,
child: Text(Constants.GET_PROMOTION_DETAIL),
),
DropdownMenuItem(
value: Constants.GET_STORES,
child: Text(Constants.GET_STORES),
),
],
onChanged: _changedDrowDownCurrentMethod,
),
RaisedButton(
onPressed: _launchMethod,
color: Colors.blue,
child: Row(
children: <Widget>[
Text(Translations.of(context).text('example_button_launch_method'))
],
),
)
],
),
),
)
],
),
Row(
children: <Widget>[
Expanded(
child: Container(
child: Column(
children: <Widget>[],
),
),
)
],
),
Row(
children: <Widget>[
Expanded(
child: Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SelectableText.rich(
TextSpan(text: _responseMsg, style: TextStyle(color: Colors.black, fontSize: 14))),
],
),
),
)
],
)
],
)),
],
),
);
}
}