added ability to configure shadow in banner (#155296)
Added ability to configure shadow in banner Issues: https://github.com/flutter/flutter/issues/154505 Before:  After: 
This commit is contained in:
parent
1dc2108a0b
commit
cc0ca11ee4
@ -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(
|
||||
|
@ -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;
|
||||
});
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user