elimino una reevaluación de los inputs
This commit is contained in:
parent
d58a6cda8f
commit
82d29022a6
12 changed files with 154 additions and 27 deletions
|
|
@ -26,12 +26,14 @@ class D4rtEditingController extends TextEditingController {
|
|||
String? _lastError;
|
||||
|
||||
String? get lastError => _lastError;
|
||||
Number? _lastValue;
|
||||
|
||||
D4rtEditingController({String? text}) : super(text: text);
|
||||
|
||||
bool validate() {
|
||||
try {
|
||||
FormulaEvaluator.evaluateExpression(text);
|
||||
_lastValue = null;
|
||||
_lastValue = FormulaEvaluator.evaluateExpression(text);
|
||||
_lastError = null;
|
||||
return true;
|
||||
} catch (e) {
|
||||
|
|
@ -40,6 +42,8 @@ class D4rtEditingController extends TextEditingController {
|
|||
}
|
||||
}
|
||||
|
||||
get d4rtValue => _lastValue;
|
||||
|
||||
@override
|
||||
set text(String newText) {
|
||||
super.text = newText;
|
||||
|
|
@ -90,9 +94,11 @@ class _FormulaScreenState extends State<FormulaScreen> {
|
|||
try {
|
||||
final inputValues = <String, dynamic>{};
|
||||
for (final input in widget.formula.input) {
|
||||
final text = _inputControllers[input.name]!.text;
|
||||
//final value = double.tryParse(text) ?? 0.0;
|
||||
final value = FormulaEvaluator.evaluateExpression(text);
|
||||
final controller = _inputControllers[input.name]!;
|
||||
if( controller.d4rtValue == null ){
|
||||
throw FormulaEvaluationException( "Field ${input.name} is invalid" );
|
||||
}
|
||||
final value = controller.d4rtValue;
|
||||
|
||||
// Convert input to base unit if needed
|
||||
// Always convert from dropdown unit to variable's base unit
|
||||
|
|
|
|||
|
|
@ -156,7 +156,7 @@ class Corpus{
|
|||
}
|
||||
}
|
||||
|
||||
static dynamic _evaluate(String code, [D4rt? interpreter]) {
|
||||
static Number _evaluate(String code, [D4rt? interpreter]) {
|
||||
final d4rtInterpreter = interpreter ?? FormulaEvaluator.createDefaultInterpreter();
|
||||
FormulaEvaluator.prepareInterpreter(d4rtInterpreter);
|
||||
final completeCode = "${FormulaEvaluator.d4rtImports}\n$code";
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ Where:
|
|||
"description": r'''
|
||||
Newton's law of universal gravitation
|
||||
|
||||
$$F = G\\frac{m_1m_2}{r^2}$$
|
||||
\(F = G\\frac{m_1m_2}{r^2}\)
|
||||
|
||||
Where:
|
||||
- $G$: Gravitational constant ($6.674\\times 10^{-11}\\ \\mathrm{N\\cdot m^2/kg^2}$)
|
||||
|
|
|
|||
|
|
@ -62,8 +62,7 @@ class FormulaEvaluator {
|
|||
final d4rtInterpreter = interpreter ?? createDefaultInterpreter();
|
||||
prepareInterpreter(d4rtInterpreter);
|
||||
final d4rtCode = """
|
||||
import 'dart:math';
|
||||
import "package:d4rt_formulas.dart";
|
||||
${d4rtImports}
|
||||
main()
|
||||
{
|
||||
late var result;
|
||||
|
|
|
|||
|
|
@ -6,6 +6,10 @@
|
|||
|
||||
#include "generated_plugin_registrant.h"
|
||||
|
||||
#include <url_launcher_linux/url_launcher_plugin.h>
|
||||
|
||||
void fl_register_plugins(FlPluginRegistry* registry) {
|
||||
g_autoptr(FlPluginRegistrar) url_launcher_linux_registrar =
|
||||
fl_plugin_registry_get_registrar_for_plugin(registry, "UrlLauncherPlugin");
|
||||
url_launcher_plugin_register_with_registrar(url_launcher_linux_registrar);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
#
|
||||
|
||||
list(APPEND FLUTTER_PLUGIN_LIST
|
||||
url_launcher_linux
|
||||
)
|
||||
|
||||
list(APPEND FLUTTER_FFI_PLUGIN_LIST
|
||||
|
|
|
|||
|
|
@ -5,6 +5,8 @@
|
|||
import FlutterMacOS
|
||||
import Foundation
|
||||
|
||||
import url_launcher_macos
|
||||
|
||||
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
|
||||
UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
|
||||
}
|
||||
|
|
|
|||
32
pubspec.lock
32
pubspec.lock
|
|
@ -125,10 +125,10 @@ packages:
|
|||
dependency: "direct main"
|
||||
description:
|
||||
name: d4rt
|
||||
sha256: f731c2027db3e1365fd2162d30a41ccbfe01d8b75901deba5fa167306d2fcf31
|
||||
sha256: "1eb626145e2ed97332f9e6e842e626f973d3969ce30e2794efb4744bd8aeba63"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "0.1.4"
|
||||
version: "0.1.7"
|
||||
equatable:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
|
@ -170,10 +170,10 @@ packages:
|
|||
dependency: "direct main"
|
||||
description:
|
||||
name: flutter_d4rt
|
||||
sha256: "1ccdc72ff87e3ecd8b10824523f7a586860393b631b2351bd913f7d72969c776"
|
||||
sha256: "7ab9d3f91de5c10db115ccab1d9b1588946f744850f5bacc243cfc9041dd0a4c"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "0.0.3"
|
||||
version: "0.0.5"
|
||||
flutter_highlight:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
|
@ -186,18 +186,18 @@ packages:
|
|||
dependency: "direct dev"
|
||||
description:
|
||||
name: flutter_lints
|
||||
sha256: "5398f14efa795ffb7a33e9b6a08798b26a180edac4ad7db3f231e40f82ce11e1"
|
||||
sha256: "3105dc8492f6183fb076ccf1f351ac3d60564bff92e20bfc4af9cc1651f4e7e1"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "5.0.0"
|
||||
version: "6.0.0"
|
||||
flutter_markdown:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: flutter_markdown
|
||||
sha256: "04c4722cc36ec5af38acc38ece70d22d3c2123c61305d555750a091517bbe504"
|
||||
sha256: "08fb8315236099ff8e90cb87bb2b935e0a724a3af1623000a9cec930468e0f27"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "0.6.23"
|
||||
version: "0.7.7+1"
|
||||
flutter_test:
|
||||
dependency: "direct dev"
|
||||
description: flutter
|
||||
|
|
@ -284,10 +284,10 @@ packages:
|
|||
dependency: transitive
|
||||
description:
|
||||
name: leak_tracker
|
||||
sha256: "8dcda04c3fc16c14f48a7bb586d4be1f0d1572731b6d81d51772ef47c02081e0"
|
||||
sha256: "33e2e26bdd85a0112ec15400c8cbffea70d0f9c3407491f672a2fad47915e2de"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "11.0.1"
|
||||
version: "11.0.2"
|
||||
leak_tracker_flutter_testing:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
|
@ -316,10 +316,10 @@ packages:
|
|||
dependency: transitive
|
||||
description:
|
||||
name: lints
|
||||
sha256: c35bb79562d980e9a453fc715854e1ed39e24e7d0297a880ef54e17f9874a9d7
|
||||
sha256: a5e2b223cb7c9c8efdc663ef484fdd95bb243bff242ef5b13e26883547fce9a0
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "5.1.1"
|
||||
version: "6.0.0"
|
||||
logging:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
|
@ -412,10 +412,10 @@ packages:
|
|||
dependency: transitive
|
||||
description:
|
||||
name: pool
|
||||
sha256: "20fe868b6314b322ea036ba325e6fc0711a22948856475e2c2b6306e8ab39c2a"
|
||||
sha256: "978783255c543aa3586a1b3c21f6e9d720eb315376a915872c61ef8b5c20177d"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.5.1"
|
||||
version: "1.5.2"
|
||||
pub_semver:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
|
@ -657,10 +657,10 @@ packages:
|
|||
dependency: transitive
|
||||
description:
|
||||
name: watcher
|
||||
sha256: "5bf046f41320ac97a469d506261797f35254fa61c641741ef32dacda98b7d39c"
|
||||
sha256: "592ab6e2892f67760543fb712ff0177f4ec76c031f02f5b4ff8d3fc5eb9fb61a"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.1.3"
|
||||
version: "1.1.4"
|
||||
web:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
|
|
|||
|
|
@ -33,11 +33,11 @@ dependencies:
|
|||
|
||||
# The following adds the Cupertino Icons font to your application.
|
||||
# Use with the CupertinoIcons class for iOS style icons.
|
||||
cupertino_icons: ^1.0.8
|
||||
cupertino_icons:
|
||||
resource_portable:
|
||||
d4rt:
|
||||
flutter_d4rt:
|
||||
flutter_markdown: ^0.6.0
|
||||
flutter_markdown:
|
||||
flutter_code_editor:
|
||||
|
||||
collection: any
|
||||
|
|
@ -50,7 +50,7 @@ dev_dependencies:
|
|||
# activated in the `analysis_options.yaml` file located at the root of your
|
||||
# package. See that file for information about deactivating specific lint
|
||||
# rules and activating additional ones.
|
||||
flutter_lints: ^5.0.0
|
||||
flutter_lints: ^6.0.0
|
||||
test: ^1.26.3
|
||||
|
||||
# For information on the generic Dart part of this file, see the
|
||||
|
|
|
|||
111
units-and-formulas-definition.md
Normal file
111
units-and-formulas-definition.md
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
# Units and Formulas Definition Format
|
||||
|
||||
## Units Specification
|
||||
|
||||
Units are defined in `.d4rt.units` files using JSON arrays. Each unit can have:
|
||||
|
||||
```dart
|
||||
{
|
||||
"name": "unit name", // Full name (required)
|
||||
"symbol": "abbr", // Symbol (required)
|
||||
"isBase": true, // Mark as base unit (exclusive with baseUnit)
|
||||
"baseUnit": "parent", // Reference unit for conversions
|
||||
"factor": 1.0, // Conversion factor to base unit
|
||||
"toBase": "x * 1000", // Conversion code to base (expression)
|
||||
"fromBase": "x / 1000" // Conversion code from base (expression)
|
||||
}
|
||||
```
|
||||
|
||||
### Key Rules:
|
||||
1. Use either `isBase` OR `baseUnit`+conversion (factor or code)
|
||||
2. Factor-based units use simple multiplicative conversions
|
||||
3. Code-based units require both `toBase` and `fromBase` Dart expressions
|
||||
4. Code expressions:
|
||||
- Use `x` as input variable
|
||||
- Must return a numeric value
|
||||
- Can use math functions via `dart:math`
|
||||
|
||||
### Examples:
|
||||
**Simple Factor-based:**
|
||||
```json
|
||||
{
|
||||
"name": "kilometer",
|
||||
"symbol": "km",
|
||||
"baseUnit": "meter",
|
||||
"factor": 1000
|
||||
}
|
||||
```
|
||||
|
||||
**Code-based Conversion:**
|
||||
```json
|
||||
{
|
||||
"name": "Fahrenheit",
|
||||
"symbol": "°F",
|
||||
"baseUnit": "Kelvin",
|
||||
"toBase": "(x - 32) * 5/9 + 273.15",
|
||||
"fromBase": "(x - 273.15) * 9/5 + 32"
|
||||
}
|
||||
```
|
||||
|
||||
## Formulas Specification
|
||||
|
||||
Formulas are defined in `.d4rt` files using JSON arrays. Each formula has:
|
||||
|
||||
```dart
|
||||
{
|
||||
"name": "Formula Name", // Required
|
||||
"description": "Markdown", // Optional
|
||||
"input": [ // List of input variables
|
||||
{
|
||||
"name": "varName", // Variable identifier
|
||||
"unit": "unitName" // Base unit for calculations
|
||||
}
|
||||
],
|
||||
"output": { // Single output variable
|
||||
"name": "resultVar",
|
||||
"unit": "outputUnit"
|
||||
},
|
||||
"d4rtCode": "Dart code", // Calculation logic
|
||||
"tags": ["physics", "energy"] // Categories
|
||||
}
|
||||
```
|
||||
|
||||
### Formula Code Rules:
|
||||
1. Input variables are declared as `final`
|
||||
2. Output variable must be assigned
|
||||
3. Can use:
|
||||
- Basic math operators
|
||||
- `dart:math` functions
|
||||
- Custom functions from FormulaEvaluator
|
||||
4. Example:
|
||||
```dart
|
||||
// Inputs: m (kg), v (m/s)
|
||||
d4rtCode: "KE = 0.5 * m * pow(v, 2);"
|
||||
```
|
||||
|
||||
### Conversion Code Examples
|
||||
|
||||
**Expression-style:**
|
||||
```dart
|
||||
// Simple factor conversion
|
||||
final x = 5; // Value in source unit
|
||||
main() => x * 1000; // Convert to base unit
|
||||
```
|
||||
|
||||
**Statement-style:**
|
||||
```dart
|
||||
// Complex conversion with multiple steps
|
||||
final x = 212; // Fahrenheit
|
||||
main() {
|
||||
var celsius = (x - 32) * 5/9;
|
||||
return celsius + 273.15; // Convert to Kelvin
|
||||
}
|
||||
```
|
||||
|
||||
## Validation Rules
|
||||
|
||||
1. Units must form a DAG (no circular dependencies)
|
||||
2. Formulas must declare all input variables
|
||||
3. Output variable must be assigned in d4rtCode
|
||||
4. Unit conversion code must handle numeric inputs
|
||||
5. Base units must exist before derived units
|
||||
|
|
@ -6,6 +6,9 @@
|
|||
|
||||
#include "generated_plugin_registrant.h"
|
||||
|
||||
#include <url_launcher_windows/url_launcher_windows.h>
|
||||
|
||||
void RegisterPlugins(flutter::PluginRegistry* registry) {
|
||||
UrlLauncherWindowsRegisterWithRegistrar(
|
||||
registry->GetRegistrarForPlugin("UrlLauncherWindows"));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
#
|
||||
|
||||
list(APPEND FLUTTER_PLUGIN_LIST
|
||||
url_launcher_windows
|
||||
)
|
||||
|
||||
list(APPEND FLUTTER_FFI_PLUGIN_LIST
|
||||
|
|
|
|||
Loading…
Reference in a new issue