Created
April 15, 2020 23:40
-
-
Save happyharis/cba6bc964100869336ce6aac6e418769 to your computer and use it in GitHub Desktop.
Parallax Effect in Flutter Web
This file contains hidden or 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'; | |
// Import transparent_image in pubspec.yaml file | |
import 'package:transparent_image/transparent_image.dart'; | |
void main() { | |
runApp(MyPortfolio()); | |
} | |
class MyPortfolio extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'We Can Code', | |
home: HomePage(), | |
); | |
} | |
} | |
class HomePage extends StatefulWidget { | |
@override | |
_HomePageState createState() => _HomePageState(); | |
} | |
class _HomePageState extends State<HomePage> { | |
double offset = 0; | |
@override | |
Widget build(BuildContext context) { | |
final height = MediaQuery.of(context).size.height; | |
final width = MediaQuery.of(context).size.width; | |
final nameStyle = Theme.of(context).textTheme.headline2; | |
final descriptionStyle = Theme.of(context).textTheme.headline4; | |
return Material( | |
child: NotificationListener<ScrollNotification>( | |
// When user scrolls, this will trigger onNotification. | |
// updateOffsetAccordingToScroll updates the offset | |
onNotification: updateOffsetAccordingToScroll, | |
// ScrollConfiguration sets the scroll glow behaviour | |
child: ScrollConfiguration( | |
behavior: NoScrollGlow(), | |
child: Stack( | |
children: <Widget>[ | |
Positioned( | |
// The hero image will be pushed up once user scrolls up | |
// That is why it is multiplied negatively. | |
top: -.25 * offset, | |
child: FadeInImage.memoryNetwork( | |
// From the transparent_image package | |
placeholder: kTransparentImage, | |
image: kHeroImage, | |
height: height, | |
width: width, | |
fit: BoxFit.fitWidth, | |
), | |
), | |
Positioned( | |
top: -.25 * offset, | |
child: SizedBox( | |
height: height, | |
width: width, | |
child: Align( | |
alignment: Alignment(0, 0), | |
child: Column( | |
mainAxisSize: MainAxisSize.min, | |
crossAxisAlignment: CrossAxisAlignment.start, | |
children: <Widget>[ | |
Text( | |
'Happy Haris', | |
style: nameStyle.copyWith( | |
backgroundColor: Colors.white, | |
), | |
), | |
SizedBox(height: 20), | |
Text( | |
'Proving learning coding, is easy', | |
style: descriptionStyle.copyWith( | |
backgroundColor: Colors.white, | |
), | |
), | |
], | |
)), | |
), | |
), | |
SingleChildScrollView( | |
child: Column( | |
children: <Widget>[ | |
SizedBox(height: height), | |
Container( | |
height: height, | |
width: width, | |
color: Colors.blueAccent, | |
) | |
], | |
), | |
) | |
], | |
), | |
), | |
), | |
); | |
} | |
/// Returns true to cancel the notification bubbling. | |
bool updateOffsetAccordingToScroll(ScrollNotification scrollNotification) { | |
setState(() => offset = scrollNotification.metrics.pixels); | |
return true; | |
} | |
} | |
class NoScrollGlow extends ScrollBehavior { | |
@override | |
Widget buildViewportChrome( | |
BuildContext context, | |
Widget child, | |
AxisDirection axisDirection, | |
) { | |
return child; | |
} | |
} | |
const kHeroImage = | |
'https://user-images.githubusercontent.com/31005114/79287313-fe7e5180-7ef5-11ea-948c-f2a33214d619.JPG'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment