mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Handle NaNs in constraints, just in case.
This commit is contained in:
parent
e6cffd2897
commit
9356c19dce
@ -305,6 +305,29 @@ class BoxConstraints extends Constraints {
|
||||
@override
|
||||
bool get debugAssertIsNormalized {
|
||||
assert(() {
|
||||
if (minWidth.isNaN || maxWidth.isNaN || minHeight.isNaN || maxHeight.isNaN) {
|
||||
List<String> affectedFieldsList = <String>[];
|
||||
if (minWidth.isNaN)
|
||||
affectedFieldsList.add('minWidth');
|
||||
if (maxWidth.isNaN)
|
||||
affectedFieldsList.add('maxWidth');
|
||||
if (minHeight.isNaN)
|
||||
affectedFieldsList.add('minHeight');
|
||||
if (maxHeight.isNaN)
|
||||
affectedFieldsList.add('maxHeight');
|
||||
assert(affectedFieldsList.length > 0);
|
||||
if (affectedFieldsList.length > 1)
|
||||
affectedFieldsList.add('and ${affectedFieldsList.removeLast()}');
|
||||
String whichFields = '';
|
||||
if (affectedFieldsList.length > 2) {
|
||||
whichFields = affectedFieldsList.join(', ');
|
||||
} else if (affectedFieldsList.length == 2) {
|
||||
whichFields = affectedFieldsList.join(' ');
|
||||
} else {
|
||||
whichFields = affectedFieldsList.single;
|
||||
}
|
||||
throw new FlutterError('BoxConstraints has ${affectedFieldsList.length == 1 ? 'a NaN value' : 'NaN values' } in $whichFields.\n$this');
|
||||
}
|
||||
if (minWidth < 0.0 && minHeight < 0.0)
|
||||
throw new FlutterError('BoxConstraints has both a negative minimum width and a negative minimum height.\n$this');
|
||||
if (minWidth < 0.0)
|
||||
|
||||
@ -30,4 +30,44 @@ void main() {
|
||||
expect(leaf.size.width, equals(400.0));
|
||||
expect(leaf.size.height, equals(100.0));
|
||||
});
|
||||
|
||||
test("BoxConstraints with NaN", () {
|
||||
String result;
|
||||
|
||||
result = 'no exception';
|
||||
try {
|
||||
BoxConstraints constraints = new BoxConstraints(minWidth: double.NAN, maxWidth: double.NAN, minHeight: 2.0, maxHeight: double.NAN);
|
||||
assert(constraints.debugAssertIsNormalized);
|
||||
} on FlutterError catch (e) {
|
||||
result = '$e';
|
||||
}
|
||||
expect(result, equals(
|
||||
'BoxConstraints has NaN values in minWidth, maxWidth, and maxHeight.\n'
|
||||
'BoxConstraints(NaN<=w<=NaN, 2.0<=h<=NaN; NOT NORMALIZED)'
|
||||
));
|
||||
|
||||
result = 'no exception';
|
||||
try {
|
||||
BoxConstraints constraints = new BoxConstraints(minHeight: double.NAN);
|
||||
assert(constraints.debugAssertIsNormalized);
|
||||
} on FlutterError catch (e) {
|
||||
result = '$e';
|
||||
}
|
||||
expect(result, equals(
|
||||
'BoxConstraints has a NaN value in minHeight.\n'
|
||||
'BoxConstraints(0.0<=w<=Infinity, NaN<=h<=Infinity; NOT NORMALIZED)'
|
||||
));
|
||||
|
||||
result = 'no exception';
|
||||
try {
|
||||
BoxConstraints constraints = new BoxConstraints(minHeight: double.NAN, maxWidth: 0.0/0.0);
|
||||
assert(constraints.debugAssertIsNormalized);
|
||||
} on FlutterError catch (e) {
|
||||
result = '$e';
|
||||
}
|
||||
expect(result, equals(
|
||||
'BoxConstraints has NaN values in maxWidth and minHeight.\n'
|
||||
'BoxConstraints(0.0<=w<=NaN, NaN<=h<=Infinity; NOT NORMALIZED)'
|
||||
));
|
||||
});
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user