计算精度修复+长度限制

This commit is contained in:
ddshi 2024-12-24 13:07:00 +08:00
parent 95ef2676c0
commit 114699f015
2 changed files with 33 additions and 18 deletions

View File

@ -3,6 +3,7 @@ import 'package:flutter/services.dart';
import '../models/record.dart'; import '../models/record.dart';
import '../data/categories.dart'; import '../data/categories.dart';
import 'package:uuid/uuid.dart'; import 'package:uuid/uuid.dart';
import 'package:decimal/decimal.dart';
class RecordPage extends StatefulWidget { class RecordPage extends StatefulWidget {
final Record? record; final Record? record;
@ -31,6 +32,8 @@ class _RecordPageState extends State<RecordPage>
String? _pendingValue; // String? _pendingValue; //
String? _inputValue; // String? _inputValue; //
final d = (String s) => Decimal.parse(s);
@override @override
void initState() { void initState() {
super.initState(); super.initState();
@ -83,7 +86,7 @@ class _RecordPageState extends State<RecordPage>
} }
if (_amount <= 0) { if (_amount <= 0) {
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('输入金额')), const SnackBar(content: Text('输入金额请大于0')),
); );
return; return;
} }
@ -101,7 +104,7 @@ class _RecordPageState extends State<RecordPage>
Navigator.of(context).pop(); Navigator.of(context).pop();
} }
/// ///
String get _displayAmount { String get _displayAmount {
if (_pendingOperation != null || if (_pendingOperation != null ||
_pendingValue != null || _pendingValue != null ||
@ -144,17 +147,18 @@ class _RecordPageState extends State<RecordPage>
void _handleDelete() { void _handleDelete() {
setState(() { setState(() {
if (_inputValue != null) { if (_inputValue != null) {
if(_inputValue!.length > 1){ if (_inputValue!.length > 1) {
_inputValue = _inputValue!.substring(0, _inputValue!.length - 1); _inputValue = _inputValue!.substring(0, _inputValue!.length - 1);
}else{ } else {
_inputValue = null; _inputValue = null;
} }
} else if (_pendingOperation != null) { } else if (_pendingOperation != null) {
_pendingOperation = null; _pendingOperation = null;
} else if (_pendingValue != null) { } else if (_pendingValue != null) {
if(_pendingValue!.length > 1){ if (_pendingValue!.length > 1) {
_pendingValue = _pendingValue!.substring(0, _pendingValue!.length - 1); _pendingValue =
}else{ _pendingValue!.substring(0, _pendingValue!.length - 1);
} else {
_pendingValue = null; _pendingValue = null;
} }
} else { } else {
@ -171,7 +175,6 @@ class _RecordPageState extends State<RecordPage>
final newOperator = operator; final newOperator = operator;
_calculateResult(); _calculateResult();
// //
setState(() { setState(() {
// 0 // 0
@ -181,7 +184,7 @@ class _RecordPageState extends State<RecordPage>
_pendingOperation = newOperator; _pendingOperation = newOperator;
} }
}); });
} else if(_inputValue != null) { } else if (_inputValue != null) {
// //
setState(() { setState(() {
_pendingOperation = operator; _pendingOperation = operator;
@ -195,22 +198,25 @@ class _RecordPageState extends State<RecordPage>
/// ///
void _calculateResult() { void _calculateResult() {
if (_pendingOperation != null &&
double result = 0;
if(_pendingValue == null && _inputValue== null){
return;
}
else if (_pendingOperation != null &&
_pendingValue != null && _pendingValue != null &&
_inputValue != null) { _inputValue != null) {
final currentValue = double.parse(_inputValue!);
double result;
switch (_pendingOperation) { switch (_pendingOperation) {
case '+': case '+':
result = double.parse(_pendingValue!) + currentValue; result = (d(_pendingValue!) + d(_inputValue!)).toDouble();
if (result == 0) { if (result == 0) {
_resetToDefault(); _resetToDefault();
return; return;
} }
break; break;
case '-': case '-':
result = double.parse(_pendingValue!) - currentValue; result = (d(_pendingValue!) - d(_inputValue!)).toDouble();
if (result <= 0) { if (result <= 0) {
_resetToDefault(); _resetToDefault();
return; return;
@ -219,15 +225,17 @@ class _RecordPageState extends State<RecordPage>
default: default:
return; return;
} }
} else {
result = double.parse(_inputValue ?? _pendingValue!);
}
setState(() { setState(() {
// //
_pendingValue = _formatNumberForDisplay(result); _pendingValue = _formatNumberForDisplay(result);
_amount = result; _amount = result;
_pendingOperation = null; _pendingOperation = null;
_inputValue = null; _inputValue = null;
}); });
}
} }
/// ///
@ -260,6 +268,12 @@ class _RecordPageState extends State<RecordPage>
/// ///
void _onKeyboardButtonPressed(String value) { void _onKeyboardButtonPressed(String value) {
if (_inputBuffer.length >= 20 && value != '删除' && value != '保存'&& value != 'again'){
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('输入数字过大')),
);
return;
}
switch (value) { switch (value) {
case '删除': case '删除':
_handleDelete(); _handleDelete();

View File

@ -35,6 +35,7 @@ dependencies:
uuid: ^4.3.3 # 添加uuid包用于生成唯一ID uuid: ^4.3.3 # 添加uuid包用于生成唯一ID
sqflite: ^2.3.2 # 添加 SQLite 支持 sqflite: ^2.3.2 # 添加 SQLite 支持
path: ^1.8.3 # 用于处理文件路径 path: ^1.8.3 # 用于处理文件路径
decimal: ^3.0.0 # 处理计算精度
# The following adds the Cupertino Icons font to your application. # The following adds the Cupertino Icons font to your application.