204 lines
6.0 KiB
Dart
204 lines
6.0 KiB
Dart
import 'package:flutter/material.dart';
|
||
import 'package:flutter/services.dart';
|
||
import 'dart:convert';
|
||
import 'dart:async';
|
||
import 'dart:core';
|
||
|
||
import 'main.dart';
|
||
import 'common.dart';
|
||
import 'consts.dart';
|
||
import 'network.dart';
|
||
import 'base_state.dart';
|
||
import 'purchase_success.dart';
|
||
|
||
/// Экран проведения покупки.
|
||
class PurchaseScreen extends StatefulWidget {
|
||
|
||
PurchaseScreen(this.user, this.card);
|
||
|
||
String user;
|
||
String card;
|
||
|
||
@override State createState() => new PurchaseScreenState<PurchaseScreen>(user, card);
|
||
}
|
||
|
||
class PurchaseScreenState<T> extends BaseState<PurchaseScreen> {
|
||
|
||
RegExp moneyRegexp = new RegExp(r'''^(?!0\.00)\d{1,11}(\.\d{0,2})?$''');
|
||
|
||
PurchaseScreenState(String userString, String card) {
|
||
this.user = JSON.decode(userString);
|
||
this.card = card;
|
||
getLoyality(user['loyalty_url']);
|
||
}
|
||
|
||
Map user;
|
||
String card = '';
|
||
String integerPart = '', fractionalPart = '';
|
||
String loyality = '';
|
||
|
||
@override Widget getScreenContent() {
|
||
return new Column(
|
||
children: <Widget>[new Expanded(child: new ListView(children: <Widget>[
|
||
getValueWithTitle('ФИО', user['first_name'] == null ? '' : user['first_name']),
|
||
getValueWithTitle('Карта', card),
|
||
getValueWithTitle('Вознаграждение', loyality),
|
||
getHintLabel(),
|
||
getDecoratedTextWidget(),
|
||
buildButton(new EdgeInsets.only(top: 36.0, left: buttonVerticalMargin, right: buttonVerticalMargin), buildRaisedButton(context, 'ЗАВЕРШИТЬ ПОКУПКУ', () => onPurchaseClick(context))),
|
||
buildButton(new EdgeInsets.only(top: 24.0, left: buttonVerticalMargin, right: buttonVerticalMargin), buildFlatButton(context, 'СКАНИРОВАТЬ', primaryColor))
|
||
]))]);
|
||
}
|
||
|
||
@override String getTitle() {
|
||
return "Проведение покупки";
|
||
}
|
||
|
||
@override getHint() {
|
||
return 'Сумма';
|
||
}
|
||
|
||
@overide getMenuButtons(BuildContext context) {
|
||
return <Widget>[getFaqButton(), getLogoutButton()];
|
||
}
|
||
|
||
@override Color getTextFilledBackground() {
|
||
return Colors.white;
|
||
}
|
||
|
||
/// Смена состояния экрана при изменении текста в поле ввода.
|
||
@override handleUserInput(String tmpString) {
|
||
|
||
setState(() {
|
||
|
||
if (tmpString.length == 0 || moneyRegexp.hasMatch(tmpString)) {
|
||
if (tmpString.contains('.')) {
|
||
int dotIndex = tmpString.indexOf('.');
|
||
integerPart = tmpString.substring(0, dotIndex);
|
||
fractionalPart = tmpString.substring(dotIndex + 1, tmpString.length);
|
||
if (fractionalPart.length > 2) {
|
||
fractionalPart = fractionalPart.substring(0, 2);
|
||
}
|
||
controller.text = '${integerPart}.${fractionalPart}';
|
||
} else {
|
||
integerPart = tmpString;
|
||
controller.text = tmpString;
|
||
}
|
||
textFieldValue = tmpString;
|
||
|
||
} else {
|
||
tmpString = tmpString.substring(0, tmpString.length - 1);
|
||
controller.text = tmpString.replaceAll('a', '');
|
||
textFieldValue = tmpString;
|
||
}
|
||
});
|
||
}
|
||
|
||
@override getTextWidget() {
|
||
return new TextField(keyboardType: TextInputType.number, decoration: new InputDecoration.collapsed(hintText: getHint(),
|
||
hintStyle: new TextStyle(color: greyTextColor, fontSize: 16.0)),
|
||
controller: controller,
|
||
onChanged: (text) => handleUserInput(text));
|
||
}
|
||
|
||
getLoyality(String url) async {
|
||
|
||
if (await platform.invokeMethod('isOnline')) {
|
||
|
||
var headers = {
|
||
'DM-Authorization': 'dmapptoken $appToken',
|
||
'Authorization': 'dmtoken ${token}'
|
||
};
|
||
|
||
httpClient.get(url, headers: headers).then((response) {
|
||
|
||
print(response.body);
|
||
|
||
Map bonuses = JSON.decode(response.body);
|
||
String type = bonuses['type'];
|
||
setState(() {
|
||
if (type == 'amount') {
|
||
this.loyality = '${user['discount']}%';
|
||
} else {
|
||
List amountToBonus = bonuses['amount_to_bonus'];
|
||
double loyalityVal = (double.parse(amountToBonus[1]) / amountToBonus[0]) * 100;
|
||
this.loyality = '${loyalityVal.toStringAsFixed(0)}%';
|
||
}
|
||
});
|
||
}).catchError((error) {
|
||
print(error.toString());
|
||
});
|
||
}
|
||
}
|
||
|
||
_buildSum() {
|
||
String temporaryInteger = integerPart;
|
||
String temporaryFractional = fractionalPart;
|
||
|
||
while (temporaryFractional.length < 2) {
|
||
temporaryFractional = temporaryFractional + '0';
|
||
}
|
||
|
||
if (temporaryInteger.length == 0) {
|
||
temporaryInteger = '0';
|
||
}
|
||
|
||
return temporaryInteger + '.' + temporaryFractional;
|
||
}
|
||
|
||
onPurchaseClick(BuildContext context) {
|
||
String val = _buildSum();
|
||
print(val);
|
||
showDialog(context: context, child: new AlertDialog(
|
||
title: new Text('Подтверждение'),
|
||
content: new Text('Вы подтверждаете покупку на ${val} руб?'),
|
||
actions: <Widget>[
|
||
new FlatButton(
|
||
child: new Text('Нет'),
|
||
onPressed: () {
|
||
Navigator.of(context).pop();
|
||
},
|
||
),
|
||
new FlatButton(
|
||
child: new Text('Да'),
|
||
onPressed: () {
|
||
purchase(val);
|
||
},
|
||
)
|
||
]));
|
||
}
|
||
|
||
purchase(String sum_total) async {
|
||
|
||
if (await platform.invokeMethod('isOnline')) {
|
||
|
||
platform.invokeMethod('getDocID').then((result) {
|
||
|
||
String url = user['purchases_url'];
|
||
|
||
var body = {
|
||
'doc_id': result,
|
||
'curr_iso_code': '643',
|
||
'commit': 'true',
|
||
'sum_total': sum_total
|
||
};
|
||
|
||
var headers = {
|
||
'DM-Authorization': 'dmapptoken $appToken',
|
||
'Authorization': 'dmtoken ${token}'
|
||
};
|
||
|
||
httpClient.post(url, body: body, headers: headers).then((response) {
|
||
|
||
print(response.body);
|
||
Navigator.of(context).pop();
|
||
pushRoute(context, new PurchaseSuccessScreen(sum_total, user['first_name'] == null ? '' : user['first_name']));
|
||
|
||
}).catchError((error) {
|
||
print(error.toString());
|
||
});
|
||
});
|
||
}
|
||
}
|
||
}
|