Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making an Image widget disabled / grayed out

Tags:

flutter

I have a bunch of Image widgets which when in a disabled state, should look grayed out.

Is there a way to alter an existing image make it look disabled / grayed out?

I want to avoid having both an enabled image asset and a disabled image asset increasing the overall APK size instead of having the single asset.

like image 294
Ride Sun Avatar asked Dec 17 '17 02:12

Ride Sun


People also ask

How do I GREY out a widget in flutter?

For runtime grayscale you can use a dart image package with flutter. Load your images with dart image package, apply grayscale function, read the bytes from grayscale image and use it with Image. memory widget of flutter. You can just get bytes without applying grayscale for a coloured image.

How do I get an image to show in a widget?

From the Insert menu, point to Widgets, then point to Mobile, and select Image Viewer. Click the location on your document where you want to place the widget. The Grid/Graph containing the widget is displayed. Optionally, resize the widget by clicking and then dragging its handles.

How do I disable widgets on flutter?

In flutter, most widgets already come with an option to disable them for example in a RaisedButton we can set the onClicked function to null to disable, or we can use NeverScrollableScrollPhysics( ) to disable a ListView.

How do you change the opacity of an image in flutter?

Build the Appbar with the scaffold widget. Now use the Image. Network widget inside the body of the scaffold widget. Finally, set the opacity using colorBlendMode.


4 Answers

Set the widget as the child of a container, and add foregroundDecoration like this:

Container(
  foregroundDecoration: BoxDecoration(
    color: Colors.grey,
    backgroundBlendMode: BlendMode.saturation,
  ),
  child: child,
)

-- Update: Based on this new video from flutter team, there is another widget for this. The code is basically the same, and it is like this:

ColorFiltered(
  colorFilter: ColorFilter.mode(
    Colors.grey,
    BlendMode.saturation,
  ),
  child: child,
)

But I still prefer the method using Container forgroundDecoration, since container gives you more flexibilities in the same place.

like image 161
Sina Seirafi Avatar answered Oct 02 '22 19:10

Sina Seirafi


According flutter documentation of ColorFiltered you can use ColorFilter.matrix link like this:

const ColorFilter greyscale = ColorFilter.matrix(<double>[
  0.2126, 0.7152, 0.0722, 0, 0,
  0.2126, 0.7152, 0.0722, 0, 0,
  0.2126, 0.7152, 0.0722, 0, 0,
  0,      0,      0,      1, 0,
]);

ColorFiltered(
  colorFilter: greyscale,
  child: child,
);

In this case ColorFilter will ignore transparent areas of your images.

like image 23
Lomski Avatar answered Oct 02 '22 21:10

Lomski


I adapted Mark O'Sullivan post and created a general purpose widget:

To use it:

text = Text("Hellow World");
GrayedOut(text);

or

GrayedOut(text, grayedOut: true);

And the class

class GrayedOut extends StatelessWidget {
  final Widget child;
  final bool grayedOut;

  GrayedOut(this.child, {this.grayedOut = true});

  @override
  Widget build(BuildContext context) {
    return grayedOut ? new Opacity(opacity: 0.3, child: child) : child;
  }
}
like image 21
Brett Sutton Avatar answered Oct 02 '22 20:10

Brett Sutton


If your child is a .png image, flutter will render it with a grey background if you put Colors.grey. Using the same color of your widget background you will have a perfect disabled image

ColorFiltered(
    colorFilter: ColorFilter.mode(
        Colors.white,
        BlendMode.saturation,
    ),
    child: child,
)

If you want to change the color of your image when user clicks it, you can do this:

GestureDetector(
    onTap: () => {
        setState((){
            _active = !_active;
        })
    },
    child: ColorFiltered(
        colorFilter: ColorFilter.mode(                                       
            _active ? Colors.transparent : Colors.white,                                                         
            BlendMode.saturation,

        ),
        child: Image(
            image: Image.asset("assets/test.png").image
        ),
    ),
)
like image 29
Matteo Antolini Avatar answered Oct 02 '22 19:10

Matteo Antolini