Skip to content

Instantly share code, notes, and snippets.

@A1Gard
Created February 24, 2020 05:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save A1Gard/1479e270966483ed3485b064f7a166e8 to your computer and use it in GitHub Desktop.
Save A1Gard/1479e270966483ed3485b064f7a166e8 to your computer and use it in GitHub Desktop.
parallax component
import 'package:flutter/material.dart';
class ParallaxImage extends StatefulWidget {
final ImageProvider image;
final Widget child;
final double height;
ParallaxImage({Key key, this.image, this.height, this.child}) : super(key: key);
@override
_ParallaxImageState createState() => _ParallaxImageState();
}
class _ParallaxImageState extends State<ParallaxImage> {
ScrollController _scrollController;
double _scrollPosition = 0;
_scrollListener() {
setState(() {
_scrollPosition = _scrollController.position.pixels;
// debugPrint(_scrollPosition.toString());
});
}
@override
void initState() {
_scrollController = ScrollController();
_scrollController.addListener(_scrollListener);
super.initState();
}
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
controller: _scrollController,
child: Column(
children: <Widget>[
Container(
height: 200 - _scrollPosition,
decoration: BoxDecoration(
image: DecorationImage(
image: widget.image,
fit: BoxFit.fitWidth,
alignment: AlignmentDirectional.topStart,
),
),
),
Container(
child: widget.child,
),
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment