added ability to configure shadow in banner (#155296)

Added ability to configure shadow in banner

Issues:  https://github.com/flutter/flutter/issues/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)
This commit is contained in:
Sikandar Sadaqat 2024-09-26 09:04:24 +05:00 committed by GitHub
parent 1dc2108a0b
commit cc0ca11ee4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 49 additions and 6 deletions

View File

@ -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<Banner> createState() => _BannerState();
}
@ -303,6 +318,7 @@ class _BannerState extends State<Banner> {
layoutDirection: widget.layoutDirection ?? Directionality.of(context),
color: widget.color,
textStyle: widget.textStyle,
shadow: widget.shadow,
);
return CustomPaint(

View File

@ -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;
});
}