Skip to content

Commit 9390c9d

Browse files
committed
compose_box [nfc]: Add InsetShadowBox
This casts a static shadow on top of a child widget from its top edge and bottom edge. Signed-off-by: Zixuan James Li <[email protected]>
1 parent 701763d commit 9390c9d

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

lib/widgets/inset_shadow_box.dart

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import 'package:flutter/material.dart';
2+
3+
/// A widget that overlays inset shadows on a child.
4+
class InsetShadowBox extends StatelessWidget {
5+
const InsetShadowBox({
6+
super.key,
7+
this.top = 0,
8+
this.bottom = 0,
9+
required this.color,
10+
required this.child,
11+
});
12+
13+
/// The distance that the shadows from the child's top edge grows downwards.
14+
///
15+
/// This does not pad the child widget.
16+
final double top;
17+
18+
/// The distance that the shadows from the child's bottom edge grows upwards.
19+
///
20+
/// This does not pad the child widget.
21+
final double bottom;
22+
23+
/// The shadow color to fade into transparency from the top and bottom borders.
24+
final Color color;
25+
26+
final Widget child;
27+
28+
BoxDecoration _shadowFrom(AlignmentGeometry begin) {
29+
return BoxDecoration(gradient: LinearGradient(
30+
begin: begin, end: -begin,
31+
colors: [color, color.withValues(alpha: 0)]));
32+
}
33+
34+
@override
35+
Widget build(BuildContext context) {
36+
return Stack(
37+
children: [
38+
child,
39+
Positioned(top: 0, left: 0, right: 0,
40+
child: Container(height: top, decoration: _shadowFrom(Alignment.topCenter))),
41+
Positioned(bottom: 0, left: 0, right: 0,
42+
child: Container(height: bottom, decoration: _shadowFrom(Alignment.bottomCenter))),
43+
]);
44+
}
45+
}

0 commit comments

Comments
 (0)