gpt4 book ai didi

android - 如何创建具有半透明背景和不透明前景的小部件?

转载 作者:IT王子 更新时间:2023-10-29 07:08:51 26 4
gpt4 key购买 nike

我创建了这个 flutter 小部件,它显示了一个半透明的背景和一个不透明的前景。我使用堆栈来做到这一点。不幸的是,我还必须使用内容来布局背景,否则内容的大小将不正确。

有没有办法让堆栈中的后台知道前台的大小而不需要进行两次布局?

typedef void OnTapFn();

class Bubble extends StatelessWidget {
const Bubble({
@required this.tapFn,
@required this.content,
this.margin = 4.0,
this.color = Colors.grey,
});

final OnTapFn tapFn;
final double margin;
final Widget content;
final Color color;

@override
Widget build(BuildContext context) => new GestureDetector(
onTap: () => tapFn,
child: new Stack(
children: <Widget>[
new Opacity(opacity: 0.5, child: _buildBackground),
new Container(
margin: new EdgeInsets.all(margin), //same as the Border width
child: new Opacity(opacity: 1.0, child: content))
],
));

Container get _buildBackground => new Container(
decoration: new BoxDecoration(
border: new Border.all(width: margin, color: color),
borderRadius: const BorderRadius.all(const Radius.circular(30.0)),
),
//placeholder to guarantee that the background is big enough
child: new Container(
color: color, child: new Opacity(opacity: 0.0, child: content)));
}

最佳答案

如果你想使用 Colors.grey(而不是通过 Colors.fromRGBO 使用它的 RGB 等价物),

您可以指定颜色:Colors.grey.withOpacity(0.5)

关于android - 如何创建具有半透明背景和不透明前景的小部件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47852314/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com