/
main.dart
368 lines (336 loc) · 11.4 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:mca_official_flutter_sdk/mca_official_flutter_sdk.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home:
// const ContentForm()
const MyHomePage(title: 'MyCover SDK Test'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var allProducts;
String userEmail = 'dami@mycovergenius.com';
String publicKey = 'MCAPUBK_TEST|48c01008-5f01-4705-b63f-e71ef5fc974f';
initialiseSdk(context, {productId}) async {
final myCover = MyCoverAI(
context: context,
publicKey: publicKey,
email: userEmail,
productId: [productId],
form: {
'email': userEmail,
'name': '',
'phone': ''
},
paymentOption: PaymentOption.gateway,
transactionType: TransactionType.purchase);
var response = await myCover.initialise();
if (response != null) {
showLoading('$response');
} else {
print("No Response!");
}
}
@override
initState() {
super.initState();
getAllProducts();
}
Future<void> showLoading(String message) {
return showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return AlertDialog(
content: Container(
margin: const EdgeInsets.fromLTRB(30, 20, 30, 20),
width: double.infinity,
height: 50,
child: Text(message),
),
);
},
);
}
static makePostRequest({apiUrl, data, token}) async {
final uri = Uri.parse(apiUrl);
final jsonString = json.encode(data);
var headers = {
HttpHeaders.contentTypeHeader: 'application/json',
HttpHeaders.authorizationHeader: 'Bearer $token',
};
return await http.post(uri, body: jsonString, headers: headers);
}
static const String productUrl =
'https://api.mycover.ai/v1/sdk/initialize';
getAllProducts() async {
var data = {"payment_option": 'gateway', "action": "purchase"};
try {
var res = await makePostRequest(
apiUrl: productUrl, data: data, token: publicKey);
print("Server Responded with $res");
print(res.body);
if (res.statusCode! >= 200 && res.statusCode < 300) {
var body = jsonDecode(res.body);
setState(() => allProducts = body['data']['productDetails']);
}
} catch (e) {
return e.toString();
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Buy product')),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
Expanded(
child: allProducts == null
? const Center(
child: CircularProgressIndicator.adaptive(
backgroundColor: Colors.green))
: ListView.separated(
itemBuilder: (c, i) {
var item = allProducts[i];
return ListTile(
leading: const Icon(Icons.store_mall_directory),
title: Text(item['name']),
subtitle: Text(item['name']),
trailing: Text(
item['is_dynamic_pricing']
? '${item['price']}%'
: 'NGN ${item['price']}',
style: const TextStyle(
fontWeight: FontWeight.w600, fontSize: 16),
),
onTap: () => initialiseSdk(context,
productId: item['id']));
},
separatorBuilder: (c, i) => const SizedBox(height: 5),
itemCount: allProducts.length),
),
],
),
));
}
}
class ContentForm extends StatefulWidget {
const ContentForm({Key? key}) : super(key: key);
@override
State<ContentForm> createState() => _ContentFormState();
}
class _ContentFormState extends State<ContentForm> {
var contentFormField = <Widget>[];
final contentTitleController = <TextEditingController>[];
final contentDescController = <TextEditingController>[];
Widget contentFormCard() {
var _contentTitleController = TextEditingController();
contentTitleController.add(_contentTitleController);
var _contentDescController = TextEditingController();
contentDescController.add(_contentDescController);
return Row(
children: [
Expanded(
child: InputFormField(
label: "Object",
controller: _contentTitleController,
// validator: (value) => FieldValidator.validate(value),
),
),
const SizedBox(width: 10),
Expanded(
child: InputFormField(
label: "How many",
controller: _contentDescController,
// validator: (value) => FieldValidator.validate(value),
),
),
],
);
}
@override
void initState() {
contentFormField.add(contentFormCard());
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(30.0),
child: Column(
children: [
Stack(
children: [
Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey, width: 1.5),
borderRadius: BorderRadius.circular(10),
shape: BoxShape.rectangle),
padding: const EdgeInsets.all(10),
margin: const EdgeInsets.symmetric(vertical: 10),
child: Scrollbar(
child: ListView.builder(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemCount: contentFormField.length,
itemBuilder: (BuildContext context, int index) {
return Row(
children: [
Expanded(child: contentFormField[index]),
const SizedBox(width: 5),
if (contentFormField.length > 1)
InkWell(
onTap: () {
contentFormField.removeAt(index);
setState(() {});
},
child: const Icon(Icons.remove_circle,
size: 20, color: Colors.red),
),
],
);
},
),
),
),
Positioned(
bottom: 0,
right: 8,
child: InkWell(
onTap: () => setState(
() => contentFormField.add(contentFormCard())),
child: Container(
decoration: BoxDecoration(
color: Colors.green,
borderRadius: BorderRadius.circular(3)),
child: Padding(
padding: const EdgeInsets.fromLTRB(3, 1, 3, 1),
child: Row(
children: const [
Icon(Icons.add_circle,
size: 15, color: Colors.white),
Padding(
padding: EdgeInsets.all(4.0),
child: Text(
'Add Item',
style: TextStyle(
fontSize: 9,
fontWeight: FontWeight.w400,
color: Colors.white),
)),
],
),
)),
)),
],
),
],
),
),
),
);
}
}
class InputFormField extends StatelessWidget {
const InputFormField({
this.suffixIcon,
this.prefixIcon,
this.validator,
this.obscure = false,
this.onChanged,
this.enabled = true,
this.controller,
this.label,
this.hint,
this.textCapitalization = TextCapitalization.none,
this.maxLines = 1,
this.padding,
this.keyboardType,
this.maxLength,
this.inputFormatters,
});
final suffixIcon;
final prefixIcon;
final onChanged;
final validator;
final enabled;
final controller;
final obscure;
final label;
final hint;
final padding;
final textCapitalization;
final keyboardType;
final inputFormatters;
final maxLines;
final maxLength;
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.fromLTRB(0, 10, 0, 12),
child: TextFormField(
buildCounter: (context,
{required currentLength, required isFocused, maxLength}) =>
null,
inputFormatters: inputFormatters,
enabled: enabled,
controller: controller,
keyboardType: keyboardType,
textCapitalization: textCapitalization,
validator: validator,
onChanged: onChanged,
obscureText: obscure,
maxLines: maxLines,
maxLength: maxLength,
decoration: InputDecoration(
filled: true,
border: InputBorder.none,
enabledBorder: const OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(4))),
disabledBorder: const OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(4))),
focusedBorder: const OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(4))),
errorBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
borderSide: const BorderSide(color: Colors.redAccent, width: 0.3),
),
focusedErrorBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(4),
),
suffixIcon: suffixIcon,
prefixIcon: prefixIcon,
labelText: label,
hintText: hint,
contentPadding:
EdgeInsets.symmetric(vertical: padding ?? 15.0, horizontal: 15.0),
),
),
);
}
}