rework purchase sum parsing (was broken on Flutter revision f2af347363, Dart version 1.25.0-dev.7.0, refs #9991
This commit is contained in:
@@ -24,8 +24,6 @@ class PurchaseScreen extends StatefulWidget {
|
|||||||
|
|
||||||
class PurchaseScreenState<T> extends BaseState<PurchaseScreen> {
|
class PurchaseScreenState<T> extends BaseState<PurchaseScreen> {
|
||||||
|
|
||||||
RegExp moneyRegexp = new RegExp(r'''^(?!0\.00)\d{1,11}(\.\d{0,2})?$''');
|
|
||||||
|
|
||||||
PurchaseScreenState(String userString, String card) {
|
PurchaseScreenState(String userString, String card) {
|
||||||
this.user = JSON.decode(userString);
|
this.user = JSON.decode(userString);
|
||||||
this.card = card;
|
this.card = card;
|
||||||
@@ -34,7 +32,6 @@ class PurchaseScreenState<T> extends BaseState<PurchaseScreen> {
|
|||||||
|
|
||||||
Map user;
|
Map user;
|
||||||
String card = '';
|
String card = '';
|
||||||
String integerPart = '', fractionalPart = '';
|
|
||||||
String loyality = '';
|
String loyality = '';
|
||||||
|
|
||||||
@override Widget getScreenContent() {
|
@override Widget getScreenContent() {
|
||||||
@@ -66,39 +63,22 @@ class PurchaseScreenState<T> extends BaseState<PurchaseScreen> {
|
|||||||
return Colors.white;
|
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() {
|
@override getTextWidget() {
|
||||||
return new TextField(keyboardType: TextInputType.number, decoration: new InputDecoration.collapsed(hintText: getHint(),
|
return new TextField(
|
||||||
hintStyle: new TextStyle(color: greyTextColor, fontSize: 16.0)),
|
keyboardType: TextInputType.number,
|
||||||
|
decoration: new InputDecoration.collapsed(
|
||||||
|
hintText: getHint(),
|
||||||
|
hintStyle: new TextStyle(color: greyTextColor, fontSize: 16.0)
|
||||||
|
),
|
||||||
controller: controller,
|
controller: controller,
|
||||||
onChanged: (text) => handleUserInput(text));
|
onSubmitted: (String text) {
|
||||||
|
setState(() {
|
||||||
|
controller.text = _parseSum(text);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
autofocus: true,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
getLoyality(String url) async {
|
getLoyality(String url) async {
|
||||||
@@ -131,24 +111,44 @@ class PurchaseScreenState<T> extends BaseState<PurchaseScreen> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_buildSum() {
|
String _cleanupNumber(String text){
|
||||||
String temporaryInteger = integerPart;
|
String tmp = text
|
||||||
String temporaryFractional = fractionalPart;
|
.replaceAll(' ', '')
|
||||||
|
.replaceAll('-', '')
|
||||||
|
.replaceAll(',', '.')
|
||||||
|
.replaceAll('..', '.');
|
||||||
|
|
||||||
while (temporaryFractional.length < 2) {
|
while(tmp.indexOf('..') != -1){
|
||||||
temporaryFractional = temporaryFractional + '0';
|
tmp = tmp.replaceAll('..', '.');
|
||||||
|
}
|
||||||
|
return tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (temporaryInteger.length == 0) {
|
_parseSum(String input) {
|
||||||
temporaryInteger = '0';
|
num sumTotal = 0.0;
|
||||||
}
|
String text = _cleanupNumber(input);
|
||||||
|
|
||||||
return temporaryInteger + '.' + temporaryFractional;
|
try {
|
||||||
|
sumTotal = num.parse(text);
|
||||||
|
} catch(exception, stacktrace) {
|
||||||
|
print(exception);
|
||||||
|
try {
|
||||||
|
int idx = text.indexOf('.');
|
||||||
|
String integerPart = text.substring(0, idx);
|
||||||
|
String fractionalPart = text.substring(idx + 1, text.length);
|
||||||
|
if(fractionalPart.length > 2) {
|
||||||
|
fractionalPart = fractionalPart.substring(0, 2);
|
||||||
|
}
|
||||||
|
return '${integerPart}.${fractionalPart}';
|
||||||
|
} catch(exception, stacktrace){
|
||||||
|
print(exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sumTotal.toStringAsFixed(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
onPurchaseClick(BuildContext context) {
|
onPurchaseClick(BuildContext context) {
|
||||||
String val = _buildSum();
|
String val = _parseSum(controller.text);
|
||||||
print(val);
|
|
||||||
showDialog(context: context, child: new AlertDialog(
|
showDialog(context: context, child: new AlertDialog(
|
||||||
title: new Text('Подтверждение'),
|
title: new Text('Подтверждение'),
|
||||||
content: new Text('Вы подтверждаете покупку на ${val} руб?'),
|
content: new Text('Вы подтверждаете покупку на ${val} руб?'),
|
||||||
|
|||||||
Reference in New Issue
Block a user