Skip to content

Commit cc0ca11

Browse files
authored
added ability to configure shadow in banner (#155296)
Added ability to configure shadow in banner Issues: flutter/flutter#154505 Before: ![before](https://github.com/user-attachments/assets/d8b8e826-bab4-462a-a7ac-191e1597c3c6) After: ![after](https://github.com/user-attachments/assets/3c4b57e7-182a-4749-af72-b82fdaf25013)
1 parent 1dc2108 commit cc0ca11

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

packages/flutter/lib/src/widgets/banner.dart

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ import 'framework.dart';
1717

1818
const double _kOffset = 40.0; // distance to bottom of banner, at a 45 degree angle inwards
1919
const double _kHeight = 12.0; // height of banner
20-
const double _kBottomOffset = _kOffset + 0.707 * _kHeight; // offset plus sqrt(2)/2 * banner height
20+
const double _kBottomOffset = _kOffset + math.sqrt1_2 * _kHeight;
2121
const Rect _kRect = Rect.fromLTWH(-_kOffset, _kOffset - _kHeight, _kOffset * 2.0, _kHeight);
22+
const BoxShadow _kShadow = BoxShadow(
23+
color: Color(0x7F000000),
24+
blurRadius: 6.0,
25+
);
2226

2327
const Color _kColor = Color(0xA0B71C1C);
2428
const TextStyle _kTextStyle = TextStyle(
@@ -68,6 +72,7 @@ class BannerPainter extends CustomPainter {
6872
required this.layoutDirection,
6973
this.color = _kColor,
7074
this.textStyle = _kTextStyle,
75+
this.shadow = _kShadow,
7176
}) : super(repaint: PaintingBinding.instance.systemFonts) {
7277
// TODO(polina-c): stop duplicating code across disposables
7378
// https://github.com/flutter/flutter/issues/137435
@@ -120,10 +125,12 @@ class BannerPainter extends CustomPainter {
120125
/// Defaults to bold, white text.
121126
final TextStyle textStyle;
122127

123-
static const BoxShadow _shadow = BoxShadow(
124-
color: Color(0x7F000000),
125-
blurRadius: 6.0,
126-
);
128+
/// The shadow properties for the banner.
129+
///
130+
/// Use a [BoxShadow] object to define the shadow's color, blur radius,
131+
/// and spread radius. These properties can be used to create different
132+
/// shadow effects.
133+
final BoxShadow shadow;
127134

128135
bool _prepared = false;
129136
TextPainter? _textPainter;
@@ -144,7 +151,7 @@ class BannerPainter extends CustomPainter {
144151
}
145152

146153
void _prepare() {
147-
_paintShadow = _shadow.toPaint();
154+
_paintShadow = shadow.toPaint();
148155
_paintBanner = Paint()
149156
..color = color;
150157
_textPainter?.dispose();
@@ -232,6 +239,7 @@ class Banner extends StatefulWidget {
232239
this.layoutDirection,
233240
this.color = _kColor,
234241
this.textStyle = _kTextStyle,
242+
this.shadow = _kShadow,
235243
});
236244

237245
/// The widget to show behind the banner.
@@ -278,6 +286,13 @@ class Banner extends StatefulWidget {
278286
/// The style of the text shown on the banner.
279287
final TextStyle textStyle;
280288

289+
/// The shadow properties for the banner.
290+
///
291+
/// Use a [BoxShadow] object to define the shadow's color, blur radius,
292+
/// and spread radius. These properties can be used to create different
293+
/// shadow effects.
294+
final BoxShadow shadow;
295+
281296
@override
282297
State<Banner> createState() => _BannerState();
283298
}
@@ -303,6 +318,7 @@ class _BannerState extends State<Banner> {
303318
layoutDirection: widget.layoutDirection ?? Directionality.of(context),
304319
color: widget.color,
305320
textStyle: widget.textStyle,
321+
shadow: widget.shadow,
306322
);
307323

308324
return CustomPaint(

packages/flutter/test/widgets/banner_test.dart

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,4 +295,31 @@ void main() {
295295
areCreateAndDispose,
296296
);
297297
});
298+
299+
testWidgets('Can configure shadow for Banner widget', (WidgetTester tester) async {
300+
debugDisableShadows = false;
301+
await tester.pumpWidget(
302+
const Directionality(
303+
textDirection: TextDirection.ltr,
304+
child: Banner(
305+
message: 'Shadow banner',
306+
location: BannerLocation.topEnd,
307+
shadow: BoxShadow(
308+
color: Color(0xFF008000),
309+
blurRadius: 8.0,
310+
),
311+
),
312+
),
313+
);
314+
final Finder customPaint = find.byType(CustomPaint);
315+
316+
expect(customPaint, findsOneWidget);
317+
318+
final CustomPaint paintWidget = tester.widget(customPaint);
319+
final BannerPainter painter = paintWidget.foregroundPainter! as BannerPainter;
320+
321+
expect(painter.shadow.color, const Color(0xFF008000));
322+
expect(painter.shadow.blurRadius, 8.0);
323+
debugDisableShadows = true;
324+
});
298325
}

0 commit comments

Comments
 (0)