Skip to content

Commit

Permalink
fix: account for expand min/max bounds (#82)
Browse files Browse the repository at this point in the history
* Add tests for min/max bounds on expand children

* Implement distribution algorithm

* Fix overflow in custom divider example

* Fix bug in shrink example

* Clean up logic

* Remove indices after loop

* Improve signature of constraint copy helper

* Fix method name in tests

* Add Decimal package to help with floating-point math

* Fix layout bug with Decimals

* Split "sum" into specific extensions on Int and Double

* Add "indicesWhere" extension

* Clean up

* Fix ListIntExtension and use it in flex count

* Fix Map construction

* Fix "ListDoubleExtensions" name

* Replace usages of "fold"

* Remove remaining usages of "fold"

* Remove unnecessary parens
  • Loading branch information
andyhorn authored Dec 25, 2024
1 parent b88bcc7 commit b7506d5
Show file tree
Hide file tree
Showing 12 changed files with 363 additions and 161 deletions.
185 changes: 104 additions & 81 deletions example/lib/screens/divider/custom_divider_example_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,96 +40,119 @@ class _CustomDividerExampleScreenState
drawer: const NavDrawer(),
body: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
Wrap(
alignment: WrapAlignment.start,
crossAxisAlignment: WrapCrossAlignment.center,
runSpacing: 18,
spacing: 18,
children: [
Column(
children: [
const Text('Length'),
Slider(
min: 0.01,
max: 1.0,
value: length,
onChanged: (value) => setState(() => length = value),
),
Text('Ratio: ${(length * 100).toStringAsFixed((2))}%'),
],
SizedBox(
width: 200,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Text('Length'),
Slider(
min: 0.01,
max: 1.0,
value: length,
onChanged: (value) => setState(() => length = value),
),
Text('Ratio: ${(length * 100).toStringAsFixed((2))}%'),
],
),
),
Column(
children: [
const Text('Thickness'),
Slider(
min: 1,
max: 20.0,
divisions: 19,
value: thickness,
onChanged: (value) => setState(() => thickness = value),
),
Text('${thickness}px'),
],
SizedBox(
width: 200,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Text('Thickness'),
Slider(
min: 1,
max: 20.0,
divisions: 19,
value: thickness,
onChanged: (value) => setState(() => thickness = value),
),
Text('${thickness}px'),
],
),
),
Column(
children: [
const Text('Padding'),
Slider(
min: 0,
max: 20,
divisions: 20,
value: padding,
onChanged: (value) => setState(() => padding = value),
),
Text('${padding}px'),
],
SizedBox(
width: 200,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Text('Padding'),
Slider(
min: 0,
max: 20,
divisions: 20,
value: padding,
onChanged: (value) => setState(() => padding = value),
),
Text('${padding}px'),
],
),
),
Column(
children: [
const Text('Cross-Axis Alignment'),
DropdownButton(
value: crossAxisAlignment,
items: const [
DropdownMenuItem(
value: CrossAxisAlignment.start,
child: Text('Start'),
SizedBox(
width: 200,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Text('Cross-Axis Alignment'),
DropdownButton(
value: crossAxisAlignment,
items: const [
DropdownMenuItem(
value: CrossAxisAlignment.start,
child: Text('Start'),
),
DropdownMenuItem(
value: CrossAxisAlignment.center,
child: Text('Center'),
),
DropdownMenuItem(
value: CrossAxisAlignment.end,
child: Text('End'),
),
],
onChanged: (value) => setState(
() => crossAxisAlignment = value!,
),
DropdownMenuItem(
value: CrossAxisAlignment.center,
child: Text('Center'),
),
DropdownMenuItem(
value: CrossAxisAlignment.end,
child: Text('End'),
),
],
onChanged: (value) => setState(
() => crossAxisAlignment = value!,
),
),
],
],
),
),
Column(
children: [
const Text('Main-Axis Alignment'),
DropdownButton(
value: mainAxisAlignment,
items: const [
DropdownMenuItem(
value: MainAxisAlignment.start,
child: Text('Start'),
SizedBox(
width: 200,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Text('Main-Axis Alignment'),
DropdownButton(
value: mainAxisAlignment,
items: const [
DropdownMenuItem(
value: MainAxisAlignment.start,
child: Text('Start'),
),
DropdownMenuItem(
value: MainAxisAlignment.center,
child: Text('Center'),
),
DropdownMenuItem(
value: MainAxisAlignment.end,
child: Text('End'),
),
],
onChanged: (value) => setState(
() => mainAxisAlignment = value!,
),
DropdownMenuItem(
value: MainAxisAlignment.center,
child: Text('Center'),
),
DropdownMenuItem(
value: MainAxisAlignment.end,
child: Text('End'),
),
],
onChanged: (value) => setState(
() => mainAxisAlignment = value!,
),
),
],
],
),
),
],
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,12 @@ class _ShrinkAndFlexExampleScreenState
),
ResizableChild(
size: const ResizableSize.shrink(),
child: ColoredBox(
color: Theme.of(context).colorScheme.tertiaryContainer,
child: const SizeLabel(),
child: SizedBox(
width: 100,
child: ColoredBox(
color: Theme.of(context).colorScheme.tertiaryContainer,
child: const SizeLabel(),
),
),
),
],
Expand Down
24 changes: 24 additions & 0 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.8"
decimal:
dependency: transitive
description:
name: decimal
sha256: da8f65df568345f2738cc8b0de74971c86d2d93ce5fc8c4ec094f6b7c5d48eb5
url: "https://pub.dev"
source: hosted
version: "3.1.0"
fake_async:
dependency: transitive
description:
Expand Down Expand Up @@ -111,6 +119,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "4.0.2"
intl:
dependency: transitive
description:
name: intl
sha256: "99f282cb0e02edcbbf8c6b3bbc7c90b65635156c412e58f3975a7e55284ce685"
url: "https://pub.dev"
source: hosted
version: "0.20.0"
leak_tracker:
dependency: transitive
description:
Expand Down Expand Up @@ -199,6 +215,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.8"
rational:
dependency: transitive
description:
name: rational
sha256: cb808fb6f1a839e6fc5f7d8cb3b0a10e1db48b3be102de73938c627f0b636336
url: "https://pub.dev"
source: hosted
version: "2.2.3"
sky_engine:
dependency: transitive
description: flutter
Expand Down
17 changes: 8 additions & 9 deletions lib/src/extensions/iterable_ext.dart
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
extension IterableNumExtensions on Iterable<num> {
num sum() => fold(0, (sum, current) => sum + current);
}

extension IterableExtensions<T> on Iterable<T> {
int nullCount() => where((item) => item == null).length;

int count(bool Function(T) test) => where(test).length;

num sum(num Function(T) extractor) => fold(
0.0,
(sum, current) => sum + extractor(current),
);

Iterable<T> evenIndices() => [
for (var i = 0; i < length; i++) ...[
if (i % 2 == 0) ...[
elementAt(i),
],
],
];

Iterable<int> indicesWhere(bool Function(T) test) => [
for (var i = 0; i < length; i++) ...[
if (test(elementAt(i))) ...[
i,
],
],
];
}
13 changes: 13 additions & 0 deletions lib/src/extensions/num_ext.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:decimal/decimal.dart';

extension DoubleExtensions on double {
Decimal toDecimal() => Decimal.parse(toString());
}

extension ListDoubleExtensions on Iterable<double> {
double sum() => fold(0.0, (sum, curr) => sum + curr);
}

extension ListIntExtensions on Iterable<int> {
int sum() => fold(0, (sum, curr) => sum + curr);
}
Loading

0 comments on commit b7506d5

Please sign in to comment.