admin管理员组文章数量:1314566
Within an AppBar
, am able to correctly set the background color, as follows:
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.red,
within the actions for the AppBar
, the color of a FloatingActionButton
is set to the same value:
actions: [
FloatingActionButton(
backgroundColor: Colors.red,
This works as expected. However, if I add withValues(alpha: 0.3)
in both places, like
Colors.red.withValues(alpha: 0.3)
the background color of the AppBar
changes correctly to pink. However the color of the FloatingActionButton
is a not pink, rather a murky dark pink, as if there is black mixed in.
What is the issue, and what is the correct way to do this?
Thanks
Within an AppBar
, am able to correctly set the background color, as follows:
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.red,
within the actions for the AppBar
, the color of a FloatingActionButton
is set to the same value:
actions: [
FloatingActionButton(
backgroundColor: Colors.red,
This works as expected. However, if I add withValues(alpha: 0.3)
in both places, like
Colors.red.withValues(alpha: 0.3)
the background color of the AppBar
changes correctly to pink. However the color of the FloatingActionButton
is a not pink, rather a murky dark pink, as if there is black mixed in.
What is the issue, and what is the correct way to do this?
Thanks
Share Improve this question edited Jan 30 at 13:32 user1052610 asked Jan 30 at 11:56 user1052610user1052610 4,71914 gold badges62 silver badges108 bronze badges 1- Can you add the complete screen code? Maybe there’s a widget below FAB that is contributing to the dark component of the result. – Clevino Alrin Commented Jan 31 at 8:04
2 Answers
Reset to default 1In Flutter v3.27
, .withOpacity(0.3)
has been replaced with .withValues(alpha: 0.3)
.
Before
Colors.red.withOpacity(0.3)
After
Colors.red.withValues(alpha: 0.3)
For more details, you can check the Flutter migration guide: Migration Guide
If it doesn't work, you can try modifying the floatingActionButtonTheme
inside the theme property of MaterialApp
.
MaterialApp(
theme: ThemeData(
floatingActionButtonTheme: FloatingActionButtonThemeData(
backgroundColor: Colors.red.withAlph(alpha: 0.3)
),
),
Alternative Solution (Not Recommended)
You can wrap the Opacity
widget inside a DecoratedBox
or Container
with the decoration
property, all within a Stack
widget.
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.transparent,
onPressed: () {},
child: Stack(
children: <Widget>[
Positioned.fill(
child: Opacity(
opacity: 0.5,
child: Container(
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(15),
),
),
),
),
const Positioned.fill(
child: Icon(
Icons.edit,
),
),
],
),
),
This was the solution:
In the MaterialApp
theme:
floatingActionButtonTheme: const FloatingActionButtonThemeData(
backgroundColor: Colors.transparent),
Then, in the AppBar
, when defining the FloatingActionButton
, do not define the backgroundColor. It will be the same as defined for the AppBar
本文标签: flutterwithOpacity not working in FloatingActionButtonStack Overflow
版权声明:本文标题:flutter - withOpacity not working in FloatingActionButton - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741967717a2407647.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论