use toFixedAsString and DoubleProperty in diagnosticProperties (#33488)
This commit is contained in:
parent
0df3ec6bcb
commit
6feedcc6a4
@ -24,7 +24,8 @@ import 'print.dart';
|
||||
bool debugAssertAllFoundationVarsUnset(String reason, { DebugPrintCallback debugPrintOverride = debugPrintThrottled }) {
|
||||
assert(() {
|
||||
if (debugPrint != debugPrintOverride ||
|
||||
debugDefaultTargetPlatformOverride != null)
|
||||
debugDefaultTargetPlatformOverride != null ||
|
||||
debugDoublePrecision != null)
|
||||
throw FlutterError(reason);
|
||||
return true;
|
||||
}());
|
||||
@ -73,3 +74,21 @@ Future<T> debugInstrumentAction<T>(String description, Future<T> action()) {
|
||||
const Map<String, String> timelineWhitelistArguments = <String, String>{
|
||||
'mode': 'basic',
|
||||
};
|
||||
|
||||
/// Configure [debugFormatDouble] using [num.toStringAsPrecision].
|
||||
///
|
||||
/// Defaults to null, which uses the default logic of [debugFormatDouble].
|
||||
int debugDoublePrecision;
|
||||
|
||||
/// Formats a double to have standard formatting.
|
||||
///
|
||||
/// This behavior can be overriden by [debugDoublePrecision].
|
||||
String debugFormatDouble(double value) {
|
||||
if (value == null) {
|
||||
return 'null';
|
||||
}
|
||||
if (debugDoublePrecision != null) {
|
||||
return value.toStringAsPrecision(debugDoublePrecision);
|
||||
}
|
||||
return value.toStringAsFixed(1);
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import 'dart:math' as math;
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
import 'assertions.dart';
|
||||
import 'debug.dart';
|
||||
|
||||
// Examples can assume:
|
||||
// int rows, columns;
|
||||
@ -1911,7 +1912,7 @@ class DoubleProperty extends _NumProperty<double> {
|
||||
);
|
||||
|
||||
@override
|
||||
String numberToString() => value?.toStringAsFixed(1);
|
||||
String numberToString() => debugFormatDouble(value);
|
||||
}
|
||||
|
||||
/// An int valued property with an optional unit the value is measured in.
|
||||
|
@ -105,7 +105,7 @@ class BottomSheetThemeData extends Diagnosticable {
|
||||
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
|
||||
super.debugFillProperties(properties);
|
||||
properties.add(DiagnosticsProperty<Color>('backgroundColor', backgroundColor, defaultValue: null));
|
||||
properties.add(DiagnosticsProperty<double>('elevation', elevation, defaultValue: null));
|
||||
properties.add(DoubleProperty('elevation', elevation, defaultValue: null));
|
||||
properties.add(DiagnosticsProperty<ShapeBorder>('shape', shape, defaultValue: null));
|
||||
}
|
||||
}
|
||||
|
@ -122,7 +122,7 @@ class DialogTheme extends Diagnosticable {
|
||||
super.debugFillProperties(properties);
|
||||
properties.add(DiagnosticsProperty<Color>('backgroundColor', backgroundColor));
|
||||
properties.add(DiagnosticsProperty<ShapeBorder>('shape', shape, defaultValue: null));
|
||||
properties.add(DiagnosticsProperty<double>('elevation', elevation));
|
||||
properties.add(DoubleProperty('elevation', elevation));
|
||||
properties.add(DiagnosticsProperty<TextStyle>('titleTextStyle', titleTextStyle, defaultValue: null));
|
||||
properties.add(DiagnosticsProperty<TextStyle>('contentTextStyle', contentTextStyle, defaultValue: null));
|
||||
}
|
||||
|
@ -179,11 +179,11 @@ class FloatingActionButtonThemeData extends Diagnosticable {
|
||||
properties.add(DiagnosticsProperty<Color>('backgroundColor', backgroundColor, defaultValue: defaultData.backgroundColor));
|
||||
properties.add(DiagnosticsProperty<Color>('focusColor', focusColor, defaultValue: defaultData.focusColor));
|
||||
properties.add(DiagnosticsProperty<Color>('hoverColor', hoverColor, defaultValue: defaultData.hoverColor));
|
||||
properties.add(DiagnosticsProperty<double>('elevation', elevation, defaultValue: defaultData.elevation));
|
||||
properties.add(DiagnosticsProperty<double>('focusElevation', focusElevation, defaultValue: defaultData.focusElevation));
|
||||
properties.add(DiagnosticsProperty<double>('hoverElevation', hoverElevation, defaultValue: defaultData.hoverElevation));
|
||||
properties.add(DiagnosticsProperty<double>('disabledElevation', disabledElevation, defaultValue: defaultData.disabledElevation));
|
||||
properties.add(DiagnosticsProperty<double>('highlightElevation', highlightElevation, defaultValue: defaultData.highlightElevation));
|
||||
properties.add(DoubleProperty('elevation', elevation, defaultValue: defaultData.elevation));
|
||||
properties.add(DoubleProperty('focusElevation', focusElevation, defaultValue: defaultData.focusElevation));
|
||||
properties.add(DoubleProperty('hoverElevation', hoverElevation, defaultValue: defaultData.hoverElevation));
|
||||
properties.add(DoubleProperty('disabledElevation', disabledElevation, defaultValue: defaultData.disabledElevation));
|
||||
properties.add(DoubleProperty('highlightElevation', highlightElevation, defaultValue: defaultData.highlightElevation));
|
||||
properties.add(DiagnosticsProperty<ShapeBorder>('shape', shape, defaultValue: defaultData.shape));
|
||||
}
|
||||
}
|
||||
|
@ -166,7 +166,7 @@ class SnackBarThemeData extends Diagnosticable {
|
||||
properties.add(DiagnosticsProperty<Color>('backgroundColor', backgroundColor, defaultValue: null));
|
||||
properties.add(DiagnosticsProperty<Color>('actionTextColor', actionTextColor, defaultValue: null));
|
||||
properties.add(DiagnosticsProperty<Color>('disabledActionTextColor', disabledActionTextColor, defaultValue: null));
|
||||
properties.add(DiagnosticsProperty<double>('elevation', elevation, defaultValue: null));
|
||||
properties.add(DoubleProperty('elevation', elevation, defaultValue: null));
|
||||
properties.add(DiagnosticsProperty<ShapeBorder>('shape', shape, defaultValue: null));
|
||||
properties.add(DiagnosticsProperty<SnackBarBehavior>('behavior', behavior, defaultValue: null));
|
||||
}
|
||||
|
@ -238,7 +238,7 @@ class FlutterLogoDecoration extends Decoration {
|
||||
properties.add(DiagnosticsNode.message('$lightColor/$darkColor on $textColor'));
|
||||
properties.add(EnumProperty<FlutterLogoStyle>('style', style));
|
||||
if (_inTransition)
|
||||
properties.add(DiagnosticsNode.message('transition $_position:$_opacity'));
|
||||
properties.add(DiagnosticsNode.message('transition ${debugFormatDouble(_position)}:${debugFormatDouble(_opacity)}'));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -41,7 +41,7 @@ class ImageInfo {
|
||||
final double scale;
|
||||
|
||||
@override
|
||||
String toString() => '$image @ ${scale}x';
|
||||
String toString() => '$image @ ${debugFormatDouble(scale)}x';
|
||||
|
||||
@override
|
||||
int get hashCode => hashValues(image, scale);
|
||||
|
@ -265,9 +265,12 @@ class MatrixUtils {
|
||||
List<String> debugDescribeTransform(Matrix4 transform) {
|
||||
if (transform == null)
|
||||
return const <String>['null'];
|
||||
final List<String> matrix = transform.toString().split('\n').toList();
|
||||
matrix.removeLast();
|
||||
return matrix;
|
||||
return <String>[
|
||||
'[0] ${debugFormatDouble(transform.entry(0, 0))},${debugFormatDouble(transform.entry(0, 1))},${debugFormatDouble(transform.entry(0, 2))},${debugFormatDouble(transform.entry(0, 3))}',
|
||||
'[1] ${debugFormatDouble(transform.entry(1, 0))},${debugFormatDouble(transform.entry(1, 1))},${debugFormatDouble(transform.entry(1, 2))},${debugFormatDouble(transform.entry(1, 3))}',
|
||||
'[2] ${debugFormatDouble(transform.entry(2, 0))},${debugFormatDouble(transform.entry(2, 1))},${debugFormatDouble(transform.entry(2, 2))},${debugFormatDouble(transform.entry(2, 3))}',
|
||||
'[3] ${debugFormatDouble(transform.entry(3, 0))},${debugFormatDouble(transform.entry(3, 1))},${debugFormatDouble(transform.entry(3, 2))},${debugFormatDouble(transform.entry(3, 3))}',
|
||||
];
|
||||
}
|
||||
|
||||
/// Property which handles [Matrix4] that represent transforms.
|
||||
@ -296,13 +299,13 @@ class TransformProperty extends DiagnosticsProperty<Matrix4> {
|
||||
if (parentConfiguration != null && !parentConfiguration.lineBreakProperties) {
|
||||
// Format the value on a single line to be compatible with the parent's
|
||||
// style.
|
||||
final List<Vector4> rows = <Vector4>[
|
||||
value.getRow(0),
|
||||
value.getRow(1),
|
||||
value.getRow(2),
|
||||
value.getRow(3),
|
||||
final List<String> values = <String>[
|
||||
'${debugFormatDouble(value.entry(0, 0))},${debugFormatDouble(value.entry(0, 1))},${debugFormatDouble(value.entry(0, 2))},${debugFormatDouble(value.entry(0, 3))}',
|
||||
'${debugFormatDouble(value.entry(1, 0))},${debugFormatDouble(value.entry(1, 1))},${debugFormatDouble(value.entry(1, 2))},${debugFormatDouble(value.entry(1, 3))}',
|
||||
'${debugFormatDouble(value.entry(2, 0))},${debugFormatDouble(value.entry(2, 1))},${debugFormatDouble(value.entry(2, 2))},${debugFormatDouble(value.entry(2, 3))}',
|
||||
'${debugFormatDouble(value.entry(3, 0))},${debugFormatDouble(value.entry(3, 1))},${debugFormatDouble(value.entry(3, 2))},${debugFormatDouble(value.entry(3, 3))}',
|
||||
];
|
||||
return '[${rows.join("; ")}]';
|
||||
return '[${values.join('; ')}]';
|
||||
}
|
||||
return debugDescribeTransform(value).join('\n');
|
||||
}
|
||||
|
@ -215,17 +215,17 @@ class StackParentData extends ContainerBoxParentData<RenderBox> {
|
||||
String toString() {
|
||||
final List<String> values = <String>[];
|
||||
if (top != null)
|
||||
values.add('top=$top');
|
||||
values.add('top=${debugFormatDouble(top)}');
|
||||
if (right != null)
|
||||
values.add('right=$right');
|
||||
values.add('right=${debugFormatDouble(right)}');
|
||||
if (bottom != null)
|
||||
values.add('bottom=$bottom');
|
||||
values.add('bottom=${debugFormatDouble(bottom)}');
|
||||
if (left != null)
|
||||
values.add('left=$left');
|
||||
values.add('left=${debugFormatDouble(left)}');
|
||||
if (width != null)
|
||||
values.add('width=$width');
|
||||
values.add('width=${debugFormatDouble(width)}');
|
||||
if (height != null)
|
||||
values.add('height=$height');
|
||||
values.add('height=${debugFormatDouble(height)}');
|
||||
if (values.isEmpty)
|
||||
values.add('not positioned');
|
||||
values.add(super.toString());
|
||||
|
@ -141,7 +141,7 @@ class FixedColumnWidth extends TableColumnWidth {
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() => '$runtimeType($value)';
|
||||
String toString() => '$runtimeType(${debugFormatDouble(value)})';
|
||||
}
|
||||
|
||||
/// Sizes the column to a fraction of the table's constraints' maxWidth.
|
||||
@ -210,7 +210,7 @@ class FlexColumnWidth extends TableColumnWidth {
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() => '$runtimeType($value)';
|
||||
String toString() => '$runtimeType(${debugFormatDouble(value)})';
|
||||
}
|
||||
|
||||
/// Sizes the column such that it is the size that is the maximum of
|
||||
@ -1178,8 +1178,8 @@ class RenderTable extends RenderBox {
|
||||
properties.add(DiagnosticsProperty<Map<int, TableColumnWidth>>('specified column widths', _columnWidths, level: _columnWidths.isEmpty ? DiagnosticLevel.hidden : DiagnosticLevel.info));
|
||||
properties.add(DiagnosticsProperty<TableColumnWidth>('default column width', defaultColumnWidth));
|
||||
properties.add(MessageProperty('table size', '$columns\u00D7$rows'));
|
||||
properties.add(IterableProperty<double>('column offsets', _columnLefts, ifNull: 'unknown'));
|
||||
properties.add(IterableProperty<double>('row offsets', _rowTops, ifNull: 'unknown'));
|
||||
properties.add(IterableProperty<String>('column offsets', _columnLefts?.map(debugFormatDouble), ifNull: 'unknown'));
|
||||
properties.add(IterableProperty<String>('row offsets', _rowTops?.map(debugFormatDouble), ifNull: 'unknown'));
|
||||
}
|
||||
|
||||
@override
|
||||
|
20
packages/flutter/test/foundation/double_precision_test.dart
Normal file
20
packages/flutter/test/foundation/double_precision_test.dart
Normal file
@ -0,0 +1,20 @@
|
||||
// Copyright 2019 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import '../flutter_test_alternative.dart';
|
||||
|
||||
void main() {
|
||||
test('debugFormatDouble formats doubles', () {
|
||||
expect(debugFormatDouble(1), '1.0');
|
||||
expect(debugFormatDouble(1.1), '1.1');
|
||||
expect(debugFormatDouble(null), 'null');
|
||||
});
|
||||
|
||||
test('debugDoublePrecision can control double precision', () {
|
||||
debugDoublePrecision = 3;
|
||||
expect(debugFormatDouble(1), '1.00');
|
||||
debugDoublePrecision = null;
|
||||
});
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user