diff --git a/example/pubspec.lock b/example/pubspec.lock index 24f250c..37fc44c 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -65,14 +65,6 @@ packages: url: "https://pub.dev" source: hosted version: "1.0.8" - decimal: - dependency: transitive - description: - name: decimal - sha256: fc706a5618b81e5b367b01dd62621def37abc096f2b46a9bd9068b64c1fa36d0 - url: "https://pub.dev" - source: hosted - version: "3.2.4" equatable: dependency: transitive description: @@ -175,14 +167,6 @@ packages: url: "https://pub.dev" source: hosted version: "4.1.2" - intl: - dependency: transitive - description: - name: intl - sha256: "3df61194eb431efc39c4ceba583b95633a403f46c9fd341e550ce0bfa50e9aa5" - url: "https://pub.dev" - source: hosted - version: "0.20.2" leak_tracker: dependency: transitive description: @@ -295,14 +279,6 @@ packages: url: "https://pub.dev" source: hosted version: "2.2.0" - rational: - dependency: transitive - description: - name: rational - sha256: cb808fb6f1a839e6fc5f7d8cb3b0a10e1db48b3be102de73938c627f0b636336 - url: "https://pub.dev" - source: hosted - version: "2.2.3" record_use: dependency: transitive description: diff --git a/lib/src/extensions/num_ext.dart b/lib/src/extensions/num_ext.dart index c142d10..507e999 100644 --- a/lib/src/extensions/num_ext.dart +++ b/lib/src/extensions/num_ext.dart @@ -1,9 +1,3 @@ -import 'package:decimal/decimal.dart'; - -extension DoubleExtensions on double { - Decimal toDecimal() => Decimal.parse(toString()); -} - extension ListDoubleExtensions on Iterable { double sum() => fold(0.0, (sum, curr) => sum + curr); } diff --git a/lib/src/layout/resizable_layout.dart b/lib/src/layout/resizable_layout.dart index cc6087e..26ec351 100644 --- a/lib/src/layout/resizable_layout.dart +++ b/lib/src/layout/resizable_layout.dart @@ -1,4 +1,3 @@ -import 'package:decimal/decimal.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/widgets.dart'; @@ -173,7 +172,7 @@ class ResizableLayoutRenderObject extends RenderBox final constraints = switch (size) { ResizableSizeExpand() => layoutDirection.copyConstraintsWith( this.constraints, - expandSizes[i ~/ 2]!.toDouble(), + expandSizes[i ~/ 2]!, ), _ => _getChildConstraints( size: size, @@ -200,57 +199,53 @@ class ResizableLayoutRenderObject extends RenderBox onComplete(finalSizes); } - Map _getExpandSizes(double availableSpace) { + Map _getExpandSizes(double availableSpace) { bool isExpand(ResizableSize size) => size is ResizableSizeExpand; - var expandIndices = _sizes.indicesWhere(isExpand).toList(); + final expandIndices = _sizes.indicesWhere(isExpand).toList(); if (expandIndices.isEmpty) { return {}; } - final allocatedSpace = Map.fromIterable( - expandIndices, - value: (_) => Decimal.zero, - ); + final allocatedSpace = { + for (final index in expandIndices) index: 0.0, + }; - var remainingFlex = _getFlexCount().toDecimal(); - var remainingSpace = availableSpace.toDecimal(); - var shouldContinue = true; + var remainingFlex = _getFlexCount(); + var remainingSpace = availableSpace; - do { + // Bounded to defend against floating-point residue that could otherwise + // keep `didChange` true forever. Each pass either removes at least one + // clamped item or makes a final redistribution; the natural termination + // pass adds one more iteration. + final maxIterations = expandIndices.length + 2; + + for (var iter = 0; iter < maxIterations && remainingFlex > 0; iter++) { var didChange = false; final toRemove = []; - final targetDeltaPerFlex = (remainingSpace / remainingFlex).toDecimal( - scaleOnInfinitePrecision: 6, - ); + final perFlex = remainingSpace / remainingFlex; for (final index in expandIndices) { - final size = _sizes[index]; - - if (size is ResizableSizeExpand) { - final flex = size.flex.toDecimal(); - final currentValue = allocatedSpace[index] ?? Decimal.zero; - final targetDelta = targetDeltaPerFlex * flex; - final targetSize = (currentValue + targetDelta).toDouble(); - final clampedValue = _clamp(targetSize, size).toDecimal(); - - if (clampedValue != currentValue) { - final difference = clampedValue - currentValue; - remainingSpace -= difference; - allocatedSpace[index] = clampedValue; - didChange = true; - } else { - remainingFlex -= flex; - toRemove.add(index); - } + final size = _sizes[index] as ResizableSizeExpand; + final currentValue = allocatedSpace[index]!; + final targetSize = currentValue + perFlex * size.flex; + final clampedValue = _clamp(targetSize, size); + + if (clampedValue != currentValue) { + remainingSpace -= clampedValue - currentValue; + allocatedSpace[index] = clampedValue; + didChange = true; + } else { + remainingFlex -= size.flex; + toRemove.add(index); } } expandIndices.removeWhere(toRemove.contains); - shouldContinue = didChange && remainingFlex > Decimal.zero; - } while (shouldContinue); + if (!didChange) break; + } return allocatedSpace; } diff --git a/pubspec.yaml b/pubspec.yaml index 8d95d39..1b55465 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -10,7 +10,6 @@ environment: dependencies: flutter: sdk: flutter - decimal: ^3.2.4 equatable: ^2.0.8 dev_dependencies: