diff --git a/lib/purchase.dart b/lib/purchase.dart index 8aa5439..820e2e9 100644 --- a/lib/purchase.dart +++ b/lib/purchase.dart @@ -24,8 +24,6 @@ class PurchaseScreen extends StatefulWidget { class PurchaseScreenState extends BaseState { - 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 extends BaseState { Map user; String card = ''; - String integerPart = '', fractionalPart = ''; String loyality = ''; @override Widget getScreenContent() { @@ -66,39 +63,22 @@ class PurchaseScreenState extends BaseState { 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)); + return new TextField( + keyboardType: TextInputType.number, + decoration: new InputDecoration.collapsed( + hintText: getHint(), + hintStyle: new TextStyle(color: greyTextColor, fontSize: 16.0) + ), + controller: controller, + onSubmitted: (String text) { + setState(() { + controller.text = _parseSum(text); + }); + }, + textAlign: TextAlign.center, + autofocus: true, + ); } getLoyality(String url) async { @@ -131,24 +111,44 @@ class PurchaseScreenState extends BaseState { } } - _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); + + 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 temporaryInteger + '.' + temporaryFractional; + 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} руб?'),