Unverified Commit 0ebcfe10 authored by rami-a's avatar rami-a Committed by GitHub

Prevent exception when creating a Divider borderSide (#39572)

parent 16fcb83f
...@@ -91,12 +91,16 @@ class Divider extends StatelessWidget { ...@@ -91,12 +91,16 @@ class Divider extends StatelessWidget {
/// {@end-tool} /// {@end-tool}
final Color color; final Color color;
/// Computes the [BorderSide] that represents a divider of the specified /// Computes the [BorderSide] that represents a divider..
/// color, or, if there is no specified color, of the default
/// [ThemeData.dividerColor] specified in the ambient [Theme].
/// ///
/// The `width` argument can be used to override the default width of the /// If [color] is null, then [DividerThemeData.color] is used. If that is also
/// divider border, which defaults to 0.0 (a hairline border). /// null, then [ThemeData.dividerColor] is used.
///
/// If [width] is null, then [DividerThemeData.thickness] is used. If that is
/// also null, then this defaults to 0.0 (a hairline border).
///
/// If [context] is null, the default color of [BorderSide] is used and the
/// default width of 0.0 is used.
/// ///
/// {@tool sample} /// {@tool sample}
/// ///
...@@ -117,9 +121,22 @@ class Divider extends StatelessWidget { ...@@ -117,9 +121,22 @@ class Divider extends StatelessWidget {
/// ``` /// ```
/// {@end-tool} /// {@end-tool}
static BorderSide createBorderSide(BuildContext context, { Color color, double width }) { static BorderSide createBorderSide(BuildContext context, { Color color, double width }) {
final Color effectiveColor = color
?? (context != null ? (DividerTheme.of(context).color ?? Theme.of(context).dividerColor) : null);
final double effectiveWidth = width
?? (context != null ? DividerTheme.of(context).thickness : null)
?? 0.0;
// Prevent assertion since it is possible that context is null and no color
// is specified.
if (effectiveColor == null) {
return BorderSide(
width: effectiveWidth,
);
}
return BorderSide( return BorderSide(
color: color ?? DividerTheme.of(context).color ?? Theme.of(context).dividerColor, color: effectiveColor,
width: width ?? DividerTheme.of(context).thickness ?? 0.0, width: effectiveWidth,
); );
} }
......
...@@ -203,4 +203,11 @@ void main() { ...@@ -203,4 +203,11 @@ void main() {
expect(lineRect.top, dividerRect.top + customIndent); expect(lineRect.top, dividerRect.top + customIndent);
expect(lineRect.bottom, dividerRect.bottom - customIndent); expect(lineRect.bottom, dividerRect.bottom - customIndent);
}); });
// Regression test for https://github.com/flutter/flutter/issues/39533
testWidgets('createBorderSide does not throw exception with null context', (WidgetTester tester) async {
// Passing a null context used to throw an exception but no longer does.
expect(() => Divider.createBorderSide(null), isNot(throwsAssertionError));
expect(() => Divider.createBorderSide(null), isNot(throwsNoSuchMethodError));
});
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment