计算精度修复+长度限制
This commit is contained in:
parent
95ef2676c0
commit
114699f015
@ -3,6 +3,7 @@ import 'package:flutter/services.dart';
|
||||
import '../models/record.dart';
|
||||
import '../data/categories.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
import 'package:decimal/decimal.dart';
|
||||
|
||||
class RecordPage extends StatefulWidget {
|
||||
final Record? record;
|
||||
@ -31,6 +32,8 @@ class _RecordPageState extends State<RecordPage>
|
||||
String? _pendingValue; // 等待计算的第一个值
|
||||
String? _inputValue; // 当前输入的值
|
||||
|
||||
final d = (String s) => Decimal.parse(s);
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
@ -83,7 +86,7 @@ class _RecordPageState extends State<RecordPage>
|
||||
}
|
||||
if (_amount <= 0) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('请输入金额')),
|
||||
const SnackBar(content: Text('输入金额请大于0')),
|
||||
);
|
||||
return;
|
||||
}
|
||||
@ -101,7 +104,7 @@ class _RecordPageState extends State<RecordPage>
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
|
||||
/// 格式化显示金额
|
||||
/// 显示金额
|
||||
String get _displayAmount {
|
||||
if (_pendingOperation != null ||
|
||||
_pendingValue != null ||
|
||||
@ -144,17 +147,18 @@ class _RecordPageState extends State<RecordPage>
|
||||
void _handleDelete() {
|
||||
setState(() {
|
||||
if (_inputValue != null) {
|
||||
if(_inputValue!.length > 1){
|
||||
if (_inputValue!.length > 1) {
|
||||
_inputValue = _inputValue!.substring(0, _inputValue!.length - 1);
|
||||
}else{
|
||||
} else {
|
||||
_inputValue = null;
|
||||
}
|
||||
} else if (_pendingOperation != null) {
|
||||
_pendingOperation = null;
|
||||
} else if (_pendingValue != null) {
|
||||
if(_pendingValue!.length > 1){
|
||||
_pendingValue = _pendingValue!.substring(0, _pendingValue!.length - 1);
|
||||
}else{
|
||||
if (_pendingValue!.length > 1) {
|
||||
_pendingValue =
|
||||
_pendingValue!.substring(0, _pendingValue!.length - 1);
|
||||
} else {
|
||||
_pendingValue = null;
|
||||
}
|
||||
} else {
|
||||
@ -171,7 +175,6 @@ class _RecordPageState extends State<RecordPage>
|
||||
final newOperator = operator;
|
||||
_calculateResult();
|
||||
|
||||
|
||||
// 计算完成后,设置新的运算符
|
||||
setState(() {
|
||||
// 如果计算导致重置(结果为0),不添加新的运算符
|
||||
@ -181,12 +184,12 @@ class _RecordPageState extends State<RecordPage>
|
||||
_pendingOperation = newOperator;
|
||||
}
|
||||
});
|
||||
} else if(_inputValue != null) {
|
||||
} else if (_inputValue != null) {
|
||||
// 正常设置运算符
|
||||
setState(() {
|
||||
_pendingOperation = operator;
|
||||
_pendingValue = _inputValue!;
|
||||
_inputValue = null;
|
||||
_inputValue = null;
|
||||
});
|
||||
} else {
|
||||
HapticFeedback.heavyImpact();
|
||||
@ -195,22 +198,25 @@ class _RecordPageState extends State<RecordPage>
|
||||
|
||||
/// 计算结果
|
||||
void _calculateResult() {
|
||||
if (_pendingOperation != null &&
|
||||
|
||||
double result = 0;
|
||||
if(_pendingValue == null && _inputValue== null){
|
||||
return;
|
||||
}
|
||||
else if (_pendingOperation != null &&
|
||||
_pendingValue != null &&
|
||||
_inputValue != null) {
|
||||
final currentValue = double.parse(_inputValue!);
|
||||
double result;
|
||||
|
||||
|
||||
switch (_pendingOperation) {
|
||||
case '+':
|
||||
result = double.parse(_pendingValue!) + currentValue;
|
||||
result = (d(_pendingValue!) + d(_inputValue!)).toDouble();
|
||||
if (result == 0) {
|
||||
_resetToDefault();
|
||||
return;
|
||||
}
|
||||
break;
|
||||
case '-':
|
||||
result = double.parse(_pendingValue!) - currentValue;
|
||||
result = (d(_pendingValue!) - d(_inputValue!)).toDouble();
|
||||
if (result <= 0) {
|
||||
_resetToDefault();
|
||||
return;
|
||||
@ -219,15 +225,17 @@ class _RecordPageState extends State<RecordPage>
|
||||
default:
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
result = double.parse(_inputValue ?? _pendingValue!);
|
||||
}
|
||||
|
||||
setState(() {
|
||||
setState(() {
|
||||
// 格式化结果,去掉不必要的小数位
|
||||
_pendingValue = _formatNumberForDisplay(result);
|
||||
_amount = result;
|
||||
_pendingOperation = null;
|
||||
_inputValue = null;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// 重置到默认状态
|
||||
@ -260,6 +268,12 @@ class _RecordPageState extends State<RecordPage>
|
||||
|
||||
/// 修改键盘按钮处理逻辑
|
||||
void _onKeyboardButtonPressed(String value) {
|
||||
if (_inputBuffer.length >= 20 && value != '删除' && value != '保存'&& value != 'again'){
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('输入数字过大')),
|
||||
);
|
||||
return;
|
||||
}
|
||||
switch (value) {
|
||||
case '删除':
|
||||
_handleDelete();
|
||||
|
||||
@ -35,6 +35,7 @@ dependencies:
|
||||
uuid: ^4.3.3 # 添加uuid包用于生成唯一ID
|
||||
sqflite: ^2.3.2 # 添加 SQLite 支持
|
||||
path: ^1.8.3 # 用于处理文件路径
|
||||
decimal: ^3.0.0 # 处理计算精度
|
||||
|
||||
|
||||
# The following adds the Cupertino Icons font to your application.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user