我找到了一个解决方案,它也适用于透明色:
- 用
Stack() 包裹容器并使用另一个Container() 来
隐藏左边框。为了达到预期的效果
容器颜色相同并调整高度(从第一个容器中减去 2 倍的边框宽度得到第二个容器的高度)。
- 要将
"Border"-Container 定位到左侧,请将alignment: Alignment.centerLeft, 添加到堆栈中。
- 为了在此自定义容器中显示文本,您需要将
Child-Widget(您将直接添加到第一个容器的子参数)作为单独的小部件定位到 Row() 与您的 @987654328 @ 和 "Border"-Container 作为它的孩子。
- 要正确对齐
Child-Widget,请使用行的mainAxisAlignment: MainAxisAlignment.spaceBetween, 并将Child-Widget 与Padding() 包裹起来,以确保"Border"-Container 将贴在左边框上。
这是所需小部件的代码:
Stack(
alignment: Alignment.centerLeft,
children: [
Container(
height: 493.0,
width: 1353.0,
decoration: BoxDecoration(
border: Border.all(color: Colors.white, width: 4.0),
color: Colors.black.withOpacity(0.45),
borderRadius: BorderRadius.only(
bottomRight: Radius.circular(20),
topRight: Radius.circular(20),
),
boxShadow: [
CustomBoxShadow(
color: Colors.white,
blurRadius: 15.0,
blurStyle: BlurStyle.outer,
)
],
),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
// Acts as the left border
Container(
width: 4.0,
height: 485.0,
color: Colors.black,
),
SizedBox(width: 115.0),
Padding(
padding: const EdgeInsets.only(
right: 75.0,
),
child: Text(
"Add the child widget here",
style: TextStyle(color: Colors.white),
), // Add text here
)
],
)
],
),
仅供参考:如果你想要一个 BoxShadow,你会遇到一个问题,即它会改变你的透明容器颜色,就像 Flutter绘制小部件后面的阴影。我在下面添加了一个 CustomBoxShadow,它允许更改默认的 blurStyle。使用blurStyle: BlurStyle.outer,保持您想要的容器透明背景颜色。代码来自这里:SO-Link。
class CustomBoxShadow extends BoxShadow {
final BlurStyle blurStyle;
const CustomBoxShadow({
Color color = const Color(0xFF000000),
Offset offset = Offset.zero,
double blurRadius = 0.0,
this.blurStyle = BlurStyle.normal,
}) : super(color: color, offset: offset, blurRadius: blurRadius);
@override
Paint toPaint() {
final Paint result = Paint()
..color = color
..maskFilter = MaskFilter.blur(this.blurStyle, blurSigma);
assert(() {
if (debugDisableShadows) result.maskFilter = null;
return true;
}());
return result;
}
}