summaryrefslogtreecommitdiffstats
path: root/lib/switchable_image.dart
blob: 97c9b02e111f044c049cb56691fdccc22e4572ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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,
          ),
        ),
      ),
    );
  }
}