image_compare_slider 2.8.0

SDKflutter
Platformandroidioswindowslinuxmacosweb

Easily compare two images with a slider and a draggable line/handle, fully customizable.

Image Compare Slider

ci pub package Coverage badge style: very good analysis Powered by Mason License: MIT

Inspired by react-compare-slider, this package allows you to easily compare two images with a slider.

PR's are welcome!

Installation 💻

❗ In order to start using Image Compare you must have the Dart SDK installed on your machine.

Add image_compare_slider to your pubspec.yaml:

dependencies:
  image_compare_slider:

Install it:

dart pub get

Import it:

import 'package:image_compare_slider/image_compare_slider.dart';

Use it:

//...
ImageCompareSlider(
  itemOne: const Image.asset('...'),
  itemTwo: const Image.network('...'),
)
//...

The widget its pretty simple and customizable, see:

Example

Customization 🎨

You can customize the widget with the following parameters:

ParameterTypeDescription
itemOneImageThe first image to compare
itemTwoImageThe second image to compare
changePositionOnHoverboolIf the slider should change position when the mouse is over it
handleSizeSizeThe size of the handle
handleRadiusBorderRadiusThe radius of the handle
fillHandleboolIf the handle should be filled
hideHandleboolIf the handle should be hidden
handlePositiondoubleThe position of the handle relative to the slider
handleFollowsPositionboolIf the handle should follow the position of the slider
onPositionChangevoid Function(double position)?The callback to be called when the position changes
directionSliderDirectionThe direction of the slider will clip the image
dividerColorColorThe color of the divider
handleColorColorThe color of the handle
handleOutlineColorColorThe color of the handle outline
dividerWidthdoubleThe width of the divider
itemOneBuilderWidget Function(Widget child, BuildContext context)?The wrapper for the first image
itemTwoBuilderWidget Function(Widget child, BuildContext context)?The wrapper for the second image
photoRadiusBorderRadiusGeometryRadius of the photo.

If you want to add some effects you can use the itemOneBuilder and itemTwoBuilder parameters to wrap the images with a ColorFilter or ImageFilter, or any other widget you want.

For example, to add a ImageFilter with a blur effect:

// ...
ImageCompareSlider(
  itemOne: const Image.asset('...'),
  itemTwo: const Image.asset('...'),
  itemOneBuilder: (child, context) => ImageFiltered(
  imageFilter: ImageFilter.blur(sigmaX: 2, sigmaY: 5),
    child: child,
  ),
)
// ...

Will result in:

You can also pass any custom properties to the Image for itemOne and itemTwo, and most of the Image properties will be applied to the ImageCompareSlider widget.

For example, adding a colorBlendMode with a color to itemOne:

// ...
ImageCompareSlider(
  itemOne: const Image.asset('...', color: Colors.red, colorBlendMode: BlendMode.overlay),
  itemTwo: const Image.asset('...'),
)
// ...

Will result in:

Customizing the handle and divider is also possible:

// ...
ImageCompareSlider(
  itemOne: const Image.asset('...'),
  itemTwo: const Image.asset('...'),
  handleSize: Size(0.05, 0.05),
  handleRadius: const BorderRadius.all(Radius.circular(50)),
  fillHandle: true,
  dividerColor: Colors.black,
  dividerWidth: 10,
  handlePosition: 0.8,
  // ...
)
// ...

Will result in:

If you are having problems because height/width is not the same for both images, consider using Intrinsics in the builder:

// ...
ImageCompareSlider(
  itemOne: const Image.asset('...'),
  itemTwo: const Image.asset('...'),
  itemOneBuilder: (child, context) => IntrinsicHeight(child: child),
  itemTwoBuilder: (child, context) => IntrinsicHeight(child: child),
  // or
  itemOneBuilder: (child, context) => IntrinsicWidth(child: child),
  itemTwoBuilder: (child, context) => IntrinsicWidth(child: child),
)