Created
February 24, 2020 05:11
-
-
Save A1Gard/1479e270966483ed3485b064f7a166e8 to your computer and use it in GitHub Desktop.
parallax component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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