I created this flutter widget which shows a semi transparent background with a not transparent foreground. I use a stack to do that. Unfortunately I have to layout the background also with the content because otherwise the content does not size correctly.
Is there a way to let the background in a stack know about the size in the foreground without doing the layout twice?
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)));
}
I suppose you'd not need a stack to achieve it. You can directly specify the background by specifying the decoration for your widget.
Example:
new Container(
            decoration: new BoxDecoration(
              border: new Border.all(width: borderWidth ,color: Colors.transparent), //color is transparent so that it does not blend with the actual color specified
              borderRadius: const BorderRadius.all(const Radius.circular(30.0)),
              color: new Color.fromRGBO(255, 0, 0, 0.5) // Specifies the background color and the opacity
            ),
          child: _content,
        )
Hope that helped!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With