text stringlengths 1 372 |
|---|
final listItemOffset = listItemBox.localToGlobal( |
listItemBox.size.centerLeft(Offset.zero), |
ancestor: scrollableBox); |
// determine the percent position of this list item within the |
// scrollable area. |
final viewportDimension = scrollable.position.viewportDimension; |
final scrollFraction = |
(listitemoffset.dy / viewportDimension).clamp(0.0, 1.0); |
// calculate the vertical alignment of the background |
// based on the scroll percent. |
final verticalAlignment = alignment(0.0, scrollFraction * 2 - 1); |
// convert the background alignment into a pixel offset for |
// painting purposes. |
final backgroundSize = |
(backgroundimagekey.currentcontext!.findrenderobject() as RenderBox) |
.size; |
final listItemSize = context.size; |
final childRect = |
verticalAlignment.inscribe(backgroundSize, offset.zero & listItemSize); |
// paint the background. |
context.paintChild( |
0, |
transform: |
transform.translate(offset: offset(0.0, childRect.top)).transform, |
); |
<code_end> |
you need one final detail to achieve the parallax effect. |
the ParallaxFlowDelegate repaints when the inputs change, |
but the ParallaxFlowDelegate doesn’t repaint every time |
the scroll position changes. |
pass the ScrollableState’s ScrollPosition to |
the FlowDelegate superclass so that the FlowDelegate |
repaints every time the ScrollPosition changes. |
<code_start> |
class ParallaxFlowDelegate extends FlowDelegate { |
ParallaxFlowDelegate({ |
required this.scrollable, |
required this.listItemContext, |
required this.backgroundImageKey, |
}) : super(repaint: scrollable.position); |
} |
<code_end> |
congratulations! |
you now have a list of cards with parallax, |
scrolling background images. |
<topic_end> |
<topic_start> |
interactive example |
run the app: |
<code_start> |
import 'package:flutter/material.dart'; |
import 'package:flutter/rendering.dart'; |
const color darkBlue = Color.fromARGB(255, 18, 32, 47); |
void main() { |
runApp(const MyApp()); |
} |
class MyApp extends StatelessWidget { |
const MyApp({super.key}); |
@override |
widget build(BuildContext context) { |
return MaterialApp( |
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue), |
debugShowCheckedModeBanner: false, |
home: const scaffold( |
body: center( |
child: ExampleParallax(), |
), |
), |
); |
} |
} |
class ExampleParallax extends StatelessWidget { |
const ExampleParallax({ |
super.key, |
}); |
@override |
widget build(BuildContext context) { |
return SingleChildScrollView( |
child: column( |
children: [ |
for (final location in locations) |
LocationListItem( |
imageUrl: location.imageUrl, |
name: location.name, |
country: location.place, |
), |
], |
), |
); |
} |
} |
class LocationListItem extends StatelessWidget { |
LocationListItem({ |
super.key, |
required this.imageUrl, |
required this.name, |
required this.country, |
}); |
final string imageUrl; |
final string name; |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.