diff options
author | Felipe Erias <felipeerias@igalia.com> | 2021-12-06 14:51:07 +0900 |
---|---|---|
committer | Felipe Erias <felipeerias@igalia.com> | 2021-12-06 14:51:07 +0900 |
commit | 122c32589fb76afc4d04252930221e91c1d31a5f (patch) | |
tree | 4fa5a25c6658546e13007e1c2e900b64c208f2be /lib/switchable_image.dart | |
parent | d7b7bfc0ecc141c97725448f17fda3512a9245a5 (diff) |
Preserve state between page changes
Add more interactive widgets
Diffstat (limited to 'lib/switchable_image.dart')
-rw-r--r-- | lib/switchable_image.dart | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/lib/switchable_image.dart b/lib/switchable_image.dart new file mode 100644 index 0000000..97c9b02 --- /dev/null +++ b/lib/switchable_image.dart @@ -0,0 +1,52 @@ +import 'package:flutter/material.dart'; + +// https://api.flutter.dev/flutter/dart-ui/ColorFilter/ColorFilter.matrix.html +const ColorFilter _identity = ColorFilter.matrix(<double>[ + 1, 0, 0, 0, // identity matrix + 0, 0, 1, 0, + 0, 0, 0, 0, + 1, 0, 0, 0, + 0, 0, 1, 0, +]); + +const ColorFilter _greyscale = ColorFilter.matrix(<double>[ + 0.2126, 0.7152, 0.0722, 0, 0, // greyscale filter + 0.2126, 0.7152, 0.0722, 0, 0, + 0.2126, 0.7152, 0.0722, 0, 0, + 0, 0, 0, 1, 0, +]); + +// This class is used to implement enabled/disabled icons with only one image: +// when the widget is disabled (value==false) the image is displayed in +// greyscale and at 50% opacity. +class SwitchableImage extends StatelessWidget { + final bool value; + final String imageAssetId; + final double width, height; + + const SwitchableImage({ + Key? key, + required this.value, + required this.imageAssetId, + required this.width, + required this.height, + }) : super(key: key); + + @override + Widget build(BuildContext context) { + return Container( + child: Opacity( + opacity: value ? 1.0 : 0.5, + child: ColorFiltered( + colorFilter: value ? _identity : _greyscale, + child: Image.asset( + imageAssetId, + width: width, + height: height, + fit: BoxFit.contain, + ), + ), + ), + ); + } +} |