Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scroll direction to parallax effect (image and container) #30

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 24 additions & 23 deletions lib/parallax.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import 'package:transformer_page_view/transformer_page_view.dart';
typedef void PaintCallback(Canvas canvas, Size siz);

class ColorPainter extends CustomPainter {

final Paint _paint;
final TransformInfo info;
final List<Color> colors;

ColorPainter(this._paint,this.info,this.colors);
ColorPainter(this._paint, this.info, this.colors);

@override
void paint(Canvas canvas, Size size) {
Expand Down Expand Up @@ -69,23 +68,16 @@ class ColorPainter extends CustomPainter {
}
}




class _ParallaxColorState extends State<ParallaxColor>{

class _ParallaxColorState extends State<ParallaxColor> {
Paint paint = new Paint();


@override
Widget build(BuildContext context) {
return new CustomPaint(
painter: new ColorPainter(paint,widget.info,widget.colors),
painter: new ColorPainter(paint, widget.info, widget.colors),
child: widget.child,
);
}


}

class ParallaxColor extends StatefulWidget {
Expand All @@ -105,30 +97,34 @@ class ParallaxColor extends StatefulWidget {
State<StatefulWidget> createState() {
return new _ParallaxColorState();
}


}

class ParallaxContainer extends StatelessWidget {
final Widget child;
final double position;
final TransformInfo info;
final double translationFactor;
final double opacityFactor;

ParallaxContainer(
{@required this.child,
@required this.position,
this.translationFactor: 100.0,
this.opacityFactor: 1.0})
: assert(position != null),
@required this.info,
this.translationFactor: 100.0,
this.opacityFactor: 1.0})
: assert(info != null),
assert(translationFactor != null);

@override
Widget build(BuildContext context) {
return Opacity(
opacity: (1 - position.abs()).clamp(0.0, 1.0) * opacityFactor,
opacity: (1 - info.position.abs()).clamp(0.0, 1.0) * opacityFactor,
child: new Transform.translate(
offset: new Offset(position * translationFactor, 0.0),
offset: new Offset(
info.scrollDirection == Axis.horizontal
? info.position * translationFactor
: 0.0,
info.scrollDirection == Axis.vertical
? info.position * translationFactor
: 0.0),
child: child,
),
);
Expand All @@ -138,14 +134,19 @@ class ParallaxContainer extends StatelessWidget {
class ParallaxImage extends StatelessWidget {
final Image image;
final double imageFactor;
final TransformInfo info;

ParallaxImage.asset(String name, {double position, this.imageFactor: 0.3})
ParallaxImage.asset(String name, {@required this.info, this.imageFactor: 0.3})
: assert(imageFactor != null),
image = Image.asset(name,
fit: BoxFit.cover,
alignment: FractionalOffset(
0.5 + position * imageFactor,
0.5,
info.scrollDirection == Axis.horizontal
? 0.5 + info.position * imageFactor
: 0.5,
info.scrollDirection == Axis.vertical
? 0.5 + info.position * imageFactor
: 0.5,
));

@override
Expand Down