Created
May 5, 2026 13:13
-
-
Save renanyoy/2b24ce67d44b00e084dc6ae6394c1ee9 to your computer and use it in GitHub Desktop.
scrolling animation, special effect when scrolling
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'; | |
| void main() => runApp(MyApp()); | |
| class MyApp extends StatelessWidget { | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp(home: MyPage()); | |
| } | |
| } | |
| class MyPage extends StatefulWidget { | |
| @override | |
| _MyPageState createState() => _MyPageState(); | |
| } | |
| class _MyPageState extends State<MyPage> { | |
| // We will use this for AnimatedBuilder and ListView | |
| final scrollController = ScrollController(); | |
| @override | |
| void dispose() { | |
| scrollController.dispose(); | |
| super.dispose(); | |
| } | |
| @override | |
| Widget build(BuildContext context) => Scaffold( | |
| body: SafeArea( | |
| child: ListView.separated( | |
| controller: scrollController, | |
| itemCount: 100, | |
| separatorBuilder: (context, index) => SizedBox(height: 8.0), | |
| itemBuilder: (context, index) => ItemView( | |
| index: index, | |
| scrollController: scrollController, | |
| ), | |
| ), | |
| ), | |
| ); | |
| } | |
| class ItemView extends StatelessWidget { | |
| final int index; | |
| final ScrollController scrollController; | |
| const ItemView({ | |
| Key key, | |
| @required this.index, | |
| @required this.scrollController, | |
| }) : super(key: key); | |
| @override | |
| Widget build(BuildContext context) => AnimatedBuilder( | |
| animation: scrollController, | |
| builder: (context, widget) { | |
| final renderObject = context.findRenderObject() as RenderBox; | |
| final offsetY = renderObject?.localToGlobal(Offset.zero)?.dy ?? 0; | |
| print('$index: $offsetY'); | |
| // Use offsetY(or X) for your effect | |
| return Container( | |
| height: 44.0, | |
| color: Colors.pink[100], | |
| alignment: Alignment.center, | |
| child: Text(index.toString()), | |
| ); | |
| }, | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment