Using opacity yields wrong scaffold's background color
232
Use custom PageRouteBuilder
, when you navigate to your page.
Like:
Widget opaquePage(Widget page) => PageRouteBuilder(
opaque: false,
pageBuilder: (BuildContext context, _, __) => page,
);
Now navigate to your page:
Navigator.push(
context,
opaquePage(MyScaffoldWidget),
)
Then you should be able to make your Scaffold
's background transparent:
Scaffold(
backgroundColor: Colors.purple.withOpacity(.85),
...
Author by
rozerro
Updated on January 03, 2023Comments
-
rozerro over 1 year
main() => runApp( MaterialApp( home: CountryScreen(), ), );
and in the
CountryScreen
build's widget methodreturn Scaffold( backgroundColor: Colors.lightGreen.withAlpha(125), body: Column( ...
Scasffold's background color is below and obviously it's a wrong color because
Colors.lightGreen.withAlpha(125)
mixes with black.How to awoid such behaviour when using alpha or opacity with a color?
For instance, if I use a solid color
Colors.lightGreen
for the scaffold backgroud it doesn't mix up with a black and it is ok.-
tomerpacific over 2 yearsCan you share what your desired result looks like?
-
pskink over 2 yearsyou would like to have translucent main window and see other apps that are behind (for example for flutter desktop app)?
-
rozerro over 2 years@tomerpacific the desired resulted color for the scaffold backgroung should be a color if apply opacity 0.5 to
Colors.lightGreen
-
rozerro over 2 years@pskink not at all. the main window backgroud color should be comletely opaque with a color
Colors.lightGreen.withAlpha(125)
-
pskink over 2 yearsif you use non opaque colors the visual appearance depends on what color is behind: for example what is color
Colors.red.withAlpha(0)
? -
pskink over 2 yearsif you use non opaque colors the visual appearance depends on what color is behind: for example what is color
Colors.red.withAlpha(0)
? -
rozerro over 2 years@pskink it gives black backround because
withAlpha(0)
makes a color comletele transparent. -
pskink over 2 yearsthe same is with
127
if background color (behind) is white the result is different from black, if you want to see it in action tryColor.alphaBlend(Colors.lightGreen.withAlpha(125), Colors.red)
andColor.alphaBlend(Colors.lightGreen.withAlpha(125), Colors.white)
-
rozerro over 2 years@pskink using
withAlpha(125)
is like a desatutating a color and make it twice lighter compared to original color, right? but in the topic example it mixes up with black color -
pskink over 2 yearsno, it will not be desaturated (as i said it will depend on what color is behind): if you want to desaturate use
HSVColor
and itssaturation
property (ranges from 0..1) -
rozerro over 2 years@pskink using
alphaBlend
seems give a desired result but I think there shold be another solution in a such case -
pskink over 2 yearsit is
HSVColor
-S
stands forsaturation
-
rozerro over 2 years@pskink wraping the column in a container with a color of
Colors.lightGreen.withAlpha(125)
and not using scaffold bacground color is sipmler -
pskink over 2 yearsadditional
Column
andContainer
just to specify the color? if you dont likeColor.alphaBlend
orHSVColor
just useColors.lightGreen.shade400
orColors.lightGreen.shade300
or similar shades (shade500
is the same asColors.lightGreen
) -
rozerro over 2 years@pskink it's one more solution
-