Skip to content
This repository was archived by the owner on Apr 29, 2021. It is now read-only.

Commit c253cd1

Browse files
committed
fix input_decoration _layout bug (c# doesn't support null key in Dictionary while dart support Map[null] = value)
1 parent e61dfea commit c253cd1

File tree

2 files changed

+69
-38
lines changed

2 files changed

+69
-38
lines changed

Runtime/foundation/basic_types.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ public static TValue getOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> i
7272
it.TryGetValue(key, out v);
7373
return v;
7474
}
75+
76+
public static TValue getOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> it, TKey key, TValue defaultVal) {
77+
TValue v = defaultVal;
78+
if (key == null) {
79+
return v;
80+
}
81+
it.TryGetValue(key, out v);
82+
return v;
83+
}
7584

7685
public static T first<T>(this IList<T> it) {
7786
return it[0];

Runtime/material/input_decorator.cs

Lines changed: 60 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -939,11 +939,21 @@ float _layoutLineBox(RenderBox box, BoxConstraints constraints) {
939939
_RenderDecorationLayout _layout(BoxConstraints layoutConstraints) {
940940
Dictionary<RenderBox, float> boxToBaseline = new Dictionary<RenderBox, float>();
941941
BoxConstraints boxConstraints = layoutConstraints.loosen();
942-
boxToBaseline[this.prefix] = this._layoutLineBox(this.prefix, boxConstraints);
943-
boxToBaseline[this.suffix] = this._layoutLineBox(this.suffix, boxConstraints);
944-
boxToBaseline[this.icon] = this._layoutLineBox(this.icon, boxConstraints);
945-
boxToBaseline[this.prefixIcon] = this._layoutLineBox(this.prefixIcon, boxConstraints);
946-
boxToBaseline[this.suffixIcon] = this._layoutLineBox(this.suffixIcon, boxConstraints);
942+
if (this.prefix != null) {
943+
boxToBaseline[this.prefix] = this._layoutLineBox(this.prefix, boxConstraints);
944+
}
945+
if (this.suffix != null) {
946+
boxToBaseline[this.suffix] = this._layoutLineBox(this.suffix, boxConstraints);
947+
}
948+
if (this.icon != null) {
949+
boxToBaseline[this.icon] = this._layoutLineBox(this.icon, boxConstraints);
950+
}
951+
if (this.prefixIcon != null) {
952+
boxToBaseline[this.prefixIcon] = this._layoutLineBox(this.prefixIcon, boxConstraints);
953+
}
954+
if (this.suffixIcon != null) {
955+
boxToBaseline[this.suffixIcon] = this._layoutLineBox(this.suffixIcon, boxConstraints);
956+
}
947957

948958
float inputWidth = Math.Max(0.0f, this.constraints.maxWidth - (
949959
_boxSize(this.icon).width
@@ -953,23 +963,33 @@ _RenderDecorationLayout _layout(BoxConstraints layoutConstraints) {
953963
+ _boxSize(this.suffix).width
954964
+ _boxSize(this.suffixIcon).width
955965
+ this.contentPadding.right));
956-
boxToBaseline[this.label] = this._layoutLineBox(this.label,
957-
boxConstraints.copyWith(maxWidth: inputWidth)
958-
);
959-
boxToBaseline[this.hint] = this._layoutLineBox(this.hint,
960-
boxConstraints.copyWith(minWidth: inputWidth, maxWidth: inputWidth)
961-
);
962-
boxToBaseline[this.counter] = this._layoutLineBox(this.counter, boxConstraints);
963-
964-
boxToBaseline[this.helperError] = this._layoutLineBox(this.helperError,
965-
boxConstraints.copyWith(
966-
maxWidth: Math.Max(0.0f, boxConstraints.maxWidth
967-
- _boxSize(this.icon).width
968-
- _boxSize(this.counter).width
969-
- this.contentPadding.horizontal
966+
if (this.label != null) {
967+
boxToBaseline[this.label] = this._layoutLineBox(this.label,
968+
boxConstraints.copyWith(maxWidth: inputWidth)
969+
);
970+
}
971+
972+
if (this.hint != null) {
973+
boxToBaseline[this.hint] = this._layoutLineBox(this.hint,
974+
boxConstraints.copyWith(minWidth: inputWidth, maxWidth: inputWidth)
975+
);
976+
}
977+
978+
if (this.counter != null) {
979+
boxToBaseline[this.counter] = this._layoutLineBox(this.counter, boxConstraints);
980+
}
981+
982+
if (this.helperError != null) {
983+
boxToBaseline[this.helperError] = this._layoutLineBox(this.helperError,
984+
boxConstraints.copyWith(
985+
maxWidth: Math.Max(0.0f, boxConstraints.maxWidth
986+
- _boxSize(this.icon).width
987+
- _boxSize(this.counter).width
988+
- this.contentPadding.horizontal
989+
)
970990
)
971-
)
972-
);
991+
);
992+
}
973993

974994
float labelHeight = this.label == null
975995
? 0
@@ -989,37 +1009,39 @@ _RenderDecorationLayout _layout(BoxConstraints layoutConstraints) {
9891009
counterHeight,
9901010
helperErrorHeight
9911011
);
992-
boxToBaseline[this.input] = this._layoutLineBox(this.input,
993-
boxConstraints.deflate(EdgeInsets.only(
994-
top: this.contentPadding.top + topHeight,
995-
bottom: this.contentPadding.bottom + bottomHeight
996-
)).copyWith(
997-
minWidth: inputWidth,
998-
maxWidth: inputWidth
999-
)
1000-
);
1012+
if (this.input != null) {
1013+
boxToBaseline[this.input] = this._layoutLineBox(this.input,
1014+
boxConstraints.deflate(EdgeInsets.only(
1015+
top: this.contentPadding.top + topHeight,
1016+
bottom: this.contentPadding.bottom + bottomHeight
1017+
)).copyWith(
1018+
minWidth: inputWidth,
1019+
maxWidth: inputWidth
1020+
)
1021+
);
1022+
}
10011023

10021024
// The field can be occupied by a hint or by the input itself
10031025
float hintHeight = this.hint == null ? 0 : this.hint.size.height;
10041026
float inputDirectHeight = this.input == null ? 0 : this.input.size.height;
10051027
float inputHeight = Math.Max(hintHeight, inputDirectHeight);
10061028
float inputInternalBaseline = Math.Max(
1007-
boxToBaseline[this.input],
1008-
boxToBaseline[this.hint]
1029+
boxToBaseline.getOrDefault(this.input, 0.0f),
1030+
boxToBaseline.getOrDefault(this.hint, 0.0f)
10091031
);
10101032

10111033
// Calculate the amount that prefix/suffix affects height above and below
10121034
// the input.
10131035
float prefixHeight = this.prefix == null ? 0 : this.prefix.size.height;
10141036
float suffixHeight = this.suffix == null ? 0 : this.suffix.size.height;
10151037
float fixHeight = Math.Max(
1016-
boxToBaseline[this.prefix],
1017-
boxToBaseline[this.suffix]
1038+
boxToBaseline.getOrDefault(this.prefix, 0.0f),
1039+
boxToBaseline.getOrDefault(this.suffix, 0.0f)
10181040
);
10191041
float fixAboveInput = Math.Max(0, fixHeight - inputInternalBaseline);
10201042
float fixBelowBaseline = Math.Max(
1021-
prefixHeight - boxToBaseline[this.prefix],
1022-
suffixHeight - boxToBaseline[this.suffix]
1043+
prefixHeight - boxToBaseline.getOrDefault(this.prefix, 0.0f),
1044+
suffixHeight - boxToBaseline.getOrDefault(this.suffix, 0.0f)
10231045
);
10241046
float fixBelowInput = Math.Max(
10251047
0,
@@ -1067,13 +1089,13 @@ _RenderDecorationLayout _layout(BoxConstraints layoutConstraints) {
10671089
float subtextHelperHeight = 0;
10681090
if (this.counter != null) {
10691091
subtextCounterBaseline =
1070-
containerHeight + subtextGap + boxToBaseline[this.counter];
1092+
containerHeight + subtextGap + boxToBaseline.getOrDefault(this.counter, 0.0f);
10711093
subtextCounterHeight = this.counter.size.height + subtextGap;
10721094
}
10731095

10741096
if (helperErrorExists) {
10751097
subtextHelperBaseline =
1076-
containerHeight + subtextGap + boxToBaseline[this.helperError];
1098+
containerHeight + subtextGap + boxToBaseline.getOrDefault(this.helperError, 0.0f);
10771099
subtextHelperHeight = helperErrorHeight;
10781100
}
10791101

0 commit comments

Comments
 (0)