TreemapColorMapper.range constructor

const TreemapColorMapper.range(
  1. {required double? from,
  2. required double? to,
  3. required Color color,
  4. double? minSaturation,
  5. double? maxSaturation,
  6. String? name}
)

Applies color to the tiles which lies between the TreemapColorMapper.from and TreemapColorMapper.to given range. The TreemapColorMapper.from and TreemapColorMapper.to must not be null.

Applies this TreemapColorMapper.color value to the tiles which are in the given range. The legend item color and text are based on the TreemapColorMapper.name value.

late List<SocialMediaUsers> _socialMediaUsersData;
late List<TreemapColorMapper> _colorMappers;

  @override
  void initState() {
    _socialMediaUsersData = <SocialMediaUsers>[
      SocialMediaUsers('India', 'Facebook', 280),
      SocialMediaUsers('India', 'Instagram', 88),
      SocialMediaUsers('USA', 'Facebook', 190),
      SocialMediaUsers('USA', 'Instagram', 120),
      SocialMediaUsers('Japan', 'Twitter', 48),
      SocialMediaUsers('Japan', 'Instagram', 31),
    ];
    _colorMappers = <TreemapColorMapper>[
      TreemapColorMapper.range(from: 0, to: 100, color: Colors.green),
      TreemapColorMapper.range(from: 101, to: 200, color: Colors.blue),
      TreemapColorMapper.range(from: 201, to: 300, color: Colors.red),
     ];
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SfTreemap(
        dataCount: _socialMediaUsersData.length,
        weightValueMapper: (int index) {
          return _socialMediaUsersData[index].usersInMillions;
        },
        colorMappers: _colorMappers,
        levels: [
          TreemapLevel(
              groupMapper: (int index) {
                return _socialMediaUsersData[index].country;
              },
              colorValueMapper: (TreemapTile tile) => tile.weight,
          ),
        ],
      ),
    );
  }

class SocialMediaUsers {
  const SocialMediaUsers(
    this.country,
    this.socialMedia,
    this.usersInMillions,
  );
  final String country;
  final String socialMedia;
  final double usersInMillions;
}

See also:

Implementation

const TreemapColorMapper.range({
  required this.from,
  required this.to,
  required this.color,
  this.minSaturation,
  this.maxSaturation,
  this.name,
})  : assert(from != null && to != null && from <= to),
      assert((minSaturation == null && maxSaturation == null) ||
          (minSaturation != null &&
              maxSaturation != null &&
              minSaturation < maxSaturation &&
              (minSaturation >= 0 && minSaturation <= 1) &&
              (maxSaturation >= 0 && maxSaturation <= 1))),
      value = null;