【发布时间】:2018-05-30 20:13:29
【问题描述】:
我创建了这个颤振小部件,它显示了一个半透明的背景和一个不透明的前景。我使用堆栈来做到这一点。不幸的是,我还必须使用内容来布置背景,否则内容的大小会不正确。
有没有办法让堆栈中的背景知道前景的大小而无需进行两次布局?
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)));
}
【问题讨论】: