Skip to content

Commit b7506d5

Browse files
authored
fix: account for expand min/max bounds (#82)
* 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
1 parent b88bcc7 commit b7506d5

12 files changed

Lines changed: 363 additions & 161 deletions

example/lib/screens/divider/custom_divider_example_screen.dart

Lines changed: 104 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -40,96 +40,119 @@ class _CustomDividerExampleScreenState
4040
drawer: const NavDrawer(),
4141
body: Column(
4242
children: [
43-
Row(
44-
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
43+
Wrap(
44+
alignment: WrapAlignment.start,
45+
crossAxisAlignment: WrapCrossAlignment.center,
46+
runSpacing: 18,
47+
spacing: 18,
4548
children: [
46-
Column(
47-
children: [
48-
const Text('Length'),
49-
Slider(
50-
min: 0.01,
51-
max: 1.0,
52-
value: length,
53-
onChanged: (value) => setState(() => length = value),
54-
),
55-
Text('Ratio: ${(length * 100).toStringAsFixed((2))}%'),
56-
],
49+
SizedBox(
50+
width: 200,
51+
child: Column(
52+
crossAxisAlignment: CrossAxisAlignment.center,
53+
children: [
54+
const Text('Length'),
55+
Slider(
56+
min: 0.01,
57+
max: 1.0,
58+
value: length,
59+
onChanged: (value) => setState(() => length = value),
60+
),
61+
Text('Ratio: ${(length * 100).toStringAsFixed((2))}%'),
62+
],
63+
),
5764
),
58-
Column(
59-
children: [
60-
const Text('Thickness'),
61-
Slider(
62-
min: 1,
63-
max: 20.0,
64-
divisions: 19,
65-
value: thickness,
66-
onChanged: (value) => setState(() => thickness = value),
67-
),
68-
Text('${thickness}px'),
69-
],
65+
SizedBox(
66+
width: 200,
67+
child: Column(
68+
crossAxisAlignment: CrossAxisAlignment.center,
69+
children: [
70+
const Text('Thickness'),
71+
Slider(
72+
min: 1,
73+
max: 20.0,
74+
divisions: 19,
75+
value: thickness,
76+
onChanged: (value) => setState(() => thickness = value),
77+
),
78+
Text('${thickness}px'),
79+
],
80+
),
7081
),
71-
Column(
72-
children: [
73-
const Text('Padding'),
74-
Slider(
75-
min: 0,
76-
max: 20,
77-
divisions: 20,
78-
value: padding,
79-
onChanged: (value) => setState(() => padding = value),
80-
),
81-
Text('${padding}px'),
82-
],
82+
SizedBox(
83+
width: 200,
84+
child: Column(
85+
crossAxisAlignment: CrossAxisAlignment.center,
86+
children: [
87+
const Text('Padding'),
88+
Slider(
89+
min: 0,
90+
max: 20,
91+
divisions: 20,
92+
value: padding,
93+
onChanged: (value) => setState(() => padding = value),
94+
),
95+
Text('${padding}px'),
96+
],
97+
),
8398
),
84-
Column(
85-
children: [
86-
const Text('Cross-Axis Alignment'),
87-
DropdownButton(
88-
value: crossAxisAlignment,
89-
items: const [
90-
DropdownMenuItem(
91-
value: CrossAxisAlignment.start,
92-
child: Text('Start'),
99+
SizedBox(
100+
width: 200,
101+
child: Column(
102+
crossAxisAlignment: CrossAxisAlignment.center,
103+
children: [
104+
const Text('Cross-Axis Alignment'),
105+
DropdownButton(
106+
value: crossAxisAlignment,
107+
items: const [
108+
DropdownMenuItem(
109+
value: CrossAxisAlignment.start,
110+
child: Text('Start'),
111+
),
112+
DropdownMenuItem(
113+
value: CrossAxisAlignment.center,
114+
child: Text('Center'),
115+
),
116+
DropdownMenuItem(
117+
value: CrossAxisAlignment.end,
118+
child: Text('End'),
119+
),
120+
],
121+
onChanged: (value) => setState(
122+
() => crossAxisAlignment = value!,
93123
),
94-
DropdownMenuItem(
95-
value: CrossAxisAlignment.center,
96-
child: Text('Center'),
97-
),
98-
DropdownMenuItem(
99-
value: CrossAxisAlignment.end,
100-
child: Text('End'),
101-
),
102-
],
103-
onChanged: (value) => setState(
104-
() => crossAxisAlignment = value!,
105124
),
106-
),
107-
],
125+
],
126+
),
108127
),
109-
Column(
110-
children: [
111-
const Text('Main-Axis Alignment'),
112-
DropdownButton(
113-
value: mainAxisAlignment,
114-
items: const [
115-
DropdownMenuItem(
116-
value: MainAxisAlignment.start,
117-
child: Text('Start'),
128+
SizedBox(
129+
width: 200,
130+
child: Column(
131+
crossAxisAlignment: CrossAxisAlignment.center,
132+
children: [
133+
const Text('Main-Axis Alignment'),
134+
DropdownButton(
135+
value: mainAxisAlignment,
136+
items: const [
137+
DropdownMenuItem(
138+
value: MainAxisAlignment.start,
139+
child: Text('Start'),
140+
),
141+
DropdownMenuItem(
142+
value: MainAxisAlignment.center,
143+
child: Text('Center'),
144+
),
145+
DropdownMenuItem(
146+
value: MainAxisAlignment.end,
147+
child: Text('End'),
148+
),
149+
],
150+
onChanged: (value) => setState(
151+
() => mainAxisAlignment = value!,
118152
),
119-
DropdownMenuItem(
120-
value: MainAxisAlignment.center,
121-
child: Text('Center'),
122-
),
123-
DropdownMenuItem(
124-
value: MainAxisAlignment.end,
125-
child: Text('End'),
126-
),
127-
],
128-
onChanged: (value) => setState(
129-
() => mainAxisAlignment = value!,
130153
),
131-
),
132-
],
154+
],
155+
),
133156
),
134157
],
135158
),

example/lib/screens/shrink_and_flex/shrink_and_flex_example_screen.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,12 @@ class _ShrinkAndFlexExampleScreenState
5151
),
5252
ResizableChild(
5353
size: const ResizableSize.shrink(),
54-
child: ColoredBox(
55-
color: Theme.of(context).colorScheme.tertiaryContainer,
56-
child: const SizeLabel(),
54+
child: SizedBox(
55+
width: 100,
56+
child: ColoredBox(
57+
color: Theme.of(context).colorScheme.tertiaryContainer,
58+
child: const SizeLabel(),
59+
),
5760
),
5861
),
5962
],

example/pubspec.lock

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ packages:
4949
url: "https://pub.dev"
5050
source: hosted
5151
version: "1.0.8"
52+
decimal:
53+
dependency: transitive
54+
description:
55+
name: decimal
56+
sha256: da8f65df568345f2738cc8b0de74971c86d2d93ce5fc8c4ec094f6b7c5d48eb5
57+
url: "https://pub.dev"
58+
source: hosted
59+
version: "3.1.0"
5260
fake_async:
5361
dependency: transitive
5462
description:
@@ -111,6 +119,14 @@ packages:
111119
url: "https://pub.dev"
112120
source: hosted
113121
version: "4.0.2"
122+
intl:
123+
dependency: transitive
124+
description:
125+
name: intl
126+
sha256: "99f282cb0e02edcbbf8c6b3bbc7c90b65635156c412e58f3975a7e55284ce685"
127+
url: "https://pub.dev"
128+
source: hosted
129+
version: "0.20.0"
114130
leak_tracker:
115131
dependency: transitive
116132
description:
@@ -199,6 +215,14 @@ packages:
199215
url: "https://pub.dev"
200216
source: hosted
201217
version: "2.1.8"
218+
rational:
219+
dependency: transitive
220+
description:
221+
name: rational
222+
sha256: cb808fb6f1a839e6fc5f7d8cb3b0a10e1db48b3be102de73938c627f0b636336
223+
url: "https://pub.dev"
224+
source: hosted
225+
version: "2.2.3"
202226
sky_engine:
203227
dependency: transitive
204228
description: flutter
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
extension IterableNumExtensions on Iterable<num> {
2-
num sum() => fold(0, (sum, current) => sum + current);
3-
}
4-
51
extension IterableExtensions<T> on Iterable<T> {
62
int nullCount() => where((item) => item == null).length;
73

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

10-
num sum(num Function(T) extractor) => fold(
11-
0.0,
12-
(sum, current) => sum + extractor(current),
13-
);
14-
156
Iterable<T> evenIndices() => [
167
for (var i = 0; i < length; i++) ...[
178
if (i % 2 == 0) ...[
189
elementAt(i),
1910
],
2011
],
2112
];
13+
14+
Iterable<int> indicesWhere(bool Function(T) test) => [
15+
for (var i = 0; i < length; i++) ...[
16+
if (test(elementAt(i))) ...[
17+
i,
18+
],
19+
],
20+
];
2221
}

lib/src/extensions/num_ext.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import 'package:decimal/decimal.dart';
2+
3+
extension DoubleExtensions on double {
4+
Decimal toDecimal() => Decimal.parse(toString());
5+
}
6+
7+
extension ListDoubleExtensions on Iterable<double> {
8+
double sum() => fold(0.0, (sum, curr) => sum + curr);
9+
}
10+
11+
extension ListIntExtensions on Iterable<int> {
12+
int sum() => fold(0, (sum, curr) => sum + curr);
13+
}

0 commit comments

Comments
 (0)