计算精度修复+长度限制
This commit is contained in:
parent
95ef2676c0
commit
114699f015
@ -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,12 +184,12 @@ class _RecordPageState extends State<RecordPage>
|
|||||||
_pendingOperation = newOperator;
|
_pendingOperation = newOperator;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else if(_inputValue != null) {
|
} else if (_inputValue != null) {
|
||||||
// 正常设置运算符
|
// 正常设置运算符
|
||||||
setState(() {
|
setState(() {
|
||||||
_pendingOperation = operator;
|
_pendingOperation = operator;
|
||||||
_pendingValue = _inputValue!;
|
_pendingValue = _inputValue!;
|
||||||
_inputValue = null;
|
_inputValue = null;
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
HapticFeedback.heavyImpact();
|
HapticFeedback.heavyImpact();
|
||||||
@ -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();
|
||||||
|
|||||||
@ -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.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user