android - 如何让透明容器从背景中脱颖而出?
问题描述
我正在尝试使我的容器透明,并且仍然希望它们能够轻松地从背景颜色中脱颖而出,就像下图一样。(这是PSD图像)
我尝试像这样包装Container
在一个Material
小部件中:
class customBar extends StatelessWidget {
const customBar({
Key key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Material(
color: Colors.transparent,
elevation: 2,
borderRadius: BorderRadius.circular(10),
child: Container(
width: double.infinity,
height: 65,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
//color: Color(0xff0a4873),
),
child: Text(
'',
textAlign: TextAlign.left,
),
),
);
}
}
但它给了它太多elevation
也尝试使用纯色,但这也不能满足我的要求,
Expanded(
flex: 4,
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
customBar(),
Container(
width: double.infinity,
height: 60,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Color(0xff0a4873),
),
child: Text(
'',
textAlign: TextAlign.left,
),
),
],
),
),
),
这是输出:
解决方案
Material
没有必要。如果要调整颜色透明度,请使用Colors.YOUR_COLOR.withOpacity(0.0 .. to .. 1.0)
或Color.fromRGBO(RED, BLUE, GREEN, 0.0 .. to .. 1.0)
示例:
Colors.blue.withOpacity(0.3);
// OR
Color.fromRGBO(50, 45, 220, 0.3);
编辑
您可以像这样简化布局。
主页
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: CustomContainer(
child: Column(
children: [
// other widgets and your containers here
],
),
),
);
}
}
您的 CustomContainer 小部件
class CustomContainer extends StatelessWidget {
final Widget child;
CustomContainer({this.child});
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomLeft,
colors: [
Color(0xFF1187be),
Color(0xff061465),
],
),
),
child: child,
);
}
}
推荐阅读
- java - 方法 count(JobViewWrapper) 已经定义
- google-fonts - 为什么只加载三种谷歌字体中的一种?
- allure - 在诱惑报告中,无法看到放在 @Step 注释方法中的记录器语句
- vue.js - 如何从状态 vuex 映射对象中的数组?
- c++ - 如何创建一个大小为 NxN 的方形二维数组,其中 N 由用户提供并填充一定范围内的随机数?
- html - 身体标签的背景图片不可见
- assembly - 汇编中数据段变量的绝对地址是什么?
- description-logic - 描述 需要对两个个体的相同性或差异性进行建模的逻辑表示
- react-native - 升级到 SDK 39 后使用 expo:build for .apk 时资产未显示
- c# - WaitAndRetryPolicy 与 BulkheadPolicy 结合,优先考虑重试。可能吗?