Skip to content

Commit

Permalink
Added vertical slider support
Browse files Browse the repository at this point in the history
  • Loading branch information
mannprerak2 committed Oct 2, 2019
1 parent 906e2d7 commit f58cad8
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 49 deletions.
30 changes: 22 additions & 8 deletions example/lib/main.dart
Expand Up @@ -22,14 +22,28 @@ class MyHomePage extends StatelessWidget {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Before After'), centerTitle: true),
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
BeforeAfter(
beforeImage: Image.asset('assets/after.jpg'),
afterImage: Image.asset('assets/before.jpg'),
),
],
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
flex: 1,
child: BeforeAfter(
beforeImage: Image.asset('assets/after.jpg'),
afterImage: Image.asset('assets/before.jpg'),
sliderOrientation: SliderOrientation.horizontal,
),
),
Expanded(
flex: 1,
child: BeforeAfter(
beforeImage: Image.asset('assets/after.jpg'),
afterImage: Image.asset('assets/before.jpg'),
sliderOrientation: SliderOrientation.vertical,
),
),
],
),
),
);
}
Expand Down
16 changes: 8 additions & 8 deletions example/pubspec.lock
Expand Up @@ -7,21 +7,21 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.0"
version: "2.3.0"
before_after:
dependency: "direct main"
description:
path: ".."
relative: true
source: path
version: "1.0.0"
version: "1.0.1"
boolean_selector:
dependency: transitive
description:
name: boolean_selector
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.4"
version: "1.0.5"
charcode:
dependency: transitive
description:
Expand Down Expand Up @@ -66,28 +66,28 @@ packages:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.6"
version: "1.1.7"
path:
dependency: transitive
description:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.6.2"
version: "1.6.4"
pedantic:
dependency: transitive
description:
name: pedantic
url: "https://pub.dartlang.org"
source: hosted
version: "1.7.0"
version: "1.8.0+1"
quiver:
dependency: transitive
description:
name: quiver
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.3"
version: "2.0.5"
sky_engine:
dependency: transitive
description: flutter
Expand Down Expand Up @@ -120,7 +120,7 @@ packages:
name: string_scanner
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.4"
version: "1.0.5"
term_glyph:
dependency: transitive
description:
Expand Down
67 changes: 41 additions & 26 deletions lib/src/custom_widget.dart
@@ -1,6 +1,8 @@
import 'package:before_after/src/rect_clipper.dart';
import 'package:flutter/material.dart';

enum SliderOrientation { vertical, horizontal }

class BeforeAfter extends StatefulWidget {
final Widget beforeImage;
final Widget afterImage;
Expand All @@ -10,19 +12,21 @@ class BeforeAfter extends StatefulWidget {
final Color thumbColor;
final double thumbRadius;
final Color overlayColor;

const BeforeAfter({
Key key,
@required this.beforeImage,
@required this.afterImage,
this.imageHeight,
this.imageWidth,
this.imageCornerRadius = 8.0,
this.thumbColor = Colors.white,
this.thumbRadius = 16.0,
this.overlayColor,
}) : assert(beforeImage != null),
final SliderOrientation sliderOrientation;
const BeforeAfter(
{Key key,
@required this.beforeImage,
@required this.afterImage,
this.imageHeight,
this.imageWidth,
this.imageCornerRadius = 8.0,
this.thumbColor = Colors.white,
this.thumbRadius = 16.0,
this.overlayColor,
this.sliderOrientation = SliderOrientation.horizontal})
: assert(beforeImage != null),
assert(afterImage != null),
assert(sliderOrientation != null),
super(key: key);

@override
Expand All @@ -31,13 +35,15 @@ class BeforeAfter extends StatefulWidget {

class _BeforeAfterState extends State<BeforeAfter> {
double _clipFactor = 0.5;

@override
Widget build(BuildContext context) {
return Stack(
alignment: Alignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.symmetric(horizontal: 24.0),
padding: widget.sliderOrientation == SliderOrientation.vertical
? const EdgeInsets.symmetric(vertical: 24.0)
: const EdgeInsets.symmetric(horizontal: 24.0),
child: SizedImage(
widget.afterImage,
widget.imageHeight,
Expand All @@ -46,9 +52,13 @@ class _BeforeAfterState extends State<BeforeAfter> {
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 24.0),
padding: widget.sliderOrientation == SliderOrientation.vertical
? const EdgeInsets.symmetric(vertical: 24.0)
: const EdgeInsets.symmetric(horizontal: 24.0),
child: ClipPath(
clipper: RectClipper(_clipFactor),
clipper: widget.sliderOrientation == SliderOrientation.vertical
? RectClipperVertical(_clipFactor)
: RectClipper(_clipFactor),
child: SizedImage(
widget.beforeImage,
widget.imageHeight,
Expand All @@ -57,11 +67,7 @@ class _BeforeAfterState extends State<BeforeAfter> {
),
),
),
Positioned(
top: 0.0,
bottom: 0.0,
right: 0.0,
left: 0.0,
Positioned.fill(
child: SliderTheme(
data: SliderThemeData(
overlayColor: widget.overlayColor,
Expand All @@ -70,11 +76,20 @@ class _BeforeAfterState extends State<BeforeAfter> {
activeTrackColor: Colors.transparent,
inactiveTrackColor: Colors.transparent,
),
child: Slider(
value: _clipFactor,
onChanged: (double factor) =>
setState(() => this._clipFactor = factor),
),
child: widget.sliderOrientation == SliderOrientation.vertical
? RotatedBox(
quarterTurns: 1,
child: Slider(
value: _clipFactor,
onChanged: (double factor) =>
setState(() => this._clipFactor = factor),
),
)
: Slider(
value: _clipFactor,
onChanged: (double factor) =>
setState(() => this._clipFactor = factor),
),
),
),
],
Expand Down
19 changes: 19 additions & 0 deletions lib/src/rect_clipper.dart
Expand Up @@ -18,3 +18,22 @@ class RectClipper extends CustomClipper<Path> {
@override
bool shouldReclip(CustomClipper<Path> oldClipper) => true;
}

class RectClipperVertical extends CustomClipper<Path> {
final double clipFactor;

RectClipperVertical(this.clipFactor);

@override
Path getClip(Size size) {
Path path = Path();
path.lineTo(0.0, size.height * clipFactor);
path.lineTo(size.width, size.height * clipFactor);
path.lineTo(size.width, 0.0);
path.close();
return path;
}

@override
bool shouldReclip(CustomClipper<Path> oldClipper) => true;
}
14 changes: 7 additions & 7 deletions pubspec.lock
Expand Up @@ -7,14 +7,14 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.0"
version: "2.3.0"
boolean_selector:
dependency: transitive
description:
name: boolean_selector
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.4"
version: "1.0.5"
charcode:
dependency: transitive
description:
Expand Down Expand Up @@ -52,28 +52,28 @@ packages:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.6"
version: "1.1.7"
path:
dependency: transitive
description:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.6.2"
version: "1.6.4"
pedantic:
dependency: transitive
description:
name: pedantic
url: "https://pub.dartlang.org"
source: hosted
version: "1.7.0"
version: "1.8.0+1"
quiver:
dependency: transitive
description:
name: quiver
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.3"
version: "2.0.5"
sky_engine:
dependency: transitive
description: flutter
Expand Down Expand Up @@ -106,7 +106,7 @@ packages:
name: string_scanner
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.4"
version: "1.0.5"
term_glyph:
dependency: transitive
description:
Expand Down

0 comments on commit f58cad8

Please sign in to comment.