diff --git a/packages/flutter/lib/src/widgets/banner.dart b/packages/flutter/lib/src/widgets/banner.dart index eeaa343b84..dc1e684643 100644 --- a/packages/flutter/lib/src/widgets/banner.dart +++ b/packages/flutter/lib/src/widgets/banner.dart @@ -17,8 +17,12 @@ import 'framework.dart'; const double _kOffset = 40.0; // distance to bottom of banner, at a 45 degree angle inwards const double _kHeight = 12.0; // height of banner -const double _kBottomOffset = _kOffset + 0.707 * _kHeight; // offset plus sqrt(2)/2 * banner height +const double _kBottomOffset = _kOffset + math.sqrt1_2 * _kHeight; const Rect _kRect = Rect.fromLTWH(-_kOffset, _kOffset - _kHeight, _kOffset * 2.0, _kHeight); +const BoxShadow _kShadow = BoxShadow( + color: Color(0x7F000000), + blurRadius: 6.0, + ); const Color _kColor = Color(0xA0B71C1C); const TextStyle _kTextStyle = TextStyle( @@ -68,6 +72,7 @@ class BannerPainter extends CustomPainter { required this.layoutDirection, this.color = _kColor, this.textStyle = _kTextStyle, + this.shadow = _kShadow, }) : super(repaint: PaintingBinding.instance.systemFonts) { // TODO(polina-c): stop duplicating code across disposables // https://github.com/flutter/flutter/issues/137435 @@ -120,10 +125,12 @@ class BannerPainter extends CustomPainter { /// Defaults to bold, white text. final TextStyle textStyle; - static const BoxShadow _shadow = BoxShadow( - color: Color(0x7F000000), - blurRadius: 6.0, - ); + /// The shadow properties for the banner. + /// + /// Use a [BoxShadow] object to define the shadow's color, blur radius, + /// and spread radius. These properties can be used to create different + /// shadow effects. + final BoxShadow shadow; bool _prepared = false; TextPainter? _textPainter; @@ -144,7 +151,7 @@ class BannerPainter extends CustomPainter { } void _prepare() { - _paintShadow = _shadow.toPaint(); + _paintShadow = shadow.toPaint(); _paintBanner = Paint() ..color = color; _textPainter?.dispose(); @@ -232,6 +239,7 @@ class Banner extends StatefulWidget { this.layoutDirection, this.color = _kColor, this.textStyle = _kTextStyle, + this.shadow = _kShadow, }); /// The widget to show behind the banner. @@ -278,6 +286,13 @@ class Banner extends StatefulWidget { /// The style of the text shown on the banner. final TextStyle textStyle; + /// The shadow properties for the banner. + /// + /// Use a [BoxShadow] object to define the shadow's color, blur radius, + /// and spread radius. These properties can be used to create different + /// shadow effects. + final BoxShadow shadow; + @override State createState() => _BannerState(); } @@ -303,6 +318,7 @@ class _BannerState extends State { layoutDirection: widget.layoutDirection ?? Directionality.of(context), color: widget.color, textStyle: widget.textStyle, + shadow: widget.shadow, ); return CustomPaint( diff --git a/packages/flutter/test/widgets/banner_test.dart b/packages/flutter/test/widgets/banner_test.dart index 405c7b9642..95617f5ede 100644 --- a/packages/flutter/test/widgets/banner_test.dart +++ b/packages/flutter/test/widgets/banner_test.dart @@ -295,4 +295,31 @@ void main() { areCreateAndDispose, ); }); + + testWidgets('Can configure shadow for Banner widget', (WidgetTester tester) async { + debugDisableShadows = false; + await tester.pumpWidget( + const Directionality( + textDirection: TextDirection.ltr, + child: Banner( + message: 'Shadow banner', + location: BannerLocation.topEnd, + shadow: BoxShadow( + color: Color(0xFF008000), + blurRadius: 8.0, + ), + ), + ), + ); + final Finder customPaint = find.byType(CustomPaint); + + expect(customPaint, findsOneWidget); + + final CustomPaint paintWidget = tester.widget(customPaint); + final BannerPainter painter = paintWidget.foregroundPainter! as BannerPainter; + + expect(painter.shadow.color, const Color(0xFF008000)); + expect(painter.shadow.blurRadius, 8.0); + debugDisableShadows = true; + }); }