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

编程入门 行业动态 更新时间:2024-10-09 03:30:43
本文介绍了如何创建具有半透明背景和不透明前景的小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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.

示例:

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, )

希望有帮助!

更多推荐

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

本文发布于:2023-11-05 02:52:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1559709.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:部件   不透明   前景   背景

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!