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> {
|
||||
|
||||
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;
|
||||
@@ -34,7 +32,6 @@ class PurchaseScreenState<T> extends BaseState<PurchaseScreen> {
|
||||
|
||||
Map user;
|
||||
String card = '';
|
||||
String integerPart = '', fractionalPart = '';
|
||||
String loyality = '';
|
||||
|
||||
@override Widget getScreenContent() {
|
||||
@@ -66,39 +63,22 @@ class PurchaseScreenState<T> extends BaseState<PurchaseScreen> {
|
||||
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)),
|
||||
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));
|
||||
onSubmitted: (String text) {
|
||||
setState(() {
|
||||
controller.text = _parseSum(text);
|
||||
});
|
||||
},
|
||||
textAlign: TextAlign.center,
|
||||
autofocus: true,
|
||||
);
|
||||
}
|
||||
|
||||
getLoyality(String url) async {
|
||||
@@ -131,24 +111,44 @@ class PurchaseScreenState<T> extends BaseState<PurchaseScreen> {
|
||||
}
|
||||
}
|
||||
|
||||
_buildSum() {
|
||||
String temporaryInteger = integerPart;
|
||||
String temporaryFractional = fractionalPart;
|
||||
String _cleanupNumber(String text){
|
||||
String tmp = text
|
||||
.replaceAll(' ', '')
|
||||
.replaceAll('-', '')
|
||||
.replaceAll(',', '.')
|
||||
.replaceAll('..', '.');
|
||||
|
||||
while (temporaryFractional.length < 2) {
|
||||
temporaryFractional = temporaryFractional + '0';
|
||||
while(tmp.indexOf('..') != -1){
|
||||
tmp = tmp.replaceAll('..', '.');
|
||||
}
|
||||
return tmp;
|
||||
}
|
||||
|
||||
if (temporaryInteger.length == 0) {
|
||||
temporaryInteger = '0';
|
||||
}
|
||||
_parseSum(String input) {
|
||||
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) {
|
||||
String val = _buildSum();
|
||||
print(val);
|
||||
String val = _parseSum(controller.text);
|
||||
showDialog(context: context, child: new AlertDialog(
|
||||
title: new Text('Подтверждение'),
|
||||
content: new Text('Вы подтверждаете покупку на ${val} руб?'),
|
||||
|
||||
Reference in New Issue
Block a user